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:How It Works
Line numbers are added during the PDF generation process:- Counts total lines in each file
- Calculates width needed for line numbers (handles 1-9999+ lines)
- Prepends line number to each line with proper padding
- Maintains alignment across all lines
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:Line Numbers + Syntax Highlighting
Line Numbers + Syntax Highlighting
When both features are enabled:The line numbers remain in black while syntax elements are colored.
Line Numbers + Empty Line Removal
Line Numbers + Empty Line Removal
When removing empty lines, line numbers skip the blank lines:Original:With empty lines removed:
Line Numbers + Code Formatting
Line Numbers + Code Formatting
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
ThepadStart() method ensures consistent alignment:
- 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
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:- Single pass: Counts lines while rendering
- Minimal overhead: Simple string operations
- No re-parsing: Uses already-processed syntax data
Adding line numbers typically adds less than 5% to processing time.