PDFx Documentation

repository·main·Indexed 22 days ago

https://github.com/akii09/pdfx

A professional React PDF component library built on @react-pdf/renderer. PDFx uses a 'copy-and-own' model via a CLI (pdfx-cli) to scaffold pre-built components and templates (blocks) directly into a user's codebase, eliminating runtime dependencies. It includes a wide range of components such as tables, forms, and QR codes, supports server-side PDF generation with Node.js, and provides Model Context Protocol (MCP) configuration for AI agents.

Tokens
37K
Snippets
131
Records
204
Agent score
77%

What's inside pdfx

  1. Overview of PDFx

    main
    PDFx provides pre-built PDF components for React applications. Unlike traditional component libraries, PDFx uses a 'copy-and-own' model: you use a CLI to add component source code directly into your project. This ensures there is no runtime dependency on PDFx once the components are added. The library is built on top of @react-pdf/renderer.
  2. How the PDFx theme system works

    main

    The theme system is designed to be local and transparent.

    1. Initialization: Running pdfx theme init creates a pdfx-theme.ts file in your local project.
    2. Runtime: Components consume the theme at runtime using the usePdfxTheme() hook.
    3. Implementation: The theme is a standard TypeScript object. There is no hidden context provider; the theme lives with your code, allowing for full control and visibility.
    pdfx theme init
  3. How PDFx component distribution works

    main

    PDFx uses a registry-based model similar to shadcn/ui. Instead of installing a monolithic npm package, you use the pdfx CLI to fetch component source code directly from the documentation website's registry.

    When you run pdfx add <name>, the CLI fetches a JSON manifest from the HTTPS registry, which contains the source code for the component. The CLI then writes these files directly into your project. This means you own the code once it is added, and there are no runtime dependencies on a @pdfx/components package.

    pdfx add <name>
  4. Generate PDFs on the server with Node.js

    main

    PDFx components are compatible with Node.js environments. You can use @react-pdf/renderer methods like renderToFile, renderToBuffer, or renderToStream to generate and save PDFs on the server (e.g., in Express or Next.js API routes).

    import { renderToFile } from '@react-pdf/renderer';
    import { Document, Page } from '@react-pdf/renderer';
    import { Heading } from './src/components/pdfx/heading/pdfx-heading';
    import { Text } from './src/components/pdfx/text/pdfx-text';
    
    const doc = (
      <Document>
        <Page size="A4" style={{ padding: 40 }}>
          <Heading level={1}>Monthly Report</Heading>
          <Text>Generated server-side with PDFx.</Text>
        </Page>
      </Document>
    );
    
    // Write straight to disk:
    await renderToFile(doc, './output.pdf');
    
    // …or get a Buffer to return from an API route / attach to an email:
    // const buffer = await renderToBuffer(doc);
  5. Configure MCP for AI Agents

    main

    PDFx supports the Model Context Protocol (MCP), allowing AI IDEs or agents to access the PDFx registry for context about component structures. You can initialize this for specific clients like Cursor.

    npx pdfx-cli@latest mcp init --client cursor
  6. How to update a component

    main

    Because components are written directly into your project rather than being managed via package.json versions, you do not update them by bumping a package version. To update a component to the latest version from the registry, run the pdfx add command again using the --force flag.

    pdfx add <name> --force
  7. Add PDFx components to your project

    main

    You can add specific, individual PDFx components directly into your local codebase. This allows you to fully own and customize the component code within your project. Use the add command followed by the component name.

    npx pdfx-cli@latest add badge
    npx pdfx-cli@latest add table form qrcode
  8. Initialize and add PDFx components via CLI

    main

    PDFx provides a CLI to scaffold pre-built PDF components into your React project. This approach allows you to own the component code completely with no runtime dependency on the PDFx package.

    1. Initialize PDFx: Run npx pdfx-cli init to set up the necessary structure.
    2. Add Components: Use npx pdfx-cli add <component-names> to copy specific components into your local ./components/pdfx directory. For example, to add heading, text, and badge, use:
      npx pdfx-cli add heading text badge
    npx pdfx-cli init
    npx pdfx-cli add heading text badge
  9. Add pre-composed PDFx blocks

    main

    Instead of individual components, you can add full, pre-designed templates (blocks) like invoices or reports using the block add command.

    npx pdfx-cli@latest block add invoice-modern
    npx pdfx-cli@latest block add report-financial
  10. Update PDF components to fix layout issues

    main

    In version 0.6.0, fixes were introduced for DataTable, Table, and PdfList components regarding layout and width handling.

    • DataTable / Table: Fixed an issue where width props on TableCell or DataTable columns were ignored. The CLI now correctly respects width values in the following formats:
      • Numbers (e.g., 100 for pt)
      • Percentage strings (e.g., "25%")
      • Pixel strings (e.g., "50px")
    • PdfList: Fixed an issue where rows would overlap when text wrapped to multiple lines across all variants (bullet, numbered, checklist, icon, and multi-level).

    To apply these fixes to your existing installation, re-run the following commands:

    pdfx add table
    pdfx add data-table
    pdfx add list
  11. Use PDFx registry components

    main

    The PDFx registry provides a comprehensive set of components for building PDF documents. These components are exported from the main registry entrypoint and can be used to construct structured, styled layouts. Common component categories include:

    • Typography: Heading, Text, Link, Badge, KeyValue.
    • Layout: Stack, Section, Divider, PageBreak, KeepTogether.
    • Data Display: Table, DataTable, PdfList, PdfCard, PdfGraph.
    • Page Elements: PageHeader, PageFooter, PdfPageNumber, PdfWatermark.
    • Interactive/Form Elements: PdfForm, PdfSignatureBlock, PdfQRCode.
    • Media: PdfImage.

    All components accept props defined by their respective [ComponentName]Props types and generally adhere to the PDFComponentProps base type.