This release is a pre-release and may not be stable for production use.
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.
Release files for chromecodepdf 0.1.0a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| chromecodepdf-0.1.0a2.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| chromecodepdf-0.1.0a2-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 11.7 kB
Release files / chromecodepdf-0.1.0a2.tar.gz
| Download URL | chromecodepdf-0.1.0a2.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2e571db235054baaf7cc632e4418e57819ada8aeeff5c0970abe5a5afe03e6fd
|
|
BLAKE2b-256 checksum How to use checksums |
3dc28709c32dc9abc17160171e7103e23179c24be8c0cc260b1353adfe20cdcb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|
Release files / chromecodepdf-0.1.0a2-py2.py3-none-any.whl
| Download URL | chromecodepdf-0.1.0a2-py2.py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
c40460bd8237efd413d7466d44b0b2ce157b4c8e5d851293b8821373e2e1d11d
|
|
BLAKE2b-256 checksum How to use checksums |
4523f6d319949dddd1d6bf7f4d24d0547a1e9429b6942a5a80e80335871bd63a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|