How to Generate PDFs in Node.js with LightningPDF
Learn how to generate professional PDFs from HTML, markdown, or templates using the LightningPDF API in Node.js.
By LightningPDF Team ·
How to Generate PDFs in Node.js
Generating PDFs programmatically is one of the most common requirements in web applications. Whether you need invoices, receipts, reports, or certificates, LightningPDF makes it simple with a single API call.
Quick Start
Install the HTTP client of your choice (we'll use fetch which is built into Node.js 18+):
const response = await fetch('https://lightningpdf.dev/api/v1/pdf/generate', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
html: '<h1>Hello World</h1><p>This is my first PDF.</p>',
options: {
format: 'A4',
margin_top: '20mm',
margin_bottom: '20mm'
}
})
});
const result = await response.json();
// result.data.pdf contains base64-encoded PDF
const pdfBuffer = Buffer.from(result.data.pdf, 'base64');
Using Templates
Instead of raw HTML, you can use pre-built templates with variable substitution:
const response = await fetch('https://lightningpdf.dev/api/v1/pdf/generate', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
template_id: 'invoice-standard',
variables: {
company_name: 'Acme Corp',
invoice_number: 'INV-2026-001',
customer_name: 'John Doe',
items: [
{ description: 'Web Development', quantity: 40, unit_price: 150 },
{ description: 'Design Services', quantity: 10, unit_price: 100 }
],
total: 7000
}
})
});
Using Markdown
You can also generate PDFs directly from markdown:
const response = await fetch('https://lightningpdf.dev/api/v1/pdf/generate', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
markdown: '# Monthly Report\n\n## Summary\n\nRevenue increased by **15%** this quarter.\n\n| Metric | Value |\n|--------|-------|\n| Revenue | $150,000 |\n| Customers | 1,200 |'
})
});
Choosing the Right Engine
LightningPDF offers two rendering engines:
- Native engine (
engine: "native"): Generates PDFs in under 100ms. Best for invoices, receipts, reports, and certificates. - Chromium engine (
engine: "chromium"): Full HTML/CSS/JS rendering in 1-3 seconds. Best for complex layouts with flexbox, grid, or custom fonts.
By default, the API automatically selects the best engine for your content.
Next Steps
- Browse the template marketplace for ready-to-use templates
- Read the API documentation for all available options
- Sign up free to get your API key (50 PDFs/month free)
Related Reading
- Generate PDFs in Python — Python integration guide
- Generate PDFs in Go — Go tutorial with invoice example
- HTML to PDF: The Complete Guide — All approaches compared
- How to Fix PDF Page Breaks — Solve page break issues
- Best PDF APIs in 2026 — Full API comparison
- LightningPDF vs Puppeteer — Why use an API instead of DIY