Node.js HTML to PDF API guide

Use native fetch in Node.js to create PDFs from API routes, workers, queues, or command line scripts.

EndpointPOST https://api.html2pdf.app/v1/generate
AuthenticationX-API-Key: <your-api-key>

Overview

Send JSON from Node.js, read the PDF as an ArrayBuffer, and persist it using the file system APIs.

The API accepts a JSON request body. Send either a public URL or inline HTML in the required html parameter. For normal synchronous conversions, the response body is the generated PDF and should be saved as binary data.

Requirements

  • Node.js 18 or newer
  • An html2pdf.app API key in an environment variable
  • A server-side runtime, job worker, or trusted script

Quick start

Replace <your-api-key> or the environment variable with your html2pdf.app API key, then run the example from a trusted backend environment.

Use this script from a backend job or local command line task to create document.pdf.

import { writeFile } from 'node:fs/promises';

const response = await fetch('https://api.html2pdf.app/v1/generate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-Key': process.env.HTML2PDF_API_KEY,
  },
  body: JSON.stringify({
    html: 'https://www.example.com',
  }),
});

if (!response.ok) {
  throw new Error('PDF generation failed with status ' + response.status);
}

const pdf = Buffer.from(await response.arrayBuffer());
await writeFile('document.pdf', pdf);

Node.js examples

These examples cover the next common production flows: generating from inline HTML with options and queuing a callback job.

1. Use page options

Pass PDF layout options in the JSON body when you need margins, page format, media mode, or a filename.

import { writeFile } from 'node:fs/promises';

const payload = {
  html: '<h1>Quarterly report</h1><p>Generated from Node.js.</p>',
  format: 'A4',
  landscape: false,
  media: 'print',
  marginTop: 48,
  marginRight: 32,
  marginBottom: 48,
  marginLeft: 32,
  filename: 'quarterly-report.pdf',
};

const response = await fetch('https://api.html2pdf.app/v1/generate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-Key': process.env.HTML2PDF_API_KEY,
  },
  body: JSON.stringify(payload),
});

if (!response.ok) {
  const errorBody = await response.text();
  throw new Error(errorBody || 'PDF generation failed');
}

await writeFile('quarterly-report.pdf', Buffer.from(await response.arrayBuffer()));

2. Queue a PDF job

Submit a job and let your webhook receive the base64 PDF document when conversion completes.

const response = await fetch('https://api.html2pdf.app/v1/generate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-API-Key': process.env.HTML2PDF_API_KEY,
  },
  body: JSON.stringify({
    html: 'https://www.example.com/report',
    callBackUrl: 'https://your-app.com/webhooks/html2pdf',
    state: 'report-1042',
  }),
});

if (!response.ok) {
  throw new Error('Unable to queue PDF generation');
}

Request options

Most integrations start with html and then add options as the document design becomes more specific.

  • html is required and accepts a public URL or raw HTML.
  • format, landscape, width, and height control page size.
  • marginTop, marginRight, marginBottom, and marginLeft control whitespace around the rendered page.
  • media selects screen or print CSS rendering.
  • callBackUrl switches the request to asynchronous generation and sends the completed PDF to your webhook.

See the full API parameter reference for every supported field.

Troubleshooting

  • Keep the API key on the server, never in bundled frontend JavaScript.
  • Use response.arrayBuffer because successful synchronous responses are binary PDFs.
  • Use a queue or callback URL for long-running report generation.

If the generated document is blank or missing styles, first confirm that the source URL is public and that required CSS, fonts, and images are reachable by the rendering service.