Integrations
Web Components
pdf-viewer
pdf-viewer
<pdf-viewer> shows a PDF document in the page with a toolbar for page navigation, zoom, search, thumbnails, download and print. Its toolbar, sidebar and borders read Vanilla Breeze design tokens, so they follow the active theme.
Install
Load the component from a CDN with one script tag. The file is about 2.4 MB because it contains PDF.js.
<script type ="module" src ="https://unpkg.com/@profpowell/[email protected] /dist/pdf-viewer.js" > </script >
Or install it from npm and import it in your own script.
npm install @profpowell/pdf-viewer
import '@profpowell/pdf-viewer' ;
Demo
A five-page sample document with the thumbnail sidebar open. Change the site theme to see the toolbar and sidebar change with it.
Usage
Point src at a PDF file. The viewer is 600 pixels tall unless you set a height in CSS.
<pdf-viewer src ="/files/annual-report.pdf" title ="Annual report" > </pdf-viewer >
Open on a given page, fit the page to the width of the viewer, and show the thumbnail sidebar and the search bar.
<pdf-viewer src ="/files/annual-report.pdf" page ="3" zoom ="fit-width" show-thumbnails show-search > </pdf-viewer >
Load a file the visitor picks, and listen for the event that reports the page count.
<input type ="file" id ="pick" accept ="application/pdf" /> <pdf-viewer id ="viewer" > </pdf-viewer > <script type ="module" > const viewer = document .getElementById ('viewer' ); document .getElementById ('pick' ).addEventListener ('change' , (event ) => { viewer.setData (event.target .files [0 ]); }); viewer.addEventListener ('pdf-loaded' , (event ) => { console .log (`${event.detail.pageCount} pages` ); }); </script >
Reference
Attributes
Attribute Values Default Purpose
srcURL None The PDF document to load.
pageNumber, starting at 1 1The page shown.
zoomA number such as 1.5, or fit-width, fit-page, auto fit-widthThe zoom level, as a number or a fit mode.
modelight, darkFollows the page, then the system setting Forces the light or dark color scheme.
viewsingle, continuoussingleShows one page at a time, or all pages in one scrolling column.
show-toolbarBoolean. Set show-toolbar="false" to hide. Shown Shows the toolbar.
show-thumbnailsBoolean Off Opens the thumbnail sidebar.
show-searchBoolean Off Opens the search bar.
no-downloadBoolean Off Hides the download button.
no-printBoolean Off Hides the print button.
titleText None The title shown in the toolbar. It is also the file name used for a download.
lazyBoolean Off Waits to load the document until the viewer scrolls into view.
Methods and properties
The properties src, page, zoom, mode and view mirror the attributes of the same name and can be set from script.
Method Purpose
nextPage(), prevPage(), goToPage(n)Move between pages.
getPageCount(), getCurrentPage()Return the number of pages and the current page number.
zoomIn(), zoomOut(), setZoom(level)Change the zoom. setZoom() takes a number or 'fit-width', 'fit-page', 'auto'.
setData(source)Loads a PDF from an ArrayBuffer, a Uint8Array or a File. Returns a promise.
openFile()Opens the browser's file chooser so the visitor can pick a PDF.
download(), print()Download or print the current document. Both return a promise.
search(query), clearSearch()Search the document text, and clear the search.
toggleFullscreen(), toggleThumbnails(), toggleSearch()Turn fullscreen, the thumbnail sidebar and the search bar on or off.
Events
Event Detail Fires when
pdf-loaded{ url, pageCount, title }The document has loaded.
pdf-load-error{ url, error }The document failed to load.
page-change{ page, previousPage, pageCount }The current page changes.
zoom-change{ zoom, previousZoom }The zoom level changes.
view-change{ view }The view switches between single and continuous.
file-opened{ name, size }The visitor opens a local file.
CSS custom properties
Set these on the element to change one viewer. Each one wins over the Vanilla Breeze token listed beside it.
Property Controls Falls back to
--pdf-viewer-bgViewer background --color-surface
--pdf-viewer-toolbar-bgToolbar background --color-surface-raised
--pdf-viewer-sidebar-bgThumbnail sidebar background --color-surface-sunken
--pdf-viewer-content-bgThe area behind the page Built-in gray
--pdf-viewer-border-colorBorders --color-border
--pdf-viewer-text-color, --pdf-viewer-text-mutedText and secondary text --color-text, --color-text-muted
--pdf-viewer-accent-colorAccent color for active controls --color-interactive
--pdf-viewer-hover-bgButton hover background Built-in value
--pdf-viewer-font-family, --pdf-viewer-font-sizeTypeface and base text size --font-sans; 14px
--pdf-viewer-shadowShadow under the page --shadow-m in light mode, --shadow-l in dark mode
--pdf-viewer-sidebar-width, --pdf-viewer-toolbar-heightSidebar width and toolbar height 200px; 40px
--pdf-viewer-highlight, --pdf-viewer-highlight-activeSearch matches, and the current match Built-in yellow and orange
The component also exposes four shadow parts for ::part() selectors: toolbar, content, sidebar and search.
See the pdf-viewer documentation for the full detail.
With Vanilla Breeze
The component reads Vanilla Breeze tokens directly. Custom properties inherit into the shadow DOM, so no setup is needed.
Token Drives
--color-surfaceViewer background
--color-surface-raisedToolbar background
--color-surface-sunkenThumbnail sidebar background
--color-border, --border-width-thinBorder color and width
--color-text, --color-text-mutedText and secondary text
--color-interactiveAccent color
--color-errorThe load error message
--font-sansTypeface
--font-size-2xs, --font-size-xs, --font-size-lText sizes in the toolbar and overlays
--size-3xs, --size-2xs, --size-xs, --size-s, --size-mPadding and gaps
--radius-xs, --radius-s, --radius-mCorner radius of buttons, inputs and the viewer frame
--shadow-s, --shadow-m, --shadow-lShadows
The fallback order for each value is: the --pdf-viewer-* property if you set it, then the Vanilla Breeze token, then a built-in default. Without Vanilla Breeze the built-in defaults apply.
Three of the names the component looks for are not Vanilla Breeze tokens: --color-surface-hover, --font-size-sm and --font-size-base. Button hover backgrounds and most text sizes therefore use the built-in defaults (13px and 14px text) on a Vanilla Breeze page. Set --pdf-viewer-hover-bg and --pdf-viewer-font-size to change them.
The page of the PDF stays white in light and dark mode. This is deliberate and matches how a PDF prints.
For light and dark mode the component watches data-mode, data-theme and a dark class on the page and updates when they change. Set the mode attribute to force one.
pdf-viewer.report { block-size : 32rem ; --pdf-viewer-accent-color : var (--color-accent); --pdf-viewer-toolbar-bg : var (--color-surface); --pdf-viewer-hover-bg : var (--color-hover-bg); --pdf-viewer-sidebar-width : 10rem ; }
Accessibility
The README has no accessibility notes. The points below come from the code of version 0.2.0.
The element adds tabindex="0" to itself so it can take keyboard focus. The shortcuts work while focus is inside the viewer.
Every toolbar button has a text label for screen readers, such as "Next page" and "Zoom in".
Key Action
Right arrow , Down arrow , Page Down Next page
Left arrow , Up arrow , Page Up Previous page
Home , End First page, last page
+ , - Zoom in, zoom out
0 Fit to width
Ctrl or Cmd + F Open or close the search bar
Escape Close the search bar, or leave fullscreen