PdfViewer
Read PDFs in the page: thumbnails, zoom, rotation, search with highlights, selectable text, download, print and fullscreen.
Status: beta. Category: Viewers.
import { PdfViewer } from '@glyphui/viewers/pdf'
Exports: PdfViewer, pdfViewerStyles, setPdfWorker, cdnWorkerUrl.
Notes
- Install the optional peer dependency first: npm install pdfjs-dist.
- pdf.js parses in a Web Worker. By default it loads from jsDelivr, matching your pdfjs-dist version. To serve it yourself, call setPdfWorker once at startup: with Vite, import workerSrc from "pdfjs-dist/build/pdf.worker.min.mjs?url"; with webpack or Next.js, pass new URL("pdfjs-dist/build/pdf.worker.min.mjs", import.meta.url).
- Only pages near the view are rendered, so long documents stay fast; the page size you see comes from each page, so mixed sizes and orientations lay out correctly.
- Keyboard: Page Up/Down change pages, + and − zoom, 0 fits the width, R rotates, and ⌘F or Ctrl+F jumps to search.
- Each page is a labelled group with its text layer, so screen readers can read the document.
- pdf.js adds about 130 kB gzipped, plus a 365 kB worker that downloads in the background; both load only when a PdfViewer mounts.
PdfViewer props
| Prop | Type | Default | Description |
|---|---|---|---|
source (required) | ViewerSource | The PDF: a URL, a File/Blob, or bytes. | |
bordered | boolean | Draw the viewer as a bordered card. Default true. | |
className | string | ||
defaultPage | number | Page to open on, from 1. | |
defaultZoom | PdfZoom | Starting zoom: fit-width (default), fit-page, or a scale where 1 is actual size. | |
fileName | string | Name for downloads. Defaults to the file or URL name. | |
height | string | number | Height of the viewer. Default 640. | |
onLoad | ((info: { pages: number; }) => void) | ||
onPageChange | ((page: number) => void) | ||
thumbnails | boolean | Show the thumbnails sidebar at first (wide screens). Default true. | |
workerSrc | PdfWorkerSource | pdf.js worker for this viewer. See setPdfWorker to set it once for the app. |
Example: A report
Search for “revenue”: matches highlight on every page.
import { PdfViewer } from '@glyphui/viewers/pdf'
import { fixture } from '../../../lib/fixtures'
export default function Example() {
return <PdfViewer source={fixture('report.pdf')} height={560} className="w-full" />
}
Example: A file the user picks
Works with File objects from FileUpload, without uploading anything.
import { type FileItem, FileUpload } from '@glyphui/ui'
import { PdfViewer } from '@glyphui/viewers/pdf'
import { useState } from 'react'
export default function Example() {
const [files, setFiles] = useState<FileItem[]>([])
const file = files[0]?.file
return (
<div className="grid w-full gap-4">
<FileUpload
accept="application/pdf,.pdf"
maxFiles={1}
files={files}
onFilesChange={setFiles}
hint="A PDF, up to 50 MB. It stays on your device."
maxSize={50 * 1024 * 1024}
/>
{file && <PdfViewer source={file} height={480} />}
</div>
)
}
Example: Start page and zoom
Open on page 3, fitting the whole page, without thumbnails.
import { PdfViewer } from '@glyphui/viewers/pdf'
import { fixture } from '../../../lib/fixtures'
export default function Example() {
return (
<PdfViewer
source={fixture('report.pdf')}
defaultPage={3}
defaultZoom="fit-page"
thumbnails={false}
height={520}
className="w-full"
/>
)
}
Live page: https://glyphui.xyz/components/pdf-viewer