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
--columnsto 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 viau'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
- Read the Source File: The script reads each file's content.
- Apply Syntax Highlighting: Uses Pygments to convert code into HTML with inline styles.
- Generate Temporary HTML: Wraps the highlighted code in an HTML template and saves it to a temporary HTML file.
- Convert HTML to PDF: Launches headless Chrome (via the
--headlessflag) to produce a PDF, respecting multi-column CSS. - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2e571db235054baaf7cc632e4418e57819ada8aeeff5c0970abe5a5afe03e6fd
|
|
| MD5 |
9766e0c15aec8af30c588805268c2a4e
|
|
| BLAKE2b-256 |
3dc28709c32dc9abc17160171e7103e23179c24be8c0cc260b1353adfe20cdcb
|
File details
Details for the file chromecodepdf-0.1.0a2-py2.py3-none-any.whl.
File metadata
- Download URL: chromecodepdf-0.1.0a2-py2.py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c40460bd8237efd413d7466d44b0b2ce157b4c8e5d851293b8821373e2e1d11d
|
|
| MD5 |
533e336e9a7857c27401691b2e2d3372
|
|
| BLAKE2b-256 |
4523f6d319949dddd1d6bf7f4d24d0547a1e9429b6942a5a80e80335871bd63a
|