Skip to content
Inkoxide

Search covers every page and heading of the docs. Use ↑ ↓ and ↵.

In the browser

The hook and the components of @inkoxide/render/browser that render a document in the page.

Edit this page

Import them from @inkoxide/render/browser. Examples, and how these components work together, are in the guide.

usePdf

Renders element to a PDF in the engine's worker when the component mounts, and again on update(). A render that starts stops the one still running; unmounting stops it too. opts are read when a render starts.

const { url, loading, update } = usePdf(<Invoice invoice={invoice} />);
import { Document, Page, Text, usePdf } from '@inkoxide/render/browser';

const Letter = ({ name }: { name: string }) => (
  <Document>
    <Page size="A5" className="p-10">
      <Text>Dear {name},</Text>
    </Page>
  </Document>
);

export default function Preview({ name }: { name: string }) {
  const { url, loading, error, update } = usePdf(<Letter name={name} />);
  if (error) return <p>The PDF did not render.</p>;
  return (
    <>
      <button onClick={() => update(<Letter name={name} />)} disabled={loading}>
        {loading ? 'Rendering…' : 'Render again'}
      </button>
      {url && <iframe src={url} title="Letter" style={{ width: '100%', height: 600 }} />}
    </>
  );
}
ArgumentTypeDefaultDescription
elementReactElement
optsBrowserRenderOptions{}

It returns:

FieldTypeDescription
blobBlob | nullThe PDF of the last render that finished, or null.
urlstring | nullAn object URL of blob, revoked when a new one replaces it and on unmount; or null.
loadingbooleanWhether a render is running.
errorunknownWhy the last render failed (a RenderError, usually), or null.
update(element?: ReactElement) => voidRenders again: element when given, otherwise the last one.

A link that downloads document as a PDF, once it has rendered. It renders when it mounts: give it another key to render again.

import { Document, Page, PdfDownloadLink, Text } from '@inkoxide/render/browser';

const Receipt = () => (
  <Document>
    <Page size="A6" className="p-8">
      <Text className="text-lg font-bold">Receipt</Text>
    </Page>
  </Document>
);

export default function DownloadButton() {
  return (
    <PdfDownloadLink document={<Receipt />} fileName="receipt.pdf">
      {({ loading }) => (loading ? 'Rendering…' : 'Download the receipt')}
    </PdfDownloadLink>
  );
}
PropTypeDefaultDescription
document (required)ReactElementThe <Document> element to render.
fileNamestringdocument.pdfThe name the file is saved under.
optionsBrowserRenderOptions
childrenReactNode | ((pdf: UsePdfResult) => ReactNode)What the link shows, or a function of the render's state.

Other props go to the <a> element.

PdfViewer

document, rendered and shown in an <iframe>, in the browser's own PDF viewer. It renders when it mounts: give it another key to render again.

import { Document, Page, PdfViewer, Text } from '@inkoxide/render/browser';

const Receipt = () => (
  <Document>
    <Page size="A6" className="p-8">
      <Text className="text-lg font-bold">Receipt</Text>
    </Page>
  </Document>
);

export default function ReceiptPreview() {
  return <PdfViewer document={<Receipt />} fallback={<p>Rendering…</p>} style={{ width: '100%', height: 600 }} />;
}
PropTypeDescription
document (required)ReactElementThe <Document> element to render.
optionsBrowserRenderOptions
fallbackReactNodeShown in place of the frame while the first render runs, or when it failed.

Other props go to the <iframe> element.

Synced from components/browser.md in the inkoxide repository.