DEV Community

Dan E
Dan E

Posted on Originally published at rendex.dev

How to Convert URLs to PDF in Node.js

How to Convert URLs to PDF in Node.js

Converting a URL to PDF in Node.js usually means spinning up Puppeteer, managing a Chrome process, and writing error-handling code for when the browser runs out of memory. The Rendex API handles the browser work: pass a URL, get a hosted PDF back.

Prerequisites

  • Node.js 18 or later (or Bun)
  • A Rendex API key. Get one free (100 renders/month, no credit card)

Step 1: Install the SDK

npm install @copperline/rendex
Enter fullscreen mode Exit fullscreen mode

Step 2: Capture a URL as a PDF

import { Rendex } from "@copperline/rendex"
import { writeFileSync } from "fs"

const rendex = new Rendex(process.env.RENDEX_API_KEY!)

const result = await rendex.screenshot({
  url: "https://example.com/invoice/1001",
  format: "pdf",
  pdfFormat: "A4",
  pdfMargin: { top: "20mm", right: "15mm", bottom: "20mm", left: "15mm" },
  pdfPrintBackground: true,
})

// Hosted URL — share or store as-is
console.log(result.url)

// Raw bytes — write to disk or stream to a storage bucket
writeFileSync("invoice-1001.pdf", Buffer.from(result.base64, "base64"))
Enter fullscreen mode Exit fullscreen mode

Step 3: Page size and margins

The pdfFormat option controls the paper size. Common values: "A4", "Letter", "Legal", "A3", "Tabloid". Margins accept any CSS length unit: px, mm, cm, or in.

Step 4: Background colors and images

Browsers strip CSS backgrounds when printing by default. Set pdfPrintBackground: true to keep brand colors and background images.

Step 5: Batch PDF generation

const batch = await rendex.batch({
  urls: invoiceUrls.map((url) => ({ url })),
  defaults: {
    format: "pdf",
    pdfFormat: "A4",
    pdfMargin: { top: "20mm", right: "15mm", bottom: "20mm", left: "15mm" },
    pdfPrintBackground: true,
  },
  webhookUrl: "https://yourapp.com/webhooks/rendex",
})
// batch.batchId — poll GET /v1/batches/:batchId for per-job status
Enter fullscreen mode Exit fullscreen mode

Troubleshooting

Blank PDF. Set pdfPrintBackground: true if the page uses CSS backgrounds.

Missing images. The page must be publicly accessible. Inline private assets as base64 data URIs.

Content cut off. Remove fixed heights from the root element or use a taller pdfFormat like "A3".

Next steps

Get a free API key and start with 100 renders per month.

Top comments (0)