pdf-js.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import 'core-js/stable/global-this' // polyfill for globalThis (used by pdf.js)
  2. import 'core-js/stable/promise/all-settled' // polyfill for Promise.allSettled (used by pdf.js)
  3. import 'core-js/stable/structured-clone' // polyfill for global.StructuredClone (used by pdf.js)
  4. import 'core-js/stable/array/at' // polyfill for Array.prototype.at (used by pdf.js)
  5. import { createWorker } from '@/utils/worker'
  6. import * as PDFJS from 'pdfjs-dist/legacy/build/pdf.mjs'
  7. import type { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api'
  8. export { PDFJS }
  9. createWorker(() => {
  10. PDFJS.GlobalWorkerOptions.workerPort = new Worker(
  11. new URL('pdfjs-dist/legacy/build/pdf.worker.mjs', import.meta.url) // NOTE: .mjs extension
  12. )
  13. })
  14. export const imageResourcesPath = '/images/pdfjs-dist/'
  15. const cMapUrl = '/js/pdfjs-dist/cmaps/'
  16. const standardFontDataUrl = '/fonts/pdfjs-dist/'
  17. const params = new URLSearchParams(window.location.search)
  18. const disableFontFace = params.get('disable-font-face') === 'true'
  19. const disableStream = process.env.NODE_ENV !== 'test'
  20. export const loadPdfDocumentFromUrl = (
  21. url: string,
  22. options: Partial<DocumentInitParameters> = {}
  23. ) =>
  24. PDFJS.getDocument({
  25. url,
  26. cMapUrl,
  27. standardFontDataUrl,
  28. disableFontFace,
  29. disableAutoFetch: true, // only fetch the data needed for the displayed pages
  30. disableStream,
  31. isEvalSupported: false,
  32. enableXfa: false, // default is false (2021-10-12), but set explicitly to be sure
  33. ...options,
  34. })