Easy PDF Server

Easy PDF Server A Simple API to Convert HTML to PDF

How to Convert HTML to PDF in Node.js

By the Easy PDF Server Team

Converting HTML to PDF in Node.js is one of the most common tasks in server-side JavaScript development. Whether you need to generate invoices, reports, certificates, or receipts, having a reliable HTML to PDF conversion pipeline is essential. In this guide, we cover three approaches and explain why a dedicated PDF conversion API often outperforms local libraries.

Why Convert HTML to PDF?

HTML is the universal format for laying out content on the web. CSS gives you precise control over typography, colors, spacing, and page structure. Converting HTML to PDF lets you reuse your existing web design skills to produce professional documents without learning a new templating language. Common use cases include:

Approach 1: Using Puppeteer Locally

Puppeteer is a Node.js library that controls headless Chrome or Chromium. It can navigate to a URL or render an HTML string, then save the output as a PDF. Here is a minimal example:

const puppeteer = require('puppeteer'); async function htmlToPdf(html, outputPath) { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setContent(html, { waitUntil: 'networkidle0' }); await page.pdf({ path: outputPath, format: 'A4' }); await browser.close(); } htmlToPdf('<h1>Hello World</h1>', 'output.pdf');

While Puppeteer works well for development, it has significant drawbacks in production. Each conversion launches a headless browser instance, consuming hundreds of megabytes of memory. Under load, Puppeteer can exhaust server resources quickly. Setting up font rendering, handling concurrent requests, and managing browser process lifecycles adds operational complexity.

Approach 2: Using pdfkit or pdfmake

Libraries like pdfkit and pdfmake generate PDFs programmatically without a browser engine. They are lightweight but require you to build documents using their proprietary APIs rather than HTML and CSS. This means you cannot reuse your existing HTML templates and must learn a new document description language.

const PDFDocument = require('pdfkit'); const doc = new PDFDocument(); doc.fontSize(25).text('Hello World', 100, 100); doc.end();

For teams that already have HTML/CSS templates, rewriting everything in pdfkit's API is time-consuming and creates a maintenance burden as templates evolve.

Approach 3: Using the Easy PDF Server REST API

Easy PDF Server provides a simple REST API that converts HTML to PDF. You send an HTML string or a URL and receive a PDF in response. No browser management, no memory overhead, no Puppeteer configuration. Here is the complete Node.js code:

const https = require('https'), fs = require('fs'); const data = JSON.stringify({ key: 'YOUR-API-KEY', html: 'YOUR-HTML-STRING', }); const options = { hostname: 'api.easypdfserver.com', port: 443, path: '/make-pdf', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': data.length, }, }; const req = https.request(options, res => { let body = Buffer.alloc(0); res.on('data', chunk => body = Buffer.concat([body, chunk])); res.on('end', () => fs.writeFile('download.pdf', body, console.error)); }); req.on('error', console.error); req.write(data); req.end();

To convert a URL instead of an HTML string, replace the html field with a url field:

const data = JSON.stringify({ key: 'YOUR-API-KEY', url: 'https://example.com/invoice', });

Why Use a PDF API Instead of Local Libraries?

Pricing and Getting Started

Easy PDF Server costs $20 per month for the hosted API (or $120 per year, saving 42%). Each plan includes email support. To get started, subscribe here and we will send your API key immediately. If you prefer to host the software yourself, Easy PDF Server is also available as an AWS Marketplace AMI at $0.39 per hour.

Ready to start converting HTML to PDF in Node.js? Sign up today and get your API key in minutes.

Related Articles

See our full feature comparison between the hosted API and self-hosted options.