Convert HTML to PDF in Node.js

Convert HTML to PDF in Node.js using the HTML2PDFAPI REST API. Works with any HTTP client including the native fetch API.

Quick Start

No SDK required. Use the native fetch API (Node.js 18+) or your preferred HTTP client:

// Using fetch (built-in Node.js 18+)
const response = await fetch('https://html2pdfapi.com/render', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    html: '<h1>Hello World</h1>',
    format: 'pdf'
  })
});

const pdfBuffer = await response.arrayBuffer();

Convert URL to PDF

// Convert URL to PDF
const response = await fetch('https://html2pdfapi.com/render', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    url: 'https://example.com',
    format: 'pdf',
    options: {
      format: 'A4',
      printBackground: true
    }
  })
});

Take Screenshots

// Take a screenshot
const response = await fetch('https://html2pdfapi.com/render', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_API_KEY',
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    url: 'https://example.com',
    format: 'png',
    options: {
      width: 1920,
      height: 1080,
      fullPage: false
    }
  })
});

Express.js Integration

// Express.js endpoint
import express from 'express';
const app = express();

app.post('/generate-pdf', async (req, res) => {
  const { html } = req.body;

  const response = await fetch('https://html2pdfapi.com/render', {
    method: 'POST',
    headers: {
      'Authorization': `Bearer ${process.env.HTML2PDFAPI_KEY}`,
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ html, format: 'pdf' })
  });

  const pdfBuffer = await response.arrayBuffer();

  res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', 'attachment; filename="document.pdf"');
  res.send(Buffer.from(pdfBuffer));
});

Available Options

OptionTypeDescription
formatstringOutput format: 'pdf', 'png', 'jpeg', 'webp'
htmlstringHTML content to convert
urlstringURL to convert
options.formatstringPage size: 'A4', 'Letter', etc.
options.landscapebooleanLandscape orientation
options.marginobjectPage margins {top, bottom, left, right}
options.printBackgroundbooleanInclude background colors/images

Error Handling

try {
  const response = await fetch('https://html2pdfapi.com/render', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer YOUR_API_KEY',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ html, format: 'pdf' })
  });

  if (!response.ok) {
    const error = await response.json();
    throw new Error(error.message);
  }

  const pdfBuffer = await response.arrayBuffer();
} catch (error) {
  console.error('PDF generation failed:', error.message);
}

Other Integrations

Python PHP Ruby Go cURL n8n

See Use Cases

No SDK required

Use native fetch or your preferred HTTP client. Works with Express, Fastify, Next.js, and more.