pdf-js.ts 1.1 KB

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