Skip to main content

Overview

repo2pdf uses highlight.js to automatically detect and highlight code syntax in your PDFs. This feature makes code more readable by applying colors to keywords, strings, comments, and other language elements.

How It Works

When syntax highlighting is enabled, repo2pdf:
  1. Detects the language based on file extension
  2. Applies highlight.js to parse and tokenize the code
  3. Maps HTML classes to PDF color values
  4. Renders colored text in the final PDF
From clone.ts:263-276:

Enabling Syntax Highlighting

When running repo2pdf, you’ll be prompted to select features:
Use the spacebar to select “Add highlighting” to enable this feature.

Color Mappings

repo2pdf uses a carefully selected color palette optimized for readability in PDF format. The colors are mapped from highlight.js token types to specific hex values.
Here are all the syntax element colors used by repo2pdf (from syntax.ts:8-41):

Supported Languages

repo2pdf supports all languages recognized by highlight.js, including:

JavaScript/TypeScript

.js, .jsx, .ts, .tsx

Python

.py, .pyw

Java/Kotlin

.java, .kt, .kts

C/C++

.c, .h, .cpp, .hpp

Ruby

.rb, .rake

Go

.go

Rust

.rs

PHP

.php

Swift

.swift
If a language is not recognized, repo2pdf will fall back to plaintext rendering without colors.

HTML to PDF Conversion

The conversion process works by:
  1. Parsing HTML output from highlight.js
  2. Extracting class names (e.g., hljs-keyword)
  3. Looking up colors from the color map
  4. Applying colors to PDF text segments
From syntax.ts:45-50:

Example Output

Here’s how different code elements appear in the PDF:

Tips for Better Highlighting

Use standard file extensions: Ensure your files use standard extensions (.js, .py, .java) for accurate language detection.
Combine with line numbers: Syntax highlighting works great with line numbers for maximum readability.
Very long files with complex syntax may take slightly longer to process due to highlight.js parsing.

Disabling Highlighting

If you prefer monochrome output, simply answer No when prompted:
All code will be rendered in black text on white background.

Line Numbers

Add line numbers alongside syntax highlighting

Code Formatting

Auto-format code before highlighting with Prettier