Engineering blogNode.jsPDF generation

Convert HTML to PDF Using Node.js – Step-by-Step Guide

Learn how to turn any HTML page into a polished PDF using Puppeteer and Node.js. This guide walks through setup, a complete script, customization options, and common pitfalls to avoid in production.

ER

Evan Rivers

Founder, HTML2PDFConverter

Updated · Dec 20256 min read

Tutorial overview

We’ll start with the basics, then build up to a flexible HTML → PDF pipeline you can drop into any Node.js service.

Converting HTML to PDF is a common task for developers — whether you’re generating invoices, reports, or saving web pages for offline use. In this guide, we’ll show you how to convert HTML to PDF using Node.js, step by step, with a practical example that works out of the box.

Why Convert HTML to PDF with Node.js?

Node.js is a powerful JavaScript runtime that allows you to automate server-side tasks. Converting HTML to PDF with Node.js offers several benefits:

  • Automation: Generate PDFs on the server without manual steps.
  • Customizability: Control page size, margins, and styling.
  • Scalability: Integrate into APIs or backend services.

Prerequisites

Before you start, make sure you have:

  • Node.js installed (v14+ recommended)
  • Basic knowledge of JavaScript
  • A sample HTML page or URL to convert
  • Optional: VS Code or your preferred Node.js editor

Step 1: Install Puppeteer

Puppeteer is a popular Node.js library that controls headless Chrome. It’s perfect for converting HTML pages to PDF.

npm install puppeteer

Step 2: Create a Simple Conversion Script

Create a file called convert.js in your project folder:

const puppeteer = require('puppeteer');

(async () => {
  try {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://example.com', { waitUntil: 'networkidle2' });
    await page.pdf({
      path: 'output.pdf',
      format: 'A4',
      printBackground: true
    });
    console.log('PDF successfully created!');
    await browser.close();
  } catch (error) {
    console.error('Error generating PDF:', error);
  }
})();

Step 3: Run Your Script

Run the script in your terminal:

node convert.js

Check your project folder — you should now see output.pdf with your HTML content converted perfectly!

Step 4: Customize Your PDF

You can tweak margins, orientation, and background rendering to better match your brand or layout needs:

await page.pdf({
  path: 'custom.pdf',
  format: 'A4',
  margin: { top: '20px', right: '20px', bottom: '20px', left: '20px' },
  printBackground: true,
  landscape: false
});
  • Margins: Control spacing around your content.
  • Landscape: Switch between portrait and landscape.
  • Backgrounds: Include CSS backgrounds.

Step 5: Handling Dynamic Content

If your HTML page has dynamic content (like charts or JavaScript-generated elements), wait for it to load fully:

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

Common Errors & Fixes

Here are a few issues you might run into while generating PDFs in Node.js — and how to fix them quickly:

ErrorFix
Chromium download failsEnsure you have enough disk space and internet connectivity, or configure Puppeteer to use an existing Chrome/Chromium installation.
Permission deniedRun the script with proper permissions or adjust folder paths so Node.js can write the PDF file.
PDF looks emptyUse waitUntil: 'networkidle0' to ensure the page and all dynamic content load fully before generating the PDF.

Conclusion

You now know how to convert HTML to PDF using Node.js. With this method, you can automate PDF generation for reports, invoices, or saved web pages directly from your backend services.

If you’d rather skip the setup and infrastructure, try our no-code alternative: HTML2PDFConverter. Convert any webpage or HTML file to PDF instantly — no installation or maintenance required.