This comparison evaluates six prominent npm packages used for creating, rendering, or converting documents into PDF format within JavaScript environments. The selection covers three distinct approaches: converting HTML to PDF (html-pdf, wkhtmltopdf, puppeteer), programmatically drawing PDF content (pdfkit, pdfmake), and rendering PDFs within React applications (react-pdf). Understanding the architectural differences between binary wrappers, headless browsers, and native JavaScript engines is critical for selecting the right tool for server-side generation or client-side display.
Building PDF functionality into a JavaScript application usually falls into one of three categories: converting HTML to PDF, drawing PDF elements programmatically, or displaying PDFs in the browser. The packages html-pdf, pdfkit, pdfmake, puppeteer, react-pdf, and wkhtmltopdf represent the most common tools for these tasks. However, they differ significantly in architecture, maintenance status, and developer experience.
Before diving into code, it is essential to address the lifecycle status of these libraries. Using deprecated tools introduces security risks and compatibility issues.
html-pdf is officially deprecated. It relies on PhantomJS, a headless browser that has been unmaintained since 2018.
// html-pdf: Deprecated usage (Do not use in new projects)
const pdf = require('html-pdf');
pdf.create('<h1>Hello</h1>').toFile('output.pdf', (err, res) => {
if (err) return console.error(err);
console.log(res.filename);
});
wkhtmltopdf wraps a binary tool that is also archived. While the npm wrapper still exists, the underlying engine does not support modern CSS standards like Flexbox or Grid.
// wkhtmltopdf: Binary wrapper usage
const wkhtmltopdf = require('wkhtmltopdf');
wkhtmltopdf('<h1>Hello</h1>', { output: 'output.pdf' });
puppeteer is actively maintained by the Chrome DevTools team and is the recommended replacement for html-pdf.
// puppeteer: Modern headless browser
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent('<h1>Hello</h1>');
await page.pdf({ path: 'output.pdf' });
await browser.close();
})();
pdfkit and pdfmake remain stable and actively maintained for programmatic generation.
// pdfkit: Low-level drawing
const PDFDocument = require('pdfkit');
const doc = new PDFDocument();
doc.pipe(require('fs').createWriteStream('output.pdf'));
doc.text('Hello', 100, 100);
doc.end();
// pdfmake: Declarative definition
const pdfmake = require('pdfmake/build/pdfmake');
const docDefinition = { content: ['Hello'] };
pdfmake.createPdf(docDefinition).download('output.pdf');
react-pdf is actively maintained for rendering PDFs in React components.
// react-pdf: Viewer component
import { Document, Page } from 'react-pdf';
function MyApp() {
return <Document file="file.pdf"><Page pageNumber={1} /></Document>;
}
The core difference lies in how these tools generate the final file. Some rely on external binaries, some use JavaScript engines, and others spin up a full browser.
puppeteer launches a real instance of Chrome. This ensures that what you see in the browser is exactly what you get in the PDF. It handles modern CSS, web fonts, and JavaScript execution.
// puppeteer: Handles complex CSS and JS
await page.setContent('<div style="display:flex"><span>A</span><span>B</span></div>');
await page.pdf({ path: 'flex-layout.pdf', printBackground: true });
wkhtmltopdf uses the Qt WebKit engine. It is faster than launching a full Chrome instance but fails on modern layouts.
// wkhtmltopdf: Limited CSS support
wkhtmltopdf('<div style="display:flex">Content</div>', { output: 'legacy.pdf' });
html-pdf used PhantomJS (WebKit). It is now obsolete and should not be compared for new feature work.
// html-pdf: Legacy PhantomJS engine
// No longer viable for modern CSS layouts
pdf.create(htmlContent).toBuffer(callback);
pdfkit is a pure JavaScript library. It does not understand HTML or CSS. You draw text and shapes using x/y coordinates.
// pdfkit: Coordinate-based drawing
doc.fontSize(25).text('Title', 100, 100);
doc.moveTo(100, 120).lineTo(200, 120).stroke();
pdfmake is also pure JavaScript but abstracts the drawing into a document model. You do not manage coordinates directly.
// pdfmake: Document model
const doc = { content: [{ text: 'Title', style: 'header' }] };
// Styles are defined separately in the 'styles' key
react-pdf runs in the browser to display files. It does not generate PDFs on the server.
// react-pdf: Client-side rendering
<Document file="/static/sample.pdf" />
How you style the content is often the deciding factor for frontend developers.
puppeteer allows you to use standard CSS. You can write media queries specifically for print.
// puppeteer: CSS Media Queries
await page.setContent(`
<style>@media print { .no-print { display: none; } }</style>
<div class="no-print">Hidden in PDF</div>
`);
wkhtmltopdf supports CSS but lacks modern features. Grid and Flexbox often break.
// wkhtmltopdf: Basic CSS only
wkhtmltopdf('<style>div { float: left; }</style><div>Content</div>', opts);
html-pdf supported CSS similar to wkhtmltopdf but is now defunct.
// html-pdf: Legacy CSS support
// Cannot rely on modern standards
pdf.create('<style>div { flex: 1; }</style>...').toFile(path);
pdfkit requires manual layout logic. There is no CSS. You calculate positions yourself.
// pdfkit: Manual positioning
doc.text('Label', 50, 50);
doc.text('Value', 150, 50);
pdfmake uses a style dictionary. It feels similar to CSS but is defined in JavaScript objects.
// pdfmake: Style dictionary
const doc = {
content: ['Text'],
styles: { header: { fontSize: 18, bold: true } }
};
react-pdf uses CSS for the container but relies on the PDF viewer for internal rendering.
// react-pdf: Component styling
<Document className="pdf-container" file="url" />
Integrating PDFs into a React workflow varies wildly between generation and viewing.
react-pdf is designed specifically for React. It provides components to render PDF pages as canvas elements.
// react-pdf: Native React components
import { Document, Page } from 'react-pdf';
export default function Viewer({ file }) {
return <Document file={file}><Page pageNumber={1} /></Document>;
}
puppeteer runs on Node.js. You typically call it from an API route, not directly in the frontend component.
// puppeteer: Server-side API route
app.post('/generate', async (req, res) => {
const browser = await puppeteer.launch();
// ... generate pdf ...
res.send(buffer);
});
pdfkit and pdfmake are also server-side libraries. They stream data to the client.
// pdfkit: Streaming to response
doc.pipe(res);
doc.text('Invoice');
doc.end();
// pdfmake: Server-side generation
const PdfPrinter = require('pdfmake');
const printer = new PdfPrinter(fonts);
const pdfDoc = printer.createPdfKitDocument(docDefinition);
html-pdf and wkhtmltopdf are legacy server-side tools with no specific React integration.
// html-pdf: Callback based
pdf.create(html).toStream((err, stream) => { stream.pipe(res); });
// wkhtmltopdf: Pipe to response
wkhtmltopdf(html, { output: res });
| Feature | puppeteer | pdfkit | pdfmake | react-pdf | wkhtmltopdf | html-pdf |
|---|---|---|---|---|---|---|
| Primary Use | HTML to PDF | Draw PDF | Define PDF | View PDF | HTML to PDF | HTML to PDF |
| Engine | Headless Chrome | Native JS | Native JS | Browser | Qt WebKit | PhantomJS |
| CSS Support | Full Modern | None | Custom DSL | N/A | Partial | Partial |
| Status | β Active | β Active | β Active | β Active | β οΈ Archived | β Deprecated |
| Complexity | High (Browser) | Medium (Coords) | Low (Object) | Low (Component) | Medium (Binary) | Low (Legacy) |
For HTML-to-PDF conversion in 2024, puppeteer is the only viable choice among the conversion tools. It guarantees that your PDF matches your web page. Avoid html-pdf and wkhtmltopdf unless you are maintaining legacy systems that cannot be updated.
For programmatic generation (invoices, tickets), choose pdfmake for ease of use and structure. Choose pdfkit only if you need custom drawing operations that pdfmake does not support.
For displaying PDFs in a React application, react-pdf is the standard solution. Do not confuse it with generation libraries.
Final Thought: The PDF ecosystem is split between conversion (HTML) and creation (Drawing). Pick the tool that matches your source data. If your data is HTML, use puppeteer. If your data is JSON or database records, use pdfmake or pdfkit. If you need to show the result, use react-pdf.
Choose puppeteer if you need high-fidelity HTML-to-PDF conversion with modern CSS support. It is the industry standard for converting complex web pages, dashboards, or emails into PDF format using a real headless Chrome engine.
Choose pdfkit if you need low-level control over PDF structure and drawing operations. It is ideal for generating dynamic reports, invoices, or tickets where you programmatically define coordinates and vectors rather than using HTML layouts.
Choose react-pdf if your goal is to display existing PDF files within a React frontend application. Note that this is a viewer library, not a generator. For creating PDFs using React components, evaluate @react-pdf/renderer instead.
Choose pdfmake if you prefer defining documents using a declarative JSON-like structure instead of drawing commands. It is well-suited for standard business documents like invoices and reports where consistent styling is managed through a central definition object.
Avoid using html-pdf for new projects as it is deprecated and relies on PhantomJS, which is no longer maintained. It is only relevant for legacy systems that cannot be refactored. Migrate to puppeteer for modern HTML-to-PDF conversion needs.
Choose wkhtmltopdf only if you require a lightweight binary wrapper for server-side HTML conversion and cannot accommodate the resource overhead of a headless browser. Be aware that the underlying project is archived and lacks support for modern CSS features.
Puppeteer is a JavaScript library which provides a high-level API to control Chrome or Firefox over the DevTools Protocol or WebDriver BiDi. Puppeteer runs in the headless (no visible UI) by default
npm i puppeteer # Downloads compatible Chrome during installation.
npm i puppeteer-core # Alternatively, install as a library, without downloading Chrome.
:::note
Modern package managers (including npm (see the RFC), pnpm, Yarn, Bun, and Deno) block dependency install scripts by default. If the install script is blocked, Puppeteer will not download the browser during installation, leading to runtime errors.
You can manually download the required browsers after installation by running:
npx puppeteer browsers install
Alternatively, you can configure your package manager to allow the install script to run (for example, with npm, by adding "puppeteer" to "allowScripts" in your package.json).
:::
Install chrome-devtools-mcp,
a Puppeteer-based MCP server for browser automation and debugging.
Puppeteer also supports the experimental WebMCP API.
import puppeteer from 'puppeteer';
// Or import puppeteer from 'puppeteer-core';
// Launch the browser and open a new blank page.
const browser = await puppeteer.launch();
const page = await browser.newPage();
// Navigate the page to a URL.
await page.goto('https://developer.chrome.com/');
// Set the screen size.
await page.setViewport({width: 1080, height: 1024});
// Open the search menu using the keyboard.
await page.keyboard.press('/');
// Type into search box using accessible input name.
await page.locator('::-p-aria(Search)').fill('automate beyond recorder');
// Wait and click on first result.
await page.locator('.devsite-result-item-link').click();
// Locate the full title with a unique string.
const textSelector = await page
.locator('::-p-text(Customize and automate)')
.waitHandle();
const fullTitle = await textSelector?.evaluate(el => el.textContent);
// Print the full title.
console.log('The title of this blog post is "%s".', fullTitle);
await browser.close();