X Enterprises
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

NameTypeRequiredDescription
urlstringYesFully qualified URL to render — must be a valid URL
options.waitForstringNoPuppeteer waitUntil strategy: "networkidle0", "networkidle2", "load", "domcontentloaded" (default: "networkidle2")
options.timeoutnumberNoNavigation timeout in milliseconds (default: 30000)
options.formatstringNoPage format override
options.landscapebooleanNoLandscape orientation
options.marginobjectNoPage margins override
options.printBackgroundbooleanNoBackground colours/images
options.saveToStoragebooleanNoUpload result to xStorage
options.folderstringNoStorage folder override

Returns

Same PdfResult as generateFromHtml: { buffer, filename, size, storageKey?, url? }.

Throws

ErrorWhen
[xPDF] URL must be a non-empty stringurl is empty, null, or not a string
[xPDF] URL must be a valid URLMalformed URL string
[xPDF] Failed to initialize PDF browserPuppeteer/Chrome launch failure
xpdf: saveToStorage requires the xStorage plugin — register @xenterprises/fastify-xstorage first or pass saveToStorage: falsesaveToStorage: true passed but xStorage is not registered

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

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, must be a valid URL), options (waitFor, timeout, format, landscape, margin, printBackground, displayHeaderFooter, headerTemplate, footerTemplate, filename, saveToStorage, folder)
returns: { buffer, filename, size } | { buffer, filename, size, storageKey, url } (when saveToStorage: true)
throws: "[xPDF] URL must be a non-empty string | [xPDF] URL must be a valid URL | [xPDF] Failed to initialize PDF browser | xpdf: saveToStorage requires the xStorage plugin"
Copyright © 2026