Skip to content

useEasyPdf Hook API Reference

The useEasyPdf hook is the main interface for generating PDFs in both visual and programmatic modes.

Basic Usage

tsx
import { useEasyPdf } from "@easypdf/react";

function PDFGenerator() {
  const { pdfRef, downloadPDF } = useEasyPdf();

  return (
    <div>
      <button onClick={() => downloadPDF(pdfRef)}>Download PDF</button>
      <div ref={pdfRef}>
        <h1>Hello, PDF!</h1>
      </div>
    </div>
  );
}

Hook Signature

tsx
const useEasyPdf = (initialConfig?: PDFConfig) => { ... }

The optional initialConfig sets defaults for all PDF operations from this hook instance.

Return Values

NameTypeDescription
pdfRefReact.RefObject<HTMLDivElement>Attach to the element you want to capture as PDF
downloadPDF(source: RefObject<HTMLDivElement> | Blob | string | null, config?: PDFConfig) => Promise<void>Generate and download a PDF
createPDF(content: React.ReactNode, config?: PDFConfig) => Promise<void>Render JSX to PDF and download (programmatic)
createPDFBlob(content: React.ReactNode, config?: PDFConfig) => Promise<Blob>Render JSX to a PDF Blob (programmatic)
viewPDF(content: React.ReactNode | Blob, config?: PDFConfig) => Promise<void>Open PDF in a new browser tab
isLoadingbooleantrue while any PDF operation is in progress
errorError | nullSet if the last operation threw an error

Methods

downloadPDF

Generates a PDF and triggers a browser download. Works in two ways depending on what you pass as the first argument:

tsx
// From a ref (Visual Mode) — captures the element's current DOM
await downloadPDF(pdfRef, { filename: "document.pdf" });

// From a Blob — downloads a blob you already have
const blob = await createPDFBlob(content);
await downloadPDF(blob, { filename: "document.pdf" });

// Shorthand: pass a filename string to use pdfRef with that filename
await downloadPDF("document.pdf");

Parameters

NameTypeDescription
sourceRefObject<HTMLDivElement> | Blob | string | nullRef to capture, a Blob to download, or a filename string (uses pdfRef)
configPDFConfigOptional config, merged with hook and global config

createPDF

Renders a JSX element off-screen and downloads the result as a PDF. Use this when you don't want to display the content on screen first.

tsx
await createPDF(
  <div>
    <h1>Generated PDF</h1>
    <p>Content rendered off-screen.</p>
  </div>,
  { filename: "generated.pdf" }
);

Parameters

NameTypeDescription
contentReact.ReactNodeJSX content to render and download
configPDFConfigOptional config

createPDFBlob

Renders a JSX element off-screen and returns the PDF as a Blob. Useful when you need to upload it, display a preview, or handle the file yourself.

tsx
const blob = await createPDFBlob(
  <div>
    <h1>Hello, PDF!</h1>
  </div>,
  { filename: "document.pdf" }
);

Parameters

NameTypeDescription
contentReact.ReactNodeJSX content to convert
configPDFConfigOptional config

Returns

Promise<Blob> — a PDF blob you can download, upload, or open.


viewPDF

Opens a PDF in a new browser tab. Accepts either JSX content (rendered off-screen) or an existing Blob.

tsx
// From JSX
await viewPDF(<div><h1>Preview</h1></div>);

// From an existing blob
const blob = await createPDFBlob(content);
await viewPDF(blob);

Loading State

All operations share a single isLoading flag:

tsx
function PDFGenerator() {
  const { pdfRef, downloadPDF, isLoading } = useEasyPdf();

  return (
    <div>
      <button onClick={() => downloadPDF(pdfRef)} disabled={isLoading}>
        {isLoading ? "Generating..." : "Download PDF"}
      </button>
      <div ref={pdfRef}>
        <h1>Hello, PDF!</h1>
      </div>
    </div>
  );
}

Error Handling

tsx
function PDFGenerator() {
  const { pdfRef, downloadPDF, isLoading, error } = useEasyPdf();

  return (
    <div>
      {error && <div className="error">Error: {error.message}</div>}

      <button onClick={() => downloadPDF(pdfRef)} disabled={isLoading}>
        {isLoading ? "Generating..." : "Download PDF"}
      </button>

      <div ref={pdfRef}>
        <h1>Hello, PDF!</h1>
      </div>
    </div>
  );
}

Configuration Priority

Config is merged in this order (later wins):

  1. Default config (pageSize: "A4", margins: 20px, scale: 2, white background)
  2. initialConfig passed to useEasyPdf(initialConfig)
  3. Per-call config passed to downloadPDF, createPDF, etc.
tsx
const { pdfRef, downloadPDF } = useEasyPdf({
  pageSize: "A4",         // hook-level default
  watermark: { text: "DRAFT" },
});

await downloadPDF(pdfRef, {
  filename: "final.pdf",  // call-level override
  watermark: { opacity: 0.3 }, // merges into existing watermark config
});

Examples

Visual Mode

tsx
function VisualPDFGenerator() {
  const { pdfRef, downloadPDF, isLoading } = useEasyPdf({
    pageSize: "A4",
    margins: { top: 20, right: 20, bottom: 20, left: 20 },
  });

  return (
    <div>
      <button onClick={() => downloadPDF(pdfRef)} disabled={isLoading}>
        {isLoading ? "Generating..." : "Download PDF"}
      </button>

      <div ref={pdfRef}>
        <h1>Document Title</h1>
        <p>Document content...</p>
      </div>
    </div>
  );
}

Programmatic Mode

tsx
function ProgrammaticPDFGenerator() {
  const { createPDF, isLoading } = useEasyPdf();

  const generatePDF = async () => {
    await createPDF(
      <div>
        <h1>Generated PDF</h1>
        <p>Generated content...</p>
      </div>,
      { filename: "generated.pdf" }
    );
  };

  return (
    <button onClick={generatePDF} disabled={isLoading}>
      {isLoading ? "Generating..." : "Generate PDF"}
    </button>
  );
}

With Custom Styling

tsx
function StyledPDFGenerator() {
  const { pdfRef, downloadPDF } = useEasyPdf({
    styles: {
      backgroundColor: "#ffffff",
      defaultFontSize: 12,
      defaultFontFamily: "Arial, sans-serif",
      defaultTextColor: "#333333",
      customCSS: `
        .pdf-title { font-size: 24px; color: #2c3e50; text-align: center; }
        .pdf-section { margin: 15px 0; padding: 20px; background: #f8f9fa; }
      `,
    },
  });

  return (
    <div>
      <button onClick={() => downloadPDF(pdfRef)}>Download PDF</button>

      <div ref={pdfRef}>
        <h1 className="pdf-title">Styled Document</h1>
        <div className="pdf-section">
          <p>Content with custom styling...</p>
        </div>
      </div>
    </div>
  );
}

Released under the MIT License.