Prompt
How do I generate PDFs from HTML in an API?
Latest observation
Oct 12, 2026
OpenAI APIWeb search: off
To generate PDFs from HTML in an API, the usual flow is:
- Accept HTML or template data in your API
- Render the HTML into a headless browser or HTML-to-PDF engine
- 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