Skip to main content

A Python script that converts code files into syntax-highlighted PDF documents using Chrome and Pygments.

Project description

chromecodepdf

A Python script that converts code files into syntax-highlighted PDF documents using Chrome and Pygments.

Features

  • Headless Chrome: Renders HTML to PDF, ensuring consistent appearance across platforms.
  • Syntax Highlighting using Pygments for various programming languages.
  • Multiple Columns: Use --columns to split the output into multiple columns.
  • Customizable Styles: Specify a color style with --style (any supported Pygments style).
  • Python 2 & 3 compatible
  • Windows & POSIX support
  • Unicode-safe, including non-ASCII paths

Requirements

  • Chrome, Chromium, or Microsoft Edge
    • A headless Chrome executable must be installed and discoverable.
    • If Chrome is not auto-detected, provide its path with --chrome-path.

Installation

pip install chromecodepdf

After installation, run it as chromecodepdf.

Usage

# Basic single-file conversion
chromecodepdf file1.py

# Multiple files at once
chromecodepdf script.sh code.c

# Specify columns
chromecodepdf --columns=2 code.c

# Override default syntax style (e.g., 'friendly')
chromecodepdf --style=friendly script.sh

# Specify a custom Chrome path (if auto-detection fails)
chromecodepdf --chrome-path="/usr/bin/google-chrome" file1.py

Example: Running on Windows with Non-ASCII Paths

Example command:

C:\Users\jifengwu2k\packages\chromecodepdf>chromecodepdf -c 2 "..\..\路径 有空格\名字 有空 格.py"

This example combines deep relative paths, Unicode, and spaces to demonstrate full Unicode and shell compatibility - a scenario where many tools break. ⚠️ Note: When used Windows, Unicode paths may appear garbled in the console (e.g., 璺緞...).
This happens because the browser may output text encoded in UTF-8, while the Windows console expects mbcs (legacy ANSI code page). Simulate via u'C:\\Users\\jifengwu\\路径 有空格\\名字 有空格.pdf'.encode('utf-8').decode('mbcs'). The file is still written correctly - this is a console display issue only.

Sample output:

INFO: Using Chrome executable C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe
76882 bytes written to file C:\Users\jifengwu\璺緞 鏈夌┖鏍糪鍚嶅瓧 鏈夌┖鏍?pdfINFO: Success: Created 'C:\Users\jifengwu\路径 有空格\名字 有空格.pdf'
INFO: Conversion complete: 1 succeeded, 0 failed

How It Works

  1. Read the Source File: The script reads each file's content.
  2. Apply Syntax Highlighting: Uses Pygments to convert code into HTML with inline styles.
  3. Generate Temporary HTML: Wraps the highlighted code in an HTML template and saves it to a temporary HTML file.
  4. Convert HTML to PDF: Launches headless Chrome (via the --headless flag) to produce a PDF, respecting multi-column CSS.
  5. Cleanup: Deletes any temporary HTML files upon completion. Logs successes and failures.

License

This script is released under the MIT License.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you'd like to change.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chromecodepdf-0.1.0a2.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

chromecodepdf-0.1.0a2-py2.py3-none-any.whl (6.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file chromecodepdf-0.1.0a2.tar.gz.

File metadata

  • Download URL: chromecodepdf-0.1.0a2.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.12

File hashes

Hashes for chromecodepdf-0.1.0a2.tar.gz
Algorithm Hash digest
SHA256 2e571db235054baaf7cc632e4418e57819ada8aeeff5c0970abe5a5afe03e6fd
MD5 9766e0c15aec8af30c588805268c2a4e
BLAKE2b-256 3dc28709c32dc9abc17160171e7103e23179c24be8c0cc260b1353adfe20cdcb

See more details on using hashes here.

File details

Details for the file chromecodepdf-0.1.0a2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for chromecodepdf-0.1.0a2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c40460bd8237efd413d7466d44b0b2ce157b4c8e5d851293b8821373e2e1d11d
MD5 533e336e9a7857c27401691b2e2d3372
BLAKE2b-256 4523f6d319949dddd1d6bf7f4d24d0547a1e9429b6942a5a80e80335871bd63a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page