Puppeteer is a powerful way to generate PDFs from HTML using headless Chromium. It gives developers direct control over a real browser and can render modern HTML, CSS and JavaScript accurately.

But if PDF generation is only a small part of your application, running Chromium yourself can introduce a surprising amount of infrastructure and maintenance.

The HTML2PDF API provides an alternative: send HTML2PDF a URL or your raw HTML and receive the generated PDF directly in the HTTP response. HTML2PDF handles the browser-based rendering infrastructure, so your application does not need to install, run or maintain its own Chromium instances just to generate PDFs.

In this guide we will look at:

  • Why developers use Puppeteer for HTML-to-PDF conversion
  • What is involved in running Puppeteer and Chromium in production
  • How the HTML2PDF API provides an alternative architecture
  • Converting URLs and raw HTML to PDF with HTML2PDF
  • When Puppeteer is the better solution
  • When using HTML2PDF is simpler

Why use Puppeteer for HTML to PDF?

Puppeteer controls Chromium programmatically from Node.js. A basic PDF conversion can be as simple as:

const puppeteer = require('puppeteer');

const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.goto('https://example.com', {
    waitUntil: 'networkidle0'
});

await page.pdf({
    path: 'example.pdf',
    format: 'A4',
    printBackground: true
});

await browser.close();

This is an excellent solution when you need complete control over the browser. You can interact with pages, manipulate the DOM, execute JavaScript and determine exactly when and how the PDF should be generated.

The difficulty is that the code above is only part of what is required when PDF generation becomes a production service. Your application also needs to run Chromium reliably.


Running Puppeteer and Chromium in production

For occasional local PDF generation, Puppeteer can be very straightforward. In production, however, you may also need to manage:

  • Installing and updating Chromium
  • Browser and Puppeteer version compatibility
  • Memory consumption
  • Concurrent Chromium processes
  • Process crashes and orphaned browser instances
  • Timeouts and pages that never finish loading
  • Container or server configuration
  • Fonts and other operating-system dependencies
  • Queueing during traffic spikes
  • Security when rendering external URLs
  • Monitoring and restarting failed processes

None of these issues make Puppeteer a bad solution. They simply mean that by using Puppeteer directly, you are also taking responsibility for operating the browser-rendering infrastructure.

If browser automation is an important part of your application, that may be exactly what you want. If your application simply needs a PDF, it may be unnecessary infrastructure.


Use the HTML2PDF API instead of running Chromium yourself

HTML2PDF moves the HTML-to-PDF rendering outside your application. Instead of starting and managing Chromium yourself, your application sends a request to the HTML2PDF API.

The basic architecture becomes:

Your application
       |
       |  URL or HTML
       v
  HTML2PDF API
       |
       |  PDF
       v
Your application

instead of:

Your application
       |
       v
   Puppeteer
       |
       v
Headless Chromium
       |
       v
      PDF

HTML2PDF takes care of the rendering environment. Your application only needs to make an HTTP request and handle the returned PDF.

This can be particularly useful when PDF generation is a supporting feature of your application rather than something you want to build and maintain infrastructure around.


1. Convert a URL to PDF with HTML2PDF

To replace a simple Puppeteer page.goto() followed by page.pdf(), send the URL directly to the HTML2PDF API.

curl "https://api.html2pdf.co.uk/?license=YOUR_LICENSE_KEY&url=https%3A%2F%2Fexample.com" \
     --output example.pdf

HTML2PDF retrieves the page, renders it and returns the resulting PDF in the HTTP response.

Your application therefore does not have to start a browser process for each conversion.


2. Convert raw HTML to PDF with HTML2PDF

Your HTML does not have to exist on a publicly accessible URL. If your application generates an invoice, report or other document internally, you can send the HTML directly to HTML2PDF using a POST request.

curl -X POST "https://api.html2pdf.co.uk/" \
     -d "license=YOUR_LICENSE_KEY" \
     --data-urlencode "html=<html><body><h1>My report</h1><p>Report content...</p></body></html>" \
     --output report.pdf

This approach is useful for dynamically generated documents such as:

  • Invoices
  • Financial reports
  • Account statements
  • Certificates
  • Order confirmations
  • Tickets
  • Analytics reports
  • Customer reports

Your application can generate the HTML using the templates and CSS technologies you already use for web pages and send the finished HTML directly to HTML2PDF.


3. Control the resulting PDF

