pdf-js-wrapper.js 5.5 KB

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