Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Robstar's avatar
Level 50

Alternatives to wkhtmltopdf

I've been using wkhtmltopdf (via https://github.com/barryvdh/laravel-dompdf) for years now. It's very fast and I can throw pretty much any HTML at it without issue.

The project has now officially been abandoned and I feel I should be looking for an alternative.

I've recently been rendering simple invoices in another project using DomPDF (via https://github.com/barryvdh/laravel-dompdf) and it all appears to be working fine but is definitely slower.

So, what are people using to render PDFs from HTML in 2024?

0 likes
6 replies
LaryAI's avatar
Level 58

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:

  1. 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();
    })();
    
  2. 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
    
  3. 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')
    
  4. 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();
    
  5. 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
    
  6. 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.

krekas's avatar

Dompdf works fine but has very limited styling capabilities. Until now best option was spatie/browsershot but now they released a package specific for generating pdf spatie/laravel-pdf which has some nice features

Robstar's avatar
Level 50

@krekas Yer I did see that mentioned, but I suspect it's infinitely slower than DomPDf and wkhtmltopdf.

Robstar's avatar
Level 50

Although saying that as it's rendering the provided HTML in a CHrome instance I should be able to throw any HTML at it.

spopic's avatar

Hey there @robstar, it's been some time already and I am also facing the same problem... I have my project running on php8.2 and laravel 10 where I barley made everything work together with wkhtml2pdf... In the next php and laravel upgrade I am expecting to have a problem and I am already looking for a replacement... If you have any useful findings and tips, I would be grateful if you would share with us.

Thanks!

Robstar's avatar
Level 50

@spopic To date I've had an experiment migrating some of the more complicated PDFs away from WKHTMLTOPDF using DomPDF. That's a semi decent alternative that requires no dependencies on the server but does have a lot of caveat, mainly centred around the fact that it only renders up to CSS 2.1. So any PDFs using flex won't work (i.e. Bootstrap 4 plus and Tailwind CSS).

At the moment my main concern is the fact that WKHTMLTOPDF has been abandoned and I believe it's random people that are compiling binaries to work on the latest versions of Ubuntu. For example, I managed to install one of these on the latest Ubuntu 22 LTS.

DomPDF is a good alternative but would 100% require I go through around 30 different PDF layouts, line by line, to ensure they render correctly. I really like WKHTMLTOPDF as you can just throw anything at it and it'll render without issue.

I think my long term solution is https://github.com/spatie/laravel-pdf. That requires a headless version of Chrome running but would allow me to render any Bootstrap / TailwindCSS HTML I can throw at it. I'm tempted to make a small Node service that renders HTML I post it and returns my PDF. I'd probably host this on it's own server.

Again, WKHTMLTOPDF is fine for the moment, but it's one of those things that is always at the back of mind, as I'm aware I'll have to migrate at some point.

I'm open to alternatives through!

Please or to participate in this conversation.