X Enterprises
fastify-xpdf

generateFromMarkdown

Convert a Markdown string to PDF — parsed with marked and rendered via Puppeteer.

generateFromMarkdown

Convert a Markdown string to PDF. The markdown is parsed with marked and wrapped in a styled HTML template, then rendered through Puppeteer. Accepts all the same options as generateFromHtml.

Signature

fastify.xPDF.generateFromMarkdown(
  markdown: string,
  options?: HtmlPdfOptions,
): Promise<PdfResult>

// HtmlPdfOptions and PdfResult are identical to generateFromHtml

Params

NameTypeRequiredDescription
markdownstringYesMarkdown content — must be a non-empty string
optionsobjectNoSame options as generateFromHtml (format, landscape, margin, etc.)

Returns

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

Throws

ErrorWhen
[xPDF] Markdown content must be a non-empty stringmarkdown is empty, null, or not a string
[xPDF] Failed to initialize PDF browserPuppeteer/Chrome launch failure

Examples

Convert a README or report to PDF

import { readFile } from "fs/promises";

fastify.get("/docs/:slug/pdf", async (request, reply) => {
  const markdown = await readFile(`./docs/${request.params.slug}.md`, "utf8");

  const { buffer } = await fastify.xPDF.generateFromMarkdown(markdown, {
    format: "A4",
    margin: { top: "2cm", right: "2cm", bottom: "2cm", left: "2cm" },
  });

  return reply
    .header("Content-Type", "application/pdf")
    .header("Content-Disposition", `attachment; filename="${request.params.slug}.pdf"`)
    .send(buffer);
});

Generate a meeting notes PDF and save to storage

fastify.post("/meetings/:id/export", async (request, reply) => {
  const meeting = await fastify.prisma.meeting.findUnique({
    where: { id: request.params.id },
  });

  const markdown = [
    `# ${meeting.title}`,
    `**Date:** ${meeting.date}`,
    `**Attendees:** ${meeting.attendees.join(", ")}`,
    "",
    "## Notes",
    meeting.notes,
    "",
    "## Action Items",
    meeting.actions.map((a) => `- [ ] ${a}`).join("\n"),
  ].join("\n");

  const { url, filename } = await fastify.xPDF.generateFromMarkdown(markdown, {
    saveToStorage: true,
    folder: "meetings",
    filename: `meeting-${meeting.id}.pdf`,
  });

  return { url, filename };
});

See Also

AI Context

package: "@xenterprises/fastify-xpdf"
method: fastify.xPDF.generateFromMarkdown(markdown, options?)
use-when: Convert a Markdown string to PDF — internally converts to HTML then renders with Puppeteer
params: markdown (string), options (format, margin, printBackground, saveToStorage)
returns: { buffer, size } | { buffer, size, storageKey, url }
Copyright © 2026