@ist-group/pdf-viewer
v0.1.0
Published
A PDF viewer for Vue 3, drawn with pdf.js: pages on canvas, paging, zoom, thumbnails and printing that works under a strict CSP.
Keywords
Readme
@ist-group/pdf-viewer
A PDF viewer for Vue 3, drawn with pdf.js: every page on a canvas, a toolbar for paging and zoom,
page thumbnails, and print() / download() for the app's own buttons. It has no print, download
or save buttons of its own.
It works under a strict Content Security Policy: it reads the PDF's bytes instead of opening a blob
URL, and prints the pages as images. What it needs from the CSP: worker-src 'self' (the pdf.js
worker) and img-src data: (printing).
Moved from ist-elev (platform-core/src/components/NSACustomComponents/NsaPdfView) with its git
history.
Use
import '@ist-group/pdf-viewer/style.css';
import { PdfViewer, pdfViewerLabelsEn } from '@ist-group/pdf-viewer';<div style="height: 80vh">
<PdfViewer ref="pdfView" :pdf="blob" title="Fraværsstamkort for Clara Jensen" />
</div>await pdfView.value?.print(); // true once the print dialog has closed, false if nothing was printed
pdfView.value?.download('Fravaersstamkort_Clara_Jensen.pdf');pdf: the PDF as aBlob.title: the viewer's accessible name.labels(optional): texts in place of the Danish defaults, all or some.pdfViewerLabelsEnis English.
The viewer fills its parent, so give the parent a height. pdf.js and its worker are loaded the first time a PDF is shown, not with the app.
Requirements
- Vue 3.5 or newer (a peer dependency).
- A bundler that understands Vite's
?urlimports: the package imports the pdf.js worker aspdfjs-dist/legacy/build/pdf.worker.min.mjs?url, and the app's build emits it as a file of its own.
Colors
Every color is a CSS custom property you can set on the viewer or any element around it:
--pdf-viewer-background, -surface, -border, -focus, -caption, -icon, -spinner,
-error-bar, -error-surface, -error-text. Unset, each takes the CUI design token of the same
role (--cui-background-dashboard and so on), and CUI's own default when the app has no CUI.
Develop
pnpm install
pnpm lint # ESLint, then prettier --check; fixes nothing
pnpm lint:fix # ESLint --fix, then prettier --write
pnpm test # vitest, jsdom
pnpm typecheck # vue-tsc
pnpm build # dist/: index.js, the lazy PdfJsViewer chunk, pdf-viewer.css and .d.ts filesThe lint rules are ist-elev's (frontend/shell/eslint.config.ts, frontend/.prettierrc) without what
only ist-elev has: its auto-import globals, its own ist-rules/*, Tailwind and Apollo.
eslint.config.ts says what was left out.
CI
Jenkinsfile here is the package's pipeline: the multibranch job ISTElev/ist-elev-pdf-viewer with script path
packages/pdf-viewer/Jenkinsfile, beside the service's service/Jenkinsfile. It builds when
packages/pdf-viewer/ changed (or with ForceBuild): install with the frozen lockfile, lint
(SkipLint skips it), typecheck, test (JUnit report), build, and pnpm pack, whose tarball it
archives. pnpm comes from corepack, at
the version packageManager names.
A tag pdf-viewer-v<version>, or the Publish parameter, adds the Publish stage, which publishes that
archived tarball. A tag must name the version in package.json, or the build fails. Other tags in the
repo (the service's) are skipped.
Publish
Public on npmjs.org, in the ist-group org beside CommonUI's @ist-group/* packages, MIT as theirs are.
A consumer needs no registry setting: @ist-group resolves from the default registry. publishConfig in
package.json names the registry and "access": "public", which npm requires for a scoped package's
first publish. Publishing takes an npm account that is a member of ist-group; an org admin invites it.
By hand, logged in with npm login: pnpm publish. npm asks for a 2FA code, and prepublishOnly runs
lint, typecheck, tests and the build first. A version can be published once; the next needs a bump.
From Jenkins, first:
- On npmjs.com: a granular access token with read and write on
@ist-group(or this package alone once it exists) and Bypass 2FA on. Write tokens live 90 days at most, so it is renewed each quarter. - In Jenkins: a credential, kind Secret text, holding that token.
- In
Jenkinsfile,environment:NPM_CREDset to that credential's id. While it is empty, Publish fails saying so. - In the job: tag discovery on (docs/jenkins-setup.md, Tags) if publishing should follow tags.
Then publish by pushing a tag pdf-viewer-v<version> for the version in package.json, or by running the
branch's build with Publish ticked.
