Docs / HTML to PDF

Rendering

HTML to PDF

Send HTML in the request and get a PDF back. No template needed.

curl https://devilpdf.com/v1/render \
  -H "Authorization: Bearer $DEVILPDF_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "html": "<h1>Hello</h1><p>Rendered by DevilPDF.</p>",
    "css": "h1 { font: 700 28pt Georgia, serif; }",
    "options": { "page_size": "Letter", "margin": "20mm" }
  }'

#What you can send

  • A fragment. Body content like the example above. We wrap it in a document and apply the page size and margins from options.
  • A complete document. If your HTML contains an <html> tag, it is rendered exactly as written. Page size and margins from options still apply, unless your own CSS has an @page rule, which wins.

Your HTML renders in a current version of Chrome, so flexbox, grid, web fonts, SVG and @page rules all work.

#Merge data into HTML

When you send data, your HTML is treated as an inline template. The full template language is available.

{
  "html": "<h1>Hello {{ name }}</h1><ul><li data-each=\"item in items\">{{ item }}</li></ul>",
  "data": { "name": "Dana", "items": ["One", "Two", "Three"] }
}

Without data, the HTML is passed through untouched, so literal {{ braces }} in your markup are safe.

#Use the print kit

Raw HTML starts from a blank page. Set options.kit to true to get the same stylesheet the templates use: embedded fonts, tables that repeat their header and never split a row, totals, callouts and the rest of the print kit.

{
  "html": "<h1 class=\"doc-title\">Statement</h1><table class=\"items\"><thead><tr><th>Item</th><th class=\"num\">Amount</th></tr></thead><tbody><tr data-each=\"row in rows\"><td>{{ row.name }}</td><td class=\"num\">{{ row.amount | money }}</td></tr></tbody></table>",
  "data": { "rows": [{ "name": "Hosting", "amount": 49 }, { "name": "Support", "amount": 120 }] },
  "options": { "kit": true, "accent": "#e11d48", "page_numbers": true }
}

#Images, fonts and stylesheets

Your HTML can load images, fonts, stylesheets and media over the network, and can use data: URIs. Everything else is blocked: scripts, XHR and fetch, iframes. Requests to private or local addresses are always blocked.

Slow assets slow the render. If an image takes longer than the 30 second limit, the render fails with render_timeout.

#JavaScript

Scripts do not run by default. To render a page that builds itself with JavaScript, such as a chart, set options.javascript to true. Use wait_for or delay so capture happens after your script has finished.

{
  "html": "<div id=\"chart\"></div><script src=\"https://cdn.example.com/chart.js\"></script><script>drawChart().then(() => document.body.classList.add('ready'))</script>",
  "options": { "javascript": true, "wait_for": "body.ready" }
}

#Headers, footers and page numbers

options.header_text, options.footer_text and options.page_numbers work for raw HTML too. For anything more elaborate, write your own @page margin boxes in CSS.

@page {
  size: A4;
  margin: 20mm 16mm;
  @bottom-center { content: "Page " counter(page) " of " counter(pages); font-size: 8pt; }
}

#Page breaks

Standard CSS controls pagination.

.chapter { break-before: page; }
.card, tr { break-inside: avoid; }
h2 { break-after: avoid; }
thead { display: table-header-group; }

With the kit enabled, use the classes page-break and keep instead.

#Limits

The html field is capped by plan: 2 MB on Free, 5 MB on Starter, 10 MB on Growth and up. The whole request body can be at most 12 MB. See limits.