pdf-js.ts 1.0 KB

123456789101112131415161718192021222324252627282930313233
  1. import * as PDFJS from 'pdfjs-dist'
  2. import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api'
  3. export { PDFJS }
  4. PDFJS.GlobalWorkerOptions.workerPort = new Worker(
  5. /* webpackChunkName: "pdf-worker" */
  6. new URL('pdfjs-dist/build/pdf.worker.mjs', import.meta.url) // NOTE: .mjs extension
  7. )
  8. export const imageResourcesPath = '/images/pdfjs-dist/'
  9. const cMapUrl = '/js/pdfjs-dist/cmaps/'
  10. const standardFontDataUrl = '/fonts/pdfjs-dist/'
  11. const params = new URLSearchParams(window.location.search)
  12. const disableFontFace = params.get('disable-font-face') === 'true'
  13. const disableStream = process.env.NODE_ENV !== 'test'
  14. export const loadPdfDocumentFromUrl = (
  15. url: string,
  16. options: Partial<DocumentInitParameters> = {}
  17. ) =>
  18. PDFJS.getDocument({
  19. url,
  20. cMapUrl,
  21. standardFontDataUrl,
  22. disableFontFace,
  23. disableAutoFetch: true, // only fetch the data needed for the displayed pages
  24. disableStream,
  25. isEvalSupported: false,
  26. enableXfa: false, // default is false (2021-10-12), but set explicitly to be sure
  27. ...options,
  28. })