Overview
repo2pdf integrates Prettier to automatically format code before adding it to your PDF. This ensures consistent, readable code regardless of the original formatting.Code formatting is always enabled in repo2pdf. There’s no option to disable it - all supported files are automatically formatted.
How It Works
The formatting process:- Detects file extension (e.g.,
.js,.ts,.css) - Maps to Prettier parser using the parser configuration
- Formats the code with Prettier defaults
- Falls back to plain text if formatting fails
- Continues to syntax highlighting with formatted code
clone.ts:236-248:
Supported Languages
repo2pdf maps file extensions to Prettier parsers: Fromclone.ts:28-56:
JavaScript/TypeScript
.js→ babel.jsx→ babel.ts→ typescript.tsx→ typescript
Web Styles
.css→ css.scss→ scss.less→ less
Markup
.html→ html.xml→ xml.vue→ vue
Data Formats
.json→ json.yaml→ yaml.ini→ ini
Backend Languages
.java→ java.kotlin→ kotlin.swift→ swift.php→ php.ruby→ ruby.python→ python
Other
.md→ markdown.graphql→ graphql.sh→ shellDockerfile→ dockerfile.perl→ perl
Graceful Fallback
If Prettier cannot format a file (due to syntax errors or unsupported features), repo2pdf:- Logs a warning with the error message
- Uses the original content without formatting
- Continues processing normally
Before and After Examples
JavaScript Example
JavaScript Example
Before formatting:After Prettier formatting:
TypeScript Example
TypeScript Example
Before formatting:After Prettier formatting:
JSON Example
JSON Example
Before formatting:After Prettier formatting:
CSS Example
CSS Example
Before formatting:After Prettier formatting:
Prettier Configuration
repo2pdf uses Prettier’s default settings with only the parser specified:- Print Width: 80 characters
- Tab Width: 2 spaces
- Semicolons: Enabled
- Single Quotes: False (uses double quotes)
- Trailing Commas: “all” (in ES5)
Processing Order
The complete processing pipeline: Fromclone.ts:234-278:
- Read file content
- Format with Prettier (if parser available)
- Replace tabs with 4 spaces
- Normalize line endings
- Remove comments (if enabled)
- Remove empty lines (if enabled)
- Apply syntax highlighting
- Add to PDF
Unsupported File Types
If no parser is found, the file is processed without formatting:.go(Go).rs(Rust).c,.cpp(C/C++).lua(Lua).r(R)
Unsupported files are still included in the PDF - they just aren’t formatted by Prettier.
Tab Replacement
After formatting, tabs are replaced with spaces:Line Ending Normalization
All line endings are normalized to Unix-style (\n):
- Windows line endings (
\r\n) - Old Mac line endings (
\r)
Benefits of Auto-Formatting
Consistency
All code follows the same style, regardless of author preferences
Readability
Properly indented and spaced code is easier to read
Professional
PDFs look polished and well-maintained
Reduced Size
Consistent formatting can reduce PDF file size
Interaction with Other Features
Formatting + Line Numbers
Formatting + Line Numbers
Line numbers are added after formatting:Line numbers reflect the formatted code, not the original.
Formatting + Syntax Highlighting
Formatting + Syntax Highlighting
Syntax highlighting is applied after formatting:
- Format with Prettier → clean structure
- Highlight with highlight.js → add colors
Formatting + Comment Removal
Formatting + Comment Removal
If you enable comment removal, comments are removed after formatting: