Newsletter
Join the Community
Subscribe to our newsletter for the latest news and updates
A JavaScript library that converts HTML to PDFMake document definitions for PDF generation.
html-to-pdfmake is an open-source JavaScript library that converts HTML markup into PDFMake document definition objects, letting you generate styled PDFs through the PDFMake API.
html-to-pdfmake takes an HTML string as input and outputs a PDFMake-compatible content array that you pass to pdfMake.createPdf. It runs in both web browsers and Node.js environments; on Node, it requires jsdom to provide a window object. The project is maintained by Aymkdn and includes an online demo and an example.js script that generates example.pdf.
Developers building Node.js or browser applications that already use PDFMake and need to convert HTML into PDF documents without hand-writing document definitions. It suits teams generating invoices, reports, or certificates from simple HTML templates, and anyone who prefers writing HTML and CSS over PDFMake's raw object syntax.
Install the npm package, initialize jsdom in Node to obtain a window object, and include the CDN scripts for pdfmake and html-to-pdfmake in the browser. Call htmlToPdfmake(htmlString, options) to get the converted content, then pass it to pdfMake.createPdf. The library parses the HTML into a DOM, walks the nodes, and maps elements, styles, and attributes to PDFMake's document definition syntax.
The README names Doppio (an online conversion service for complex HTML), dropflow (converts HTML to canvas), snapdom (converts HTML to an image), and DOM-PDF (another HTML-to-PDFMake library). For rich content, these may produce better results.
Yes, but it requires jsdom. Create a JSDOM instance and pass its window object as the window option to htmlToPdfmake, then use pdfMake's getBuffer to write a PDF file.
No. The README explicitly warns that complex HTML, such as content produced by a Rich Text Editor, is not well supported. It recommends online solutions like Doppio, or converting the HTML to canvas or an image before exporting to PDF.
Base64-encoded data URIs work in both browser and Node. In browsers, you can also use image URLs via the imagesByReference option, which returns an images object for PDFMake. For URLs with custom headers, use the data-src JSON attribute.
When enabled, htmlToPdfmake returns an object with both content and images properties instead of a single content array. The images object maps generated reference keys to image URLs, ready for PDFMake's images option.
