Skip to main content

Overview

Line numbers make it easy to reference specific lines of code in your PDF documentation. repo2pdf can automatically add line numbers to every line of code in your generated PDFs.

Enabling Line Numbers

When running repo2pdf, you’ll be prompted:
Select Yes to add line numbers to your PDF.

How It Works

Line numbers are added during the PDF generation process:
  1. Counts total lines in each file
  2. Calculates width needed for line numbers (handles 1-9999+ lines)
  3. Prepends line number to each line with proper padding
  4. Maintains alignment across all lines
From clone.ts:279-294:

Dynamic Width Calculation

Line numbers automatically adjust their width based on file size:

Short Files

1-9 linesSingle digit width:

Medium Files

10-99 linesTwo digit width:

Large Files

100+ linesThree+ digit width:
The width is calculated by counting newlines and converting to string length, ensuring perfect alignment.

Visual Examples

Without Line Numbers

With Line Numbers

Combined with Other Features

Line numbers work seamlessly with other repo2pdf features:
When both features are enabled:
The line numbers remain in black while syntax elements are colored.
When removing empty lines, line numbers skip the blank lines:Original:
With empty lines removed:
Be careful: the line numbers won’t match the original file when empty lines are removed!
When Prettier formatting is enabled, line numbers reflect the formatted output:Original (unformatted):
PDF output (formatted with line numbers):
Line numbers are added AFTER formatting, so they accurately reflect the PDF content.

Padding and Alignment

The padStart() method ensures consistent alignment:
This means:
  • Line 1 becomes " 1 " in a 3-digit file (100+ lines)
  • Line 10 becomes " 10 " in a 3-digit file
  • Line 100 becomes "100 " in a 3-digit file
The extra space after the line number creates visual separation from the code.

Use Cases

Code Reviews

Reference specific lines when reviewing code: “See line 42 in auth.ts”

Documentation

Point readers to exact locations in your codebase

Teaching

Help students follow along with specific line references

Debugging

Match PDF line numbers with error stack traces

Performance Considerations

Line number calculation is efficient:
  1. Single pass: Counts lines while rendering
  2. Minimal overhead: Simple string operations
  3. No re-parsing: Uses already-processed syntax data
Adding line numbers typically adds less than 5% to processing time.

Disabling Line Numbers

If you don’t need line numbers, simply answer No when prompted:
The PDF will contain code without line number prefixes.

Font and Styling

Line numbers use the same monospace font (Courier) as the code:
This ensures consistent alignment and readability.

Syntax Highlighting

Add colors to make code more readable

Page Numbers

Add page numbers to your PDF footer