Documents and fields
PDF Viewer
Shows a PDF page by page in the browser with pdf.js, with an overlay layer for fields. You pass in pdf.js.
- @signnative/ui
- MIT
- Client only · no backend
The examples use a synthetic PDF made in your browser. Nothing is uploaded.
The CSS variables this sets
:root {
--signing-accent: #3157e8;
--sig-accent: #3157e8;
--sig-accent-soft: color-mix(in oklab, #3157e8 14%, white);
--sig-radius: 8px;
--radius-control: 8px;
--radius-panel: 12px;
--font-sans: var(--font-inter), ui-sans-serif, system-ui, sans-serif;
}In the signer page kit, set the accent with accentProps(accent) on the element around the surface; it sets data-signing-accent and --signing-accent. The secure elements take the same values as a checked appearance object instead.
Pages, fitted to the container
Pages are drawn as they scroll into view. onPageChange reports the current page.
current page: 1
Loading document…
import { useState } from "react";
import { PdfViewer, type PdfjsLib } from "@signnative/ui";
import "@signnative/ui/styles.css";
// pdf.js is not a dependency of the package: pass it in. Serve its worker yourself.
const loadPdfjs = () => import("pdfjs-dist/legacy/build/pdf.mjs") as unknown as Promise<PdfjsLib>;
export function Example({ pdf }: { pdf: Uint8Array | null }) {
const [page, setPage] = useState(1);
return (
<div>
<p>Page {page}</p>
<PdfViewer data={pdf} pdfjs={loadPdfjs} workerSrc="/pdf.worker.min.mjs" maxWidth={560} onPageChange={setPage} />
</div>
);
}
Clicks and an overlay
onPageClick gives fractions of the page; renderOverlay draws on top of it in the same unit.
Click the page: no clicks yet
Loading document…
import { useState } from "react";
import { PdfViewer, type PdfjsLib } from "@signnative/ui";
import "@signnative/ui/styles.css";
// pdf.js is not a dependency of the package: pass it in. Serve its worker yourself.
const loadPdfjs = () => import("pdfjs-dist/legacy/build/pdf.mjs") as unknown as Promise<PdfjsLib>;
export function Example({ pdf }: { pdf: Uint8Array | null }) {
const [dots, setDots] = useState<{ x: number; y: number }[]>([]);
return (
<PdfViewer
data={pdf}
pdfjs={loadPdfjs}
workerSrc="/pdf.worker.min.mjs"
onPageClick={(_page, x, y) => setDots((d) => [...d, { x, y }])}
renderOverlay={() => dots.map((d, i) => <span key={i} style={{ position: "absolute", left: d.x * 100 + "%", top: d.y * 100 + "%" }}>•</span>)}
/>
);
}
Loading
data={null} shows the loading state.
Loading document…
import { PdfViewer, type PdfjsLib } from "@signnative/ui";
import "@signnative/ui/styles.css";
// pdf.js is not a dependency of the package: pass it in. Serve its worker yourself.
const loadPdfjs = () => import("pdfjs-dist/legacy/build/pdf.mjs") as unknown as Promise<PdfjsLib>;
export function Example() {
return <PdfViewer data={null} pdfjs={loadPdfjs} />;
}
Installation
npx shadcn@latest add <site>/r/pdf-viewer.json
Not published yet
API
className and children are left out: every component that takes them passes them on.
PdfViewer
| Name | Type | Default | Description |
|---|---|---|---|
| data | ArrayBuffer | Uint8Array | null | The PDF. Copied, so your buffer stays usable. null shows the loading state. | |
| pdfjs | PdfjsLib | (() => Promise<PdfjsLib>) | pdf.js (pdfjs-dist 4 or later), or a function that loads it. The package does not import it. | |
| workerSrc | string | Where the browser gets pdf.worker.min.mjs. | |
| assetsUrl | string | Base URL of pdf.js's standard_fonts/, cmaps/, wasm/ and iccs/ folders, for PDFs that need them. | |
| pageWidth | number | Width of every page in CSS pixels. Left out, pages fit the container. | |
| maxWidth | number | 820 | Widest a page gets when it fits the container. |
| textLayer | boolean | true | The page's text, invisible over the drawing, so it can be read aloud, selected and searched. |
| onPagesMeasured | (sizes: PdfPageSize[]) => void | Once per document: every page's size in PDF points, rotation applied. | |
| onPageChange | (page: number) => void | When another page becomes the current one. | |
| renderOverlay | (page, size) => ReactNode | Draws on top of a page, for example fields. Place children with fractions of the page. | |
| onPageClick | (page: number, x: number, y: number) => void | A click on a page, at fractions from its top-left corner. | |
| labels | Partial<PdfViewerLabels> | English | Every text, for translation. |
Accessibility
- The viewer has a name with the page count. Page Up and Page Down go to the previous and next page.
- The text layer lets screen readers read the page and lets people select and search text.