Skip to main content

Save code files into PDF format with syntax highlighting.

Project description

PDFCode

Save code files into PDF format with syntax highlighting.

It can convert a single code file or all files in a project (folder) using one command.


1. Installation

MacOS:

> pip install PDFCode

########################################
# install wkhtmltopdf
> brew cask reinstall wkhtmltopdf
# or
# brew install caskroom/cask/wkhtmltopdf
########################################


########################################
# install libmagic
> brew install libmagic
########################################

2. Usage

  • 2.1 Python Library
from PDFCode import PDFCode
  • 2.2 Console app. Open a terminal and type the following command:
> pdfcode --help 

# usage: PDFCode.py [-h] [--dst [DST]] [-l] [-s SIZE] [-S NAME] [-m MARGIN]
#                   [--ignore IGNORE] [-v]
#                   src

# Convert source files into .pdf with syntax highlighting

# positional arguments:
#   src                   The path of the file/folder

# optional arguments:
#   -h, --help            show this help message and exit
#   --dst [DST]           The path of the saving target folder. Empty will save
#                         to PDFCode_Results/
#   -l, --linenos         Include line numbers.
#   -s SIZE, --size SIZE  PDF size. Letter,A1,A2,A3,A4,A5, etc.
#   -S NAME, --style NAME
#                         The style name for highlighting.
#   -m MARGIN, --margin MARGIN
#                         The layout margins in inch (default 0.4in).
#   --ignore IGNORE       Ignore files whose names contain sub-string. (multiple
#                         ignore str splited using , e.g., --ignore abc,.jpg to
#                         ignore 'abc' and '.jpg')
#   -v, --version         show program's version number and exit

Example

# process single file:
> pdfcode ./test/algol.py -s a4 -S colorful --ignore .json,test

# process all files in a folder and its subfolders:
> pdfcode ./test -s a4 -S colorful 

Now, the code supports 36 styles:

abap algol algol_nu arduino autumn borland
bw colorful default emacs friendly fruity
igor inkpot lovelace manni monokai murphy
native paraiso-dark paraiso-light pastie perldoc rainbow_dash
rrt sas solarized-dark solarized-light stata stata-dark
stata-light tango trac vim vs xcode

3. Acknowledgements

  • This code is modified from code2pdf. I am holding this repository because the code2pdf had the following problems at the moment when I was trying to use.
    1. The generated PDF files did not have syntax highlighting (all black-white).
    2. The command only supported to process a single file. Sometime, I want to process multiple code files.
    3. The code2pdf used python2 and pyqt4. They were not easy to setup on my machine (especially pyqt4).
  • This code is implemented based on libraries like pdfkit, magic, pygments, etc. I appreciate the contributations of all of these authors.

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

PDFCode-0.1.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

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

PDFCode-0.1.2-py3-none-any.whl (6.4 kB view details)

Uploaded Python 3

File details

Details for the file PDFCode-0.1.2.tar.gz.

File metadata

  • Download URL: PDFCode-0.1.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for PDFCode-0.1.2.tar.gz
Algorithm Hash digest
SHA256 7a3db2e0b9cde47a1a955d0f61751b58b0985bb64632f7076532d06c8107ad26
MD5 96f9891ad5f750bed7e078d8d9b18b63
BLAKE2b-256 56943eac918755d58435309574c93e3c630b23a384830a830c0b95a0b8a46160

See more details on using hashes here.

File details

Details for the file PDFCode-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: PDFCode-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3.post20200330 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.6.10

File hashes

Hashes for PDFCode-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ea8b2fcc899f838c93d4078c27034bff2d8910250f0eb63d3d0f2984a6ab2500
MD5 966650579a91155bab8ec7aed9bbeed8
BLAKE2b-256 83d92f6ad8d8cc6eeb85d78434015835414b453e57a254856886bf4f296567cf

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