PDF Viewer
A composable PDF viewer with paging, zoom, rotation, thumbnails and download.
/ –
100%
Installation
Run the following command to install the `pdf-viewer` components:
npx shadcn-svelte@latest add https://more-shadcn.noair.fun/r/pdf-viewer.json
Rendering is done by
pdfjs-dist, which is installed alongside it and loaded
lazily on the client the first time a viewer mounts — it adds nothing to pages that never show
a PDF.Composing your own
With no children the root renders a complete viewer — one that drops the thumbnail sidebar,
and its toggle, when the viewer itself is narrower than 560px, so the pages keep the room.
Pass children and it renders only what you give it, so every part — the pages, the thumbnails,
the toolbar and each control inside it — can be arranged however you like. Here the thumbnails
move to the right and the toolbar keeps just paging and zoom.
/ –
100%
Every part reads the same context, which is exported as
getPdfViewerContext() for
building controls of your own — it exposes the page count, current page, zoom and rotation,
plus goTo, zoomIn, rotate and download.Controlling it from outside
page, scale, rotation, fit and sidebar are all bindable. Setting page scrolls to it, and the
binding updates as the reader scrolls. fit keeps refitting as the viewer resizes until
you zoom manually. page=1 · scale=1.00 · fit=width
Opening a local file
src takes a URL or raw bytes. Bytes are copied before they go to pdf.js — it hands
them to its worker, which would otherwise leave your buffer detached and unusable. The file below
is read in your browser and never uploaded. Read in your browser, never uploaded.
Rendering
Only pages near the viewport hold a bitmap — the rest are empty frames at the right size, so a
long document costs little memory. Pages render at device resolution for crisp text, back off
before exceeding what the browser will allocate at extreme zoom, and re-render offscreen so a
zoom never flashes blank.
Ctrl/⌘ + scroll zooms the document rather than
the page.Fonts and character maps
pdf.js ships its standard fonts, CJK character maps and image decoders as separate files. By
default they load from jsDelivr, pinned to the installed version. To self-host, copy them out
of
node_modules/pdfjs-dist and point the viewer at them.In development
Under
vite dev you may see ReferenceError: document is not defined in
the console, once per document. Vite's dev server injects its client into the pdf.js worker file,
and that client expects a page. It is harmless — documents still load and render — and production
builds ship the worker untouched.