pdf-js-wrapper.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import { captureMessage } from '../../../infrastructure/error-reporter'
  2. const params = new URLSearchParams(window.location.search)
  3. const disableFontFace = params.get('disable-font-face') === 'true'
  4. const disableStream = process.env.NODE_ENV !== 'test'
  5. const rangeChunkSize = 128 * 1024 // 128K chunks
  6. export default class PDFJSWrapper {
  7. constructor(container) {
  8. this.container = container
  9. }
  10. async init() {
  11. const { PDFJS, PDFJSViewer, PDFJSWorker, cMapUrl, imageResourcesPath } =
  12. await import('./pdf-js-versions').then(m => m.default)
  13. this.PDFJS = PDFJS
  14. this.PDFJSViewer = PDFJSViewer
  15. this.PDFJSWorker = PDFJSWorker
  16. this.cMapUrl = cMapUrl
  17. this.imageResourcesPath = imageResourcesPath
  18. // create the event bus
  19. const eventBus = new PDFJSViewer.EventBus()
  20. // create the link service
  21. const linkService = new PDFJSViewer.PDFLinkService({
  22. eventBus,
  23. externalLinkTarget: 2,
  24. externalLinkRel: 'noopener',
  25. })
  26. // create the localization
  27. // const l10n = new PDFJSViewer.GenericL10n('en-GB') // TODO: locale mapping?
  28. // create the viewer
  29. const viewer = new PDFJSViewer.PDFViewer({
  30. container: this.container,
  31. eventBus,
  32. imageResourcesPath,
  33. linkService,
  34. // l10n, // commented out since it currently breaks `aria-label` rendering in pdf pages
  35. enableScripting: false, // default is false, but set explicitly to be sure
  36. enableXfa: false, // default is false (2021-10-12), but set explicitly to be sure
  37. renderInteractiveForms: false,
  38. maxCanvasPixels: 8192 * 8192, // default is 4096 * 4096, increased for better resolution at high zoom levels
  39. })
  40. linkService.setViewer(viewer)
  41. this.eventBus = eventBus
  42. this.linkService = linkService
  43. this.viewer = viewer
  44. }
  45. // load a document from a URL
  46. loadDocument(url) {
  47. // cancel any previous loading task
  48. if (this.loadDocumentTask) {
  49. this.loadDocumentTask.destroy()
  50. this.loadDocumentTask = undefined
  51. }
  52. return new Promise((resolve, reject) => {
  53. this.loadDocumentTask = this.PDFJS.getDocument({
  54. url,
  55. cMapUrl: this.cMapUrl,
  56. cMapPacked: true,
  57. disableFontFace,
  58. rangeChunkSize,
  59. disableAutoFetch: true,
  60. disableStream,
  61. textLayerMode: 2, // PDFJSViewer.TextLayerMode.ENABLE,
  62. })
  63. this.loadDocumentTask.promise
  64. .then(doc => {
  65. if (!this.loadDocumentTask) {
  66. return // ignoring the response since loading task has been aborted
  67. }
  68. const previousDoc = this.viewer.pdfDocument
  69. this.viewer.setDocument(doc)
  70. this.linkService.setDocument(doc)
  71. resolve(doc)
  72. if (previousDoc) {
  73. previousDoc.cleanup().catch(console.error)
  74. previousDoc.destroy()
  75. }
  76. })
  77. .catch(error => {
  78. if (this.loadDocumentTask) {
  79. if (!error || error.name !== 'MissingPDFException') {
  80. captureMessage(`pdf preview error ${error}`)
  81. }
  82. reject(error)
  83. }
  84. })
  85. .finally(() => {
  86. this.loadDocumentTask = undefined
  87. })
  88. })
  89. }
  90. // update the current scale value if the container size changes
  91. updateOnResize() {
  92. if (!this.isVisible()) {
  93. return
  94. }
  95. const currentScaleValue = this.viewer.currentScaleValue
  96. if (
  97. currentScaleValue === 'auto' ||
  98. currentScaleValue === 'page-fit' ||
  99. currentScaleValue === 'page-width'
  100. ) {
  101. this.viewer.currentScaleValue = currentScaleValue
  102. }
  103. this.viewer.update()
  104. }
  105. // get the page and offset of a click event
  106. clickPosition(event, pageElement, textLayer) {
  107. const { viewport } = this.viewer.getPageView(textLayer.pageNumber - 1)
  108. const pageRect = pageElement.querySelector('canvas').getBoundingClientRect()
  109. const dx = event.clientX - pageRect.left
  110. const dy = event.clientY - pageRect.top
  111. const [left, top] = viewport.convertToPdfPoint(dx, dy)
  112. return {
  113. page: textLayer.pageNumber - 1,
  114. offset: {
  115. left,
  116. top: viewport.viewBox[3] - top,
  117. },
  118. }
  119. }
  120. // get the current page, offset and page size
  121. get currentPosition() {
  122. const pageIndex = this.viewer.currentPageNumber - 1
  123. const pageView = this.viewer.getPageView(pageIndex)
  124. const pageRect = pageView.div.getBoundingClientRect()
  125. const containerRect = this.container.getBoundingClientRect()
  126. const dy = containerRect.top - pageRect.top
  127. const dx = containerRect.left - pageRect.left
  128. const [left, top] = pageView.viewport.convertToPdfPoint(dx, dy)
  129. const [, , width, height] = pageView.viewport.viewBox
  130. return {
  131. page: pageIndex,
  132. offset: { top, left },
  133. pageSize: { height, width },
  134. }
  135. }
  136. scrollToPosition(position, scale = null) {
  137. const destArray = [
  138. null,
  139. {
  140. name: 'XYZ', // 'XYZ' = scroll to the given coordinates
  141. },
  142. position.offset.left,
  143. position.offset.top,
  144. scale,
  145. ]
  146. this.viewer.scrollPageIntoView({
  147. pageNumber: position.page + 1,
  148. destArray,
  149. })
  150. // scroll the page down by an extra few pixels to account for the pdf.js viewer page border
  151. this.viewer.container.scrollBy({
  152. top: -9,
  153. })
  154. }
  155. isVisible() {
  156. return this.viewer.container.offsetParent !== null
  157. }
  158. abortDocumentLoading() {
  159. this.loadDocumentTask = undefined
  160. }
  161. destroy() {
  162. if (this.loadDocumentTask) {
  163. this.loadDocumentTask.destroy()
  164. this.loadDocumentTask = undefined
  165. }
  166. if (this.viewer) {
  167. this.viewer.destroy()
  168. }
  169. }
  170. }