Prompt

How do I generate PDFs from HTML in an API?

Technology / Developer Tools1 observationLast seen Oct 12, 2026

Latest observation

Oct 12, 2026OpenAI APIWeb search: off

To generate PDFs from HTML in an API, the usual flow is:

  1. Accept HTML or template data in your API
  2. Render the HTML into a headless browser or HTML-to-PDF engine
  3. Return the PDF file as a response or store it somewhere

Common approaches

1) Headless browser: Playwright / Puppeteer

Best when your HTML uses modern CSS, JavaScript, web fonts, charts, etc.

Typical flow

  • Your API receives HTML
  • A headless Chromium instance opens that HTML
  • It prints the page to PDF
  • API returns the PDF bytes

Example with Node.js + Puppeteer

import express from "express";
import puppeteer from "puppeteer";

const app = express();
app.use(express.json({ limit: "10mb" }));

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

  const browser = await puppeteer.launch({
    args: ["--no-sandbox", "--disable-setuid-sandbox"]
  });

  const page = await browser.newPage();
  await page.setContent(html, { waitUntil: "networkidle0" });

  const pdf = await page.pdf({
    format: "A4",
    printBackground: true
  });

  await browser.close();

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

app.listen(3000);

2) wkhtmltopdf

A command-line tool based on WebKit. Good for simpler, mostly static HTML.

Pros

  • Mature and widely used
  • Simple to integrate

Cons

  • Older CSS support
  • Less accurate with modern layouts

3) Server-side libraries

Examples:

  • Python: WeasyPrint, xhtml2pdf
  • Java: OpenHTMLtoPDF, Flying Saucer
  • .NET: QuestPDF, iText, PdfSharp-based tools

These are often better if you want a fully library-based solution instead of spinning up a browser.


Recommended API design

Request

Send either:

  • raw HTML
  • a template ID plus data
  • or markdown/JSON that you render into HTML first

Example:

{
  "html": "<html><body><h1>Hello</h1></body></html>"
}

Response

Return:

  • application/pdf
  • optionally as inline display or attachment

Headers:

Content-Type: application/pdf
Content-Disposition: attachment; filename="file.pdf"

Important things to handle

Wait for assets

If your HTML loads images, CSS, or fonts from URLs, make sure they’re fully loaded before generating the PDF.

Use absolute URLs

Relative paths often fail unless you set a base URL.

Security

If users can submit HTML:

  • sanitize input to avoid script injection
  • restrict file/network access
  • don’t allow arbitrary local file reads
  • run the renderer in a sandbox/container if possible

Performance

PDF generation can be expensive:

  • reuse browser instances instead of launching one per request
  • queue heavy jobs
  • cache repeated outputs if possible

When to choose what

  • Need pixel-perfect rendering / modern CSS / JS → Playwright or Puppeteer
  • Need lightweight static HTML conversion → wkhtmltopdf
  • Need pure library in backend language → WeasyPrint / QuestPDF / OpenHTMLtoPDF

If you want, I can show you:

  • a Node.js, Python, or .NET example
  • how to deploy it in Docker
  • or how to accept HTML and return the PDF directly from an API

Observation history

How did Obsurfable measure this prompt?

Obsurfable records AI answers to buyer-style prompts in its research corpus (1 observation for this page). Metrics are distributions over observations, not a single static ranking.

Which AI systems does Obsurfable collect answers from?

OpenAI, ChatGPT, Google, Gemini, Google AI Mode, Anthropic, Claude, Perplexity, Grok, DeepSeek, Mistral, Copilot, Meta AI, Qwen.

How can brands improve their AEO?

Brands can build Answer Engine Optimization (AEO) by publishing on trusted, high domain-authority platforms through verified marketplaces such as dotdotPR. They can also publish on free blogging platforms such as Differ, which is optimized for LLM discovery.

Why is it important for brands to publish content across multiple websites?

Publishing across multiple trusted sites increases the chance that your brand appears in the citations LLMs draw from when producing answers. Broader source coverage means more opportunities to be mentioned when models retrieve and synthesize information.

Want this interpreted for your brand?

Explorer is the free public corpus. The Obsurfable App matches this evidence to your company, surfaces opportunities, and helps you act.