As of 2024, there are several alternatives to wkhtmltopdf that you can consider for rendering PDFs from HTML in your web projects. Here are a few options that are popular in the web development community:
-
Puppeteer: Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer is capable of rendering and printing PDFs from HTML.
Example usage with Puppeteer:
const puppeteer = require('puppeteer'); (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com', {waitUntil: 'networkidle2'}); await page.pdf({path: 'example.pdf', format: 'A4'}); await browser.close(); })(); -
Chrome Headless: Modern versions of Chrome and Chromium support headless mode which can be used to generate PDFs from the command line or programmatically.
Example command-line usage:
chrome --headless --disable-gpu --print-to-pdf=path/to/output.pdf https://example.com -
WeasyPrint: WeasyPrint is a visual rendering engine for HTML and CSS that can output to PDF. It is designed to support web standards for printing.
Example usage with WeasyPrint:
from weasyprint import HTML HTML('https://example.com').write_pdf('example.pdf') -
mPDF: mPDF is a PHP library which generates PDF files from UTF-8 encoded HTML. It is based on FPDF and HTML2FPDF, with a number of enhancements.
Example usage with mPDF:
require_once __DIR__ . '/vendor/autoload.php'; $mpdf = new \Mpdf\Mpdf(); $mpdf->WriteHTML('<h1>Hello world!</h1>'); $mpdf->Output(); -
Gotenberg: Gotenberg is a Docker-powered stateless API for converting HTML, Markdown and Office documents to PDF.
Example usage with Gotenberg:
curl --request POST \ --url http://localhost:3000/forms/chromium/convert/html \ --header 'Content-Type: multipart/form-data' \ --form [email protected] \ --form 'paperWidth=8.27' \ --form 'paperHeight=11.7' \ --output result.pdf -
jsPDF: jsPDF is a client-side library that can generate PDF documents using JavaScript. It's useful for client-side PDF generation.
Example usage with jsPDF:
var doc = new jsPDF(); doc.text('Hello world!', 10, 10); doc.save('a4.pdf');
When choosing an alternative, consider factors such as performance, ease of use, compatibility with your current tech stack, and the complexity of the PDFs you need to generate. Puppeteer and Chrome Headless are particularly powerful when you need to handle complex layouts or when you want to leverage the full capabilities of modern web browsers for PDF generation.