Markdown to PDF Extension
A Visual Studio Code extension that converts Markdown files to PDF with a single command.
Features
- Convert any Markdown file to PDF
- Custom styled output with code highlighting
- Inline code with grey background and monospace font
- Code blocks with grey background and syntax highlighting
- Right-click context menu integration for Markdown files
- Command palette support
- Option to open the generated PDF or show it in file explorer
- Progress notification during conversion
Usage
Method 1: Command Palette
- Open a Markdown file in VS Code
- Press
Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac) to open the command palette
- Type "Markdown to PDF: Convert to PDF" and press Enter
- The PDF will be generated in the same directory as your Markdown file
- Open a Markdown file in VS Code
- Right-click anywhere in the editor
- Select "Markdown to PDF: Convert to PDF" from the context menu
Development
Prerequisites
- Node.js (v14 or higher)
- npm
Setup
cd markdown-to-pdf-extension
npm install
npm run compile
Running the Extension
- Open the project in VS Code
- Press
F5 to open a new Extension Development Host window
- Create or open a Markdown file in the new window
- Test the extension using either method described above
Building for Production
npm run vscode:prepublish
Installing Locally
To install the extension locally:
- Install vsce:
npm install -g @vscode/vsce
- Package the extension:
vsce package
- Install the generated
.vsix file: code --install-extension markdown-to-pdf-0.0.1.vsix
Configuration
You can customize the PDF output by modifying the extension settings in VS Code.
Method 1: Settings UI
- Open VS Code Settings:
Ctrl+, (Windows/Linux) or Cmd+, (Mac)
- Search for "Markdown to PDF"
- Adjust the settings as needed
Method 2: settings.json
- Open Command Palette:
Ctrl+Shift+P / Cmd+Shift+P
- Type "Preferences: Open User Settings (JSON)"
- Add your configuration:
{
"markdownToPdf.fontSize": 11,
"markdownToPdf.paperFormat": "A4",
"markdownToPdf.paperOrientation": "portrait",
"markdownToPdf.paperBorder": "2cm",
"markdownToPdf.bodyPadding": 10,
"markdownToPdf.lineHeight": 1.5
}
Available Settings
| Setting |
Type |
Default |
Description |
markdownToPdf.fontSize |
number |
11 |
Font size in pixels (8-20) |
markdownToPdf.paperFormat |
string |
"A4" |
Paper format (A3, A4, A5, Legal, Letter, Tabloid) |
markdownToPdf.paperOrientation |
string |
"portrait" |
Paper orientation (portrait, landscape) |
markdownToPdf.paperBorder |
string |
"2cm" |
Page margins (e.g., "2cm", "1in", "10mm") |
markdownToPdf.bodyPadding |
number |
10 |
Body padding in pixels (0-50) |
markdownToPdf.lineHeight |
number |
1.5 |
Line height multiplier (1.0-2.5) |
Examples
Larger text with more spacing:
{
"markdownToPdf.fontSize": 14,
"markdownToPdf.bodyPadding": 20,
"markdownToPdf.lineHeight": 1.8
}
Letter size for US documents:
{
"markdownToPdf.paperFormat": "Letter",
"markdownToPdf.paperBorder": "1in"
}
Compact layout:
{
"markdownToPdf.fontSize": 10,
"markdownToPdf.bodyPadding": 5,
"markdownToPdf.lineHeight": 1.3,
"markdownToPdf.paperBorder": "1.5cm"
}
Custom Styling
The extension uses a custom CSS file (styles/pdf-style.css) that provides:
- Code syntax highlighting with grey backgrounds
- Monospace fonts for inline code (
`code`)
- Styled code blocks (
```code```)
- Clean typography for headings, lists, and tables
- GitHub-inspired styling
For advanced customization, you can edit styles/pdf-style.css in the extension directory.
Dependencies
markdown-pdf: Core library for converting Markdown to PDF
- Uses PhantomJS for rendering (bundled with markdown-pdf)
License
MIT