#pdf #font #path #margin #define #ignore #image #overflowing #error-tolerant

bin+lib code-to-pdf

Generates a syntax-highlighted PDF of your source code

9 releases

Uses new Rust 2024

new 0.1.9 Apr 17, 2025
0.1.8 Apr 7, 2025
0.1.5 Mar 29, 2025

#1716 in Text processing

Download history 263/week @ 2025-03-21 159/week @ 2025-03-28 344/week @ 2025-04-04 95/week @ 2025-04-11

877 downloads per month

MIT license

190KB
759 lines

code-to-pdf

Converts a folder of source code to a fully syntax-highlighted PDF

Features

  • Syntax highlights code (uses syntect for highlighting and two-face for syntax definitions)
  • Automatically handles line wrapping and page overflowing
  • Fast. Processing runs in parallel on multiple cores
  • Error-tolerant
  • Configurable (custom file exclusions, output filename, fonts)
  • Displays images
  • Respects ignore globs in .ignore and .gitignore (uses ignore)

Installation

From crates.io

cargo install code-to-pdf

Using cargo binstall

Installs a pre-built binary if it is available for your system

cargo binstall code-to-pdf

Using artefacts on Github Releases

Navigate to the latest release

Run the correct installer script for your system, or just download the correct binary directly

Build from repository source

git clone https://github.com/Tommypop2/code-to-pdf
cargo install --path ./code-to-pdf

Usage

Generating a PDF from a folder

c2pdf .

This walks the current folder and generates a syntax-highlighted PDF of all files in that folder

Excluding paths

c2pdf . --exclude pnpm-lock.yaml,*.lock

Custom output path

c2pdf . --out custom_name.pdf

Setting the font

Supports loading fonts from a path or the system fonts

# From a path
c2pdf . --font fonts/Helvetica.ttf

# From system fonts
c2pdf . --font "Arial"

Setting font size

c2pdf . --font-size 12.5 # 12.0 by default

Setting margins

c2pdf . --margin-top 20 --margin-bottom 5 --margin-left 10 --margin-right 10 # (these are the defaults)

Setting custom page text

This is text that is added to every page

c2pdf . --page-text "Hello\nWorld" # (use `\n` to indicate a newline)

Dependencies

~57MB
~745K SLoC