Using the HTML2PDF API instead of Puppeteer does not mean that every PDF has to use the same layout. HTML2PDF provides conversion options including:

  • Page size such as A4 or Letter
  • Portrait or landscape orientation
  • Margins
  • Headers and footers
  • Screen or print CSS
  • Options for JavaScript-heavy pages
  • Lazy-loaded content
  • Rendering delays
  • Watermarks

For example:

https://api.html2pdf.co.uk/
    ?license=YOUR_LICENSE_KEY
    &url=https://example.com
    &page_size=A4
    &orientation=portrait
    &css_media_type=print

For a full list of available options, see the HTML2PDF API documentation.

Tip: Set your default layout options (page size, margins, header/footer, etc.) once in the HTML2PDF members area. Your application can then keep its API requests simple and override individual options only when needed.

Puppeteer vs HTML2PDF

Puppeteer and HTML2PDF solve the HTML-to-PDF problem in different ways. Puppeteer gives your application direct control over Chromium. HTML2PDF provides PDF generation as a managed API.

Puppeteer HTML2PDF API
Browser infrastructureYou manage itManaged by HTML2PDF
Local Chromium installationRequiredNot required
Browser updatesYou manage themManaged by HTML2PDF
Memory/process managementYour responsibilityManaged by HTML2PDF
Full browser controlYesNo
Custom browser automationYesNo
Simple HTTP integrationNoYes
URL to PDFYesYes
Raw HTML to PDFYesYes
Modern HTML/CSS renderingYesYes

Choose Puppeteer when

Puppeteer is probably the better choice if you need to:

  • Automate complex browser interactions
  • Click buttons or complete multi-step browser workflows
  • Manipulate the DOM extensively before generating the PDF
  • Run other browser automation alongside PDF generation
  • Control the Chromium instance directly
  • Keep the complete rendering infrastructure under your own control

Choose HTML2PDF when

The HTML2PDF API can be simpler if:

  • You primarily need the final PDF
  • PDF generation is a supporting feature rather than your core product
  • You don't want to install or maintain Chromium
  • You don't want browser processes consuming resources on your application servers
  • You want to integrate PDF generation using a normal HTTP request
  • You need to generate PDFs from different programming languages
  • You prefer the rendering infrastructure to be managed separately from your application

Migrating PDF generation from Puppeteer to HTML2PDF

If you currently use Puppeteer primarily for PDF generation, replacing it with HTML2PDF can require relatively little application code.

A basic Puppeteer implementation might look like this:

const browser = await puppeteer.launch();
const page = await browser.newPage();

await page.goto(url);

await page.pdf({
    path: 'output.pdf',
    format: 'A4'
});

await browser.close();

With HTML2PDF, the browser-management code is replaced by an HTTP request:

const url =
    'https://api.html2pdf.co.uk/?license=YOUR_LICENSE_KEY' +
    '&url=' + encodeURIComponent('https://example.com');

const response = await fetch(url);

if (!response.ok) {
    throw new Error(`HTML2PDF API error: ${response.status}`);
}

const pdf = Buffer.from(await response.arrayBuffer());

You can save the resulting buffer, return it directly to the user, attach it to an email or upload it to your storage system.

The important architectural difference is that your application no longer needs to start and manage a Chromium process to perform the conversion.


Is HTML2PDF cheaper than running Puppeteer?

Not necessarily. Puppeteer itself is open source. If you already have suitable infrastructure and efficiently generate large numbers of PDFs, running your own Chromium instances can be very cost-effective.

HTML2PDF adds a subscription cost, but the service also handles the rendering infrastructure for you.

The relevant comparison is therefore not simply:

free Puppeteer vs paid HTML2PDF

but rather:

running and maintaining your own browser-based PDF rendering infrastructure vs using the managed HTML2PDF API.

When developer time, server management and operational complexity matter, using HTML2PDF can be the simpler solution. At very high volumes, particularly when you already have the expertise and infrastructure available, running Puppeteer yourself may make more sense.


Generate PDFs without running Chromium yourself

If you need high-quality browser-rendered PDFs but do not need to control Chromium itself, HTML2PDF provides a much simpler architecture.

With the HTML2PDF API you can:

  1. Send HTML2PDF a URL or raw HTML
  2. Specify the PDF conversion options you need
  3. Receive the generated PDF directly in the HTTP response

There is no Chromium installation or Puppeteer process to manage in your application.

Explore the full HTML2PDF API documentation or compare the available plans and pricing to start testing HTML2PDF with your own pages and HTML.