Module Reference
Generate PDFs from HTML or URL in Cloud Code.
What This Adds In Cloud Code
- Server-side PDF rendering as base64 output.
- Supports HTML or URL input.
- Built-in URL/reference safety checks.
Quick Start
const PDF = require('pdf');
const pdfBase64 = PDF.render('<html><body><h1>Invoice</h1></body></html>');API
| Function | Params | Returns | Notes |
|---|---|---|---|
render(htmlOrUrl, options) | string + optional rendering options | string | Returns base64 PDF content. |
Practical Example
const PDF = require('pdf');
const Mail = require('mail');
const invoicePdf = PDF.render(`<html><body><h1>Invoice #${request.params.order_no}</h1></body></html>`);
Mail.send({
to: request.params.email,
subject: 'Your invoice',
text: 'Attached invoice.',
attachments: {
'invoice.pdf': invoicePdf
}
});Downloading as a File
In a route or extension, combine with res.send() to trigger a browser download:
const PDF = require('pdf');
const pdfBase64 = PDF.render('<html><body><h1>Report</h1></body></html>');
res.send(pdfBase64, {
filename: 'report.pdf',
type: 'application/pdf'
});Failure Modes & Gotchas
render throws an Error when it cannot produce a PDF. It never returns an
empty string. Wrap calls in try/catch:
const PDF = require('pdf');
try {
const pdfBase64 = PDF.render(html);
res.send(pdfBase64, { filename: 'invoice.pdf', type: 'application/pdf' });
} catch (error) {
console.error(error.message);
res.status(503).json({ error: 'PDF not available, try again later' });
}| Cause | error.message |
|---|---|
| Unsafe URL or embedded reference | invalid uri or invalid content |
| Renderer error or timeout | PDF rendering failed |
| Too many renders started this minute | PDF render throttled (… limit …/min); retry after Ns |
| Too many renders running across the platform | PDF renderer busy (…); retry after 5s |
| Render loop (a request rendering itself, recursively) | CloudCode self-request depth limit exceeded (max 3) |
- Per-minute limits. Renders that run while handling a web request (routes,
functions called over the API, and storefront PDF pages) are limited to 30
starts per minute per site and 10 per minute per client IP. Storefront PDF
responses answer these with
429and aRetry-Afterheader; in Cloud Code you get the error above and decide the response yourself. - Concurrency cap. The platform also limits how many web-request renders run
at the same time. When it is full, storefront PDF responses return
503withRetry-After; in Cloud Code,renderthrows the "busy" error. - Background jobs are exempt from both limits. Generate PDFs in bulk (batch invoices, exports) from a job, not from a route or function.
- URL sources. When you pass a site URL (or
header_html/footer_htmlURLs), all fetches for one render share a 45-second budget, each response is capped at 10 MB, and at most 3 redirects are followed. - Render loops are blocked. A route that renders its own URL as a PDF, which renders it again, stops at the Cloud Code self-request depth limit instead of spawning renderers indefinitely.
- Large or complex HTML increases render time.