RenderPDF.js: A Library for Converting HTML to PDF
RenderPDF.js converts HTML to PDF using the positions and dimensions of a rendered page. It writes text and simple shapes directly to the PDF, capturing more complex effects as images only where needed.
Why I built RenderPDF.js
When I used jsPDF to export HTML, the result wasn't precise enough. CSS layouts shifted, and the PDF looked different from the page in the browser. I didn't want to adjust the same layout separately for the web and PDF.
That led me to build RenderPDF.js. I wanted to use the layout the browser had already produced while keeping text selectable in the PDF. For more complex CSS effects, the library can capture just the affected area as an image.
How does the library work?
The browser first lays out the page according to its CSS. RenderPDF.js reads the positions of elements, text lines, and tables from that layout and transfers them to the PDF. It does not calculate Flexbox or Grid again.
The measured positions determine the drawing order. Supported text, images, fills, and simple shapes are written directly to the PDF. Some effects, including gradients and inset shadows, cannot be preserved that way. In those cases, the library can capture just the affected area as an image. It does not paint that area's contents twice; text can still be copied where it is safe to preserve it. An image capture can increase file size and export time.
The library then splits the content into pages. It follows supported CSS page breaks, keeps text and table rows together, and can repeat table headers. Content that is too wide can be scaled to the page width without reflowing the HTML. During export, it also checks whether the source document has changed. Coordinates are converted from CSS pixels to PDF points only when writing the file (1 px = 0.75 pt).
The supported scope mainly covers static content without overlapping elements. Arbitrary stacking, vertical text, and some effects and font features are unsupported. Depending on the problem, the library returns an error or a warning. You can also configure it to reject selected unsupported decorations instead of omitting them.
What else the library can do
Besides HTML, imageToPDF() converts PNG, JPEG, and supported multipage TIFF files. It does not recognize text in images. With createDocument(), you can build a PDF from text, images, vector shapes, links, and form fields. mergePDFs(), extractPDFPages(), and splitPDF() work with pages from existing PDFs without turning them into images. The drawing and composition APIs also work in Node.js. The API documentation covers the inputs and limitations.
Using it in an app
The source code and examples are on GitHub. Install the npm package with:
1npm install renderpdf-jsThen convert an element on the page to PDF:
1import { renderPDF } from 'renderpdf-js';
2
3const source = document.querySelector<HTMLElement>('#report');
4if (!source) throw new Error('Report element was not found.');
5
6const pdf = await renderPDF(source, {
7 format: 'A4',
8 margin: 24,
9 fit: 'width',
10 pageBreak: 'css',
11 repeatTableHeaders: true,
12 onWarning: (warning) => console.warn(warning.code, warning.message),
13});
14
15await pdf.download('report.pdf');This example uses renderPDF() because the source is an element on the page. If your HTML is a string, use renderHTML(): it lays out the content in a temporary isolated document before converting it to PDF.
fit: 'width' scales content that is wider than the page while preserving its original line breaks. pageBreak: 'css' reads supported break-before, break-after, and break-inside rules. Repeating a table header requires a real thead in the HTML.
For documents where typography needs to match, provide the same licensed font files used by the page and set fontMismatch: 'error'. A substitute font can change line breaks. For more complex documents, fallback: 'auto' enables local image captures, strictCss: true rejects selected unsupported decorations, and rasterScale controls capture resolution. header, footer, metadata, and onProgress configure repeated page content, PDF properties, and progress reporting. After export, check the warnings and the finished pages.
Measurements against jsPDF
Across 24 short synthetic test documents, RenderPDF.js had a lower median export time in 21 cases. jsPDF was faster in three cases involving a gradient, an inset shadow, or an explicitly rasterized area. RenderPDF.js produced a smaller file in 22 cases. Both tools preserved all 460 checked text markers. In all 16 single-page documents that could be compared visually, the RenderPDF.js output was closer to the browser preview.
Chart 1. Time in ms, median of five exports. Brave 147, Apple M4 Pro, A4, identical fonts, RenderPDF.js 0.6.0, and jsPDF 4.2.1.
jsPDF was faster on two longer documents: it exported a 200-row directory in 0.68 s versus 1.21 s, and a 1,000-row product list in 1.64 s versus 1.74 s. It also made a smaller directory PDF; RenderPDF.js made a smaller product list PDF. The short and long documents came from separate measurements, so I do not combine them into one result.
Chart 2. Time in s, median of five exports. Brave 147, Apple M4 Pro, A4, matched fonts, RenderPDF.js 0.6.0, and jsPDF 4.2.1.
In a visual check, the directory rows from RenderPDF.js were closer to the browser: 2.18% of pixels differed, compared with 5.45% for jsPDF. For the product list, jsPDF had a slightly better median across scored rows, 10.68% versus 11.51%. One incomplete jsPDF row was excluded from that median: the beginning of a product name was missing at a page break. A percentage of differing pixels alone does not reveal missing text.
Chart 3. Median percentage of differing pixels in a row crop. Brave 147, Apple M4 Pro, matched fonts, PDF.js at 96 DPI; one export per library and document.
When to use RenderPDF.js
If you already have a document in HTML and want its PDF to look like the browser preview, I would try RenderPDF.js. It was faster on most short test documents and rendered the directory more faithfully. jsPDF exported both long documents faster and had slightly fewer visual differences in the product list.
Before choosing a library for your app, export a representative document with its real fonts. Check page breaks, selectable text, file size, and export time. For complex CSS, read the library's warnings too: they may point to a part of the design that the PDF could not preserve.
