fastify-xpdf
generateFromUrl
Navigate to a URL with Puppeteer and generate a PDF of the fully rendered page.
generateFromUrl
Navigate to a URL with Puppeteer's Chrome headless browser and capture the fully rendered page as a PDF. Waits for the network to settle before capturing (configurable via waitFor).
Signature
fastify.xPDF.generateFromUrl(
url: string,
options?: UrlPdfOptions,
): Promise<PdfResult>
interface UrlPdfOptions extends HtmlPdfOptions {
waitFor?: string // Puppeteer waitUntil value (default: "networkidle2")
timeout?: number // Navigation timeout in ms (default: 30000)
}
Params
| Name | Type | Required | Description |
|---|---|---|---|
url | string | Yes | Fully qualified URL to render — must be a valid URL |
options.waitFor | string | No | Puppeteer waitUntil strategy: "networkidle0", "networkidle2", "load", "domcontentloaded" (default: "networkidle2") |
options.timeout | number | No | Navigation timeout in milliseconds (default: 30000) |
options.format | string | No | Page format override |
options.landscape | boolean | No | Landscape orientation |
options.margin | object | No | Page margins override |
options.printBackground | boolean | No | Background colours/images |
options.saveToStorage | boolean | No | Upload result to xStorage |
options.folder | string | No | Storage folder override |
Returns
Same PdfResult as generateFromHtml: { buffer, filename, size, storageKey?, url? }.
Throws
| Error | When |
|---|---|
[xPDF] URL must be a non-empty string | url is empty, null, or not a string |
[xPDF] URL must be a valid URL | Malformed URL string |
[xPDF] Failed to initialize PDF browser | Puppeteer/Chrome launch failure |
Examples
Capture a public report page
fastify.post("/snapshots", async (request, reply) => {
const { pageUrl, filename } = request.body;
const { buffer } = await fastify.xPDF.generateFromUrl(pageUrl, {
waitFor: "networkidle0",
timeout: 60000,
format: "A4",
printBackground: true,
filename,
});
return reply
.header("Content-Type", "application/pdf")
.header("Content-Disposition", `attachment; filename="${filename}"`)
.send(buffer);
});
Archive a rendered dashboard page to storage
async function archiveDashboard(dashboardUrl) {
const { url: storageUrl, storageKey } = await fastify.xPDF.generateFromUrl(
dashboardUrl,
{
waitFor: "networkidle0",
timeout: 45000,
landscape: true,
format: "A3",
saveToStorage: true,
folder: `archives/${new Date().toISOString().slice(0, 10)}`,
},
);
return { storageUrl, storageKey };
}
See Also
- generateFromHtml — Use when you already have the HTML string
- generateFromMarkdown — Convert markdown content to PDF
AI Context
package: "@xenterprises/fastify-xpdf"
method: fastify.xPDF.generateFromUrl(url, options?)
use-when: Render a live URL to PDF using Puppeteer — navigates Chrome to the URL and captures the page
params: url (string), options (format, margin, printBackground, waitUntil, saveToStorage)
returns: { buffer, size } | { buffer, size, storageKey, url }
