v22.18.0 release notes

PdfViewer

PdfViewer shows PDF documents in the page with pdf.js: pages drawn on demand, selectable and searchable text read by screen readers, links, zoom, rotation, search with highlighted matches, protected files, download and full screen, with an accessible toolbar. pdfjs-dist, an Apache-2.0 optional dependency, is loaded only where a viewer is shown, with nothing loaded from a CDN.

View as .md
import { PdfViewer } from '@exhaustive-ui/core/pdfviewer';

PdfViewer reads the documents with pdf.js, the PDF reader of Firefox, published by Mozilla as pdfjs-dist under the Apache-2.0 license. It is an optional peer dependency of ExhaustiveUI, so only the applications that show PDF files install it. There is no account, key or paid service: install the package, and nothing else is needed.

npm install pdfjs-dist

pdf.js is loaded in the browser only, when a viewer is shown: the pages without a viewer do not download it, and during server-side rendering the viewer renders its frame and a placeholder. The documents are read in the main thread by default, or in a web worker with two lines of configuration, described in Worker and Assets.

PdfViewer shows the document of its src. The pages are drawn on demand as they come near the visible area, at the pixel density of the screen, with a placeholder of their exact size until then, so the scroll bar does not jump. The text of the pages can be selected and copied, and their links followed. The toolbar moves between pages, zooms, rotates, searches, downloads the file and shows it in full screen.

Loading document

The src is the URL of a file, its bytes as an Uint8Array or an ArrayBuffer, or a Blob, such as the File of a file input: the file picked below never leaves the browser. A URL is downloaded by parts when the server accepts range requests, and the first page shows before the end of the download. onLoad gives the number of pages and the information of the document, onError the cause of a failure.

Loading document

page, zoom and rotation are two-way bindable. The page follows the scroll and the toolbar, and setting it scrolls to that page. The zoom is a factor, 1 being the printed size of the page, or a fit mode: auto (the width of the page, up to its printed size, the default), page-width or page-fit; minZoom and maxZoom bound it. When the zoom or the rotation changes, the same part of the document stays in view.

ZoomFitWidth50%100%

Page 1, zoom page-fit, rotation 0°

Loading document

toolbarItems chooses the controls of the toolbar among pages, zoom, rotate, search, download and fullscreen, and toolbar set to false hides it. The toolbarstart and toolbarend templates add content at both ends of the toolbar, and the toolbar template replaces its content. They receive the viewer, to call its methods such as goToPage(), zoomIn(), download() or openSearch(), and the current page, numPages and scale. Their buttons join the arrow key navigation of the toolbar.

There is no print button: printing from the page needs either the PDF reader of the browser in a hidden frame, which several browsers block, or replacing the whole page while printing. The download button hands the file to the PDF reader of the user, which prints it.

Loading document

The search button, or Ctrl+F (⌘+F on macOS) while the focus is in the viewer, opens the search bar. The matches are highlighted in the text of the pages, the current one stands out, and Enter and Shift+Enter move to the next and the previous match, across pages. The search ignores the case and the accents, so e finds é, unless caseSensitive or matchDiacritics is set. The search(text), findNext() and findPrevious() methods search from your own controls, and onSearch tells the number of matches.

Loading document

A protected document asks for its password in the viewer, and a wrong password is announced and asked again. Set the password input to open it without asking, and listen to onPassword to ask in your own way: it tells whether the password is required or incorrect. The password of this sample is exhaustive.

Loading document

Without configuration, pdf.js reads the documents in the main thread, from a lazy chunk of the application: it works everywhere and nothing is loaded from a CDN, but a large or complex file can make the page less responsive while it is opened. To read the documents in a web worker, copy the worker file of pdf.js with the assets of the application and give its URL to providePdfViewer, in the providers of the application or of a route. Each viewer then starts its own worker and stops it when it is destroyed. This documentation does so.

The worker can not be found without configuration: the Angular application builder puts the code shared by its chunks in the main bundle, so a worker made from a chunk of pdf.js would start the whole application again. When the worker file can not start, for example with a Content Security Policy without worker-src 'self', the viewer warns in the console and reads the documents in the main thread.

// angular.json, in projects > your-app > architect > build > options > assets
{ "glob": "pdf.worker.min.mjs", "input": "node_modules/pdfjs-dist/build", "output": "/assets/pdfjs" }
import { ApplicationConfig } from '@angular/core';
import { providePdfViewer } from '@exhaustive-ui/core/pdfviewer';

export const appConfig: ApplicationConfig = {
    providers: [providePdfViewer({ workerSrc: '/assets/pdfjs/pdf.worker.min.mjs' })]
};

providePdfViewer also sets the URLs of the optional data files of pdf.js, copied the same way from node_modules/pdfjs-dist: cMapUrl for the character maps of some Chinese, Japanese and Korean documents, standardFontDataUrl for the documents that use the standard fonts without embedding them (the fonts of the system are used otherwise) and wasmUrl for JPEG 2000 images. documentOptions passes other options to pdf.js, such as withCredentials or httpHeaders, and loader loads another build of pdf.js.

Screen Reader

The pages are in a scrollable element with the document role, named by the document label, ariaLabel or ariaLabelledBy, which takes the focus so that the keyboard can scroll it. Each page is a group named Page 2 of 12. The drawing of a page is hidden from assistive technologies, and the text of the page sits over it in a transparent text layer, in the reading order of the file: screen readers read it, the browser search finds it, and it can be selected and copied. The links of the pages are real links, named by the text under them. The text exists for the pages near the visible area, which are drawn as the document scrolls.

The controls follow the toolbar pattern: an element with the toolbar role and a label, one tab stop, and arrow keys between the controls. Every button has an accessible name from the labels input. The buttons that can not act stay focusable with aria-disabled, so the focus is not lost when the last page is reached. The fit to width button is a toggle with aria-pressed, and the search button tells with aria-expanded whether the search bar is open.

A polite live region announces the loaded document and its number of pages, the current page when it changes, the zoom, and the number of matches of a search. The progress bar has the progressbar role while the document loads, an error is an alert, and the password form has a labelled input, with the wrong password announced. Animations stop when the user prefers reduced motion.

<p-pdf-viewer src="/files/report.pdf" ariaLabel="Annual report" [labels]="{ toolbar: 'Report tools', document: 'Annual report' }" />

Keyboard Support

KeyFunction
tabMoves the focus to the toolbar, the search bar, the pages and their links.
right arrow / left arrowIn the toolbar, moves the focus to the next or the previous control, in the reading direction.
home / endIn the toolbar, moves the focus to the first or the last control. Elsewhere, shows the first or the last page.
page down / page upShows the next or the previous page.
up arrow / down arrowScrolls the pages. In the page number input, shows the previous or the next page.
enterIn the page number input, shows the typed page. In the search input, moves to the next match, or to the previous one with shift.
+ / -Zooms in or out, also with ctrl or ⌘.
ctrl + 0 / ⌘ + 0Goes back to the automatic zoom.
ctrl + f / ⌘ + fOpens the search bar, when the focus is in the viewer.
escapeIn the search bar, closes it, removes the highlights and puts the focus back on the search button.