Guide
Converting Markdown to PDF in Node.js Applications
Updated July 10, 2026
Node.js is widely used for building fast server-side applications, static site generators, and developer automation tools. For projects requiring PDF exports of blogs, user manuals, invoices, or resumes, integrating a compilation pipeline is straightforward. In this article, we will show you how to convert markdown to pdf nodejs applications using Puppeteer and popular open-source packages.
We will cover the top packages available in the npm ecosystem and build a script that converts Markdown into a clean, modern PDF document.
Choosing the Right Node.js Library
The JavaScript ecosystem has a few solid libraries for HTML-to-PDF rendering:
- Puppeteer: Google’s headless Chrome API. By converting Markdown to HTML and loading it in Puppeteer, you can generate exact replicas of web designs using standard CSS print features.
- md-to-pdf: A wrapper built around Puppeteer specifically designed to take Markdown files and write out formatted PDFs, supporting custom CSS stylesheets.
- pdfkit: A library for building PDFs dynamically from scratch. Similar to Python’s ReportLab, it requires custom draw calls and is less suitable for converting Markdown structure directly.
For most Node.js applications, compiling Markdown to HTML with a library like marked or markdown-it, and printing it using Puppeteer, is the most reliable approach.
Implementation: Custom Node.js PDF Converter
Let’s build a clean script that uses marked to convert Markdown to HTML and Puppeteer to compile the final PDF file.
1. Install Project Dependencies
First, initialize your project and install the required npm packages:
npm init -y
npm install puppeteer marked
2. Create the Conversion Script
Create a file named converter.js with the following JavaScript code:
import fs from 'fs';
import { marked } from 'marked';
import puppeteer from 'puppeteer';
async function convertMarkdownToPDF(inputPath, outputPath, cssPath) {
// Read the Markdown file
const markdownContent = fs.readFileSync(inputPath, 'utf8');
// Convert Markdown to HTML
const htmlContent = marked.parse(markdownContent);
// Read custom CSS if available
let customCSS = '';
if (cssPath && fs.existsSync(cssPath)) {
customCSS = fs.readFileSync(cssPath, 'utf8');
}
// Create full HTML template
const fullHtml = `
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<style>
body { font-family: system-ui, sans-serif; margin: 2rem; color: #333; }
pre { background: #f4f4f4; padding: 1rem; border-radius: 4px; }
${customCSS}
</style>
</head>
<body>
${htmlContent}
</body>
</html>
`;
// Launch headless browser
const browser = await puppeteer.launch({ headless: 'new' });
const page = await browser.newPage();
// Set the HTML content
await page.setContent(fullHtml, { waitUntil: 'networkidle0' });
// Generate the PDF file
await page.pdf({
path: outputPath,
format: 'A4',
margin: { top: '20mm', right: '20mm', bottom: '20mm', left: '20mm' },
printBackground: true
});
await browser.close();
}
// Execute the converter
convertMarkdownToPDF('document.md', 'document.pdf', 'theme.css')
.then(() => console.log('PDF exported successfully.'))
.catch(err => console.error('Error rendering PDF:', err));
This script reads your Markdown file, transforms it to HTML, injects your custom styling, launches a headless Chromium instance, prints the page layout to a PDF, and closes the browser.
Customizing Page Options in Puppeteer
Using Puppeteer gives you access to a rich set of options for configuring print properties in page.pdf():
displayHeaderFooter: Set totrueto show page headers and footers.headerTemplateandfooterTemplate: Allow HTML code to render page numbers and date stamps dynamically using thedate,title,url,pageNumber, andtotalPageshelper classes.
Conclusion
Converting Markdown to PDF inside your Node.js application is a powerful way to generate dynamic reports, invoices, and books. By wrapping Puppeteer, you gain total control over the page layout and rendering properties. For quick, one-off file edits, check out our browser-based online editor to write and export documents in real-time.
Written by Markdown to PDF Editorial Team
Our team specializes in document design, web standards, and developer utilities. This guide was researched and vetted against current browser printing standards and Paged.js specifications. Learn more on our About page.
Try it yourself — free, no signup
Convert your Markdown to a polished PDF right in your browser.
Open the editor