pdf-js-versions.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // To add a new version, copy and adjust one of the `importPDFJS*` functions below,
  2. // add the variant to the "switch" statement, and add to `pdfjsVersions` in webpack.config.js
  3. import 'core-js/stable/global-this' // polyfill for globalThis (used by pdf.js)
  4. import 'core-js/stable/promise/all-settled' // polyfill for Promise.allSettled (used by pdf.js)
  5. import getMeta from '@/utils/meta'
  6. import { createWorker } from '@/utils/worker'
  7. async function importPDFJS401() {
  8. const cMapUrl = '/js/pdfjs-dist401/cmaps/'
  9. const standardFontDataUrl = '/fonts/pdfjs-dist401/'
  10. const imageResourcesPath = '/images/pdfjs-dist401/'
  11. // ensure that PDF.js is loaded before importing the viewer
  12. const PDFJS = await import('pdfjs-dist401/legacy/build/pdf')
  13. const [PDFJSViewer] = await Promise.all([
  14. import('pdfjs-dist401/legacy/web/pdf_viewer'),
  15. import('pdfjs-dist401/legacy/web/pdf_viewer.css'),
  16. ])
  17. createWorker(() => {
  18. PDFJS.GlobalWorkerOptions.workerPort = new Worker(
  19. new URL('pdfjs-dist401/legacy/build/pdf.worker.mjs', import.meta.url) // NOTE: .mjs extension
  20. )
  21. })
  22. return {
  23. PDFJS,
  24. PDFJSViewer,
  25. cMapUrl,
  26. imageResourcesPath,
  27. standardFontDataUrl,
  28. }
  29. }
  30. async function importPDFJS213() {
  31. const cMapUrl = '/js/pdfjs-dist213/cmaps/'
  32. const standardFontDataUrl = '/fonts/pdfjs-dist213/'
  33. const imageResourcesPath = '/images/pdfjs-dist213/'
  34. // ensure that PDF.js is loaded before importing the viewer
  35. const PDFJS = await import('pdfjs-dist213/legacy/build/pdf')
  36. const [PDFJSViewer] = await Promise.all([
  37. import('pdfjs-dist213/legacy/web/pdf_viewer'),
  38. import('pdfjs-dist213/legacy/web/pdf_viewer.css'),
  39. ])
  40. createWorker(() => {
  41. PDFJS.GlobalWorkerOptions.workerPort = new Worker(
  42. new URL('pdfjs-dist213/legacy/build/pdf.worker.js', import.meta.url)
  43. )
  44. })
  45. return {
  46. PDFJS,
  47. PDFJSViewer,
  48. cMapUrl,
  49. imageResourcesPath,
  50. standardFontDataUrl,
  51. }
  52. }
  53. async function importPDFJS() {
  54. const variant = getMeta('ol-pdfjsVariant', 'default')
  55. // NOTE: split test variants must have at least 3 characters
  56. switch (variant) {
  57. case '213':
  58. case 'default':
  59. return importPDFJS213()
  60. case '401':
  61. return importPDFJS401()
  62. }
  63. }
  64. export default importPDFJS()