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.
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.
PDF generation in Node.js is the process of programmatically creating PDF documents from HTML, markdown, or template data using JavaScript, typically through a headless browser like Puppeteer or a cloud API like LightningPDF.
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)
Frequently Asked Questions
What is the easiest way to generate PDFs in Node.js?
The easiest way to generate PDFs in Node.js is using the LightningPDF API with the built-in fetch function available in Node.js 18 and later. Send your HTML or template data as JSON to the API endpoint and receive a PDF in the response, all in about 5 lines of code.
Do I need Puppeteer to generate PDFs in Node.js?
No, you do not need Puppeteer to generate PDFs in Node.js. LightningPDF handles all browser rendering on its servers, so you just send HTML via a REST API call. This eliminates the need to install Chrome, manage browser instances, or deal with memory and scaling issues.
How fast can I generate PDFs in Node.js?
With LightningPDF's native engine, you can generate simple PDFs like invoices in under 100 milliseconds from Node.js. Complex layouts with advanced CSS are rendered via Chromium in 1 to 3 seconds. The API automatically selects the best engine for your content.
Can I use templates for PDF generation in Node.js?
Yes, LightningPDF supports template-based PDF generation where you pass a template ID and JSON variables. You can use templates from the marketplace or create custom ones in the visual designer. Variables are interpolated server-side, so your Node.js code only sends the data.
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
LightningPDF Team
Building fast, reliable PDF generation tools for developers.