From 0111f45b58828255ec23defe762f2981f9cc00a5 Mon Sep 17 00:00:00 2001 From: Marc Glasser Date: Mon, 12 Dec 2022 14:35:33 -1000 Subject: [PATCH] Load pdfjs webworker script from source --- config/webpack/webpack.common.js | 7 +++++++ src/components/PDFView/index.js | 6 +++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/config/webpack/webpack.common.js b/config/webpack/webpack.common.js index ef1f46e8383f..b414846988a4 100644 --- a/config/webpack/webpack.common.js +++ b/config/webpack/webpack.common.js @@ -142,6 +142,13 @@ const webpackConfig = ({envFile = '.env', platform = 'web'}) => ({ ], }, + // We are importing this worker as a string by using asset/source otherwise it will default to loading via an HTTPS request later. + // This causes issues if we have gone offline before the pdfjs web worker is set up as we won't be able to load it from the server. + { + test: new RegExp('node_modules/pdfjs-dist/legacy/build/pdf.worker.js'), + type: 'asset/source', + }, + // Rule for react-native-web-webview { test: /postMock.html$/, diff --git a/src/components/PDFView/index.js b/src/components/PDFView/index.js index 485b40c1e983..f44baa00c500 100644 --- a/src/components/PDFView/index.js +++ b/src/components/PDFView/index.js @@ -1,7 +1,8 @@ import _ from 'underscore'; import React, {Component} from 'react'; import {View, Dimensions} from 'react-native'; -import {Document, Page} from 'react-pdf/dist/esm/entry.webpack'; +import {Document, Page, pdfjs} from 'react-pdf/dist/esm/entry.webpack'; +import pdfWorkerSource from 'pdfjs-dist/legacy/build/pdf.worker'; import FullScreenLoadingIndicator from '../FullscreenLoadingIndicator'; import styles from '../../styles/styles'; import variables from '../../styles/variables'; @@ -27,6 +28,9 @@ class PDFView extends Component { this.initiatePasswordChallenge = this.initiatePasswordChallenge.bind(this); this.attemptPDFLoad = this.attemptPDFLoad.bind(this); this.toggleKeyboardOnSmallScreens = this.toggleKeyboardOnSmallScreens.bind(this); + + const workerBlob = new Blob([pdfWorkerSource], {type: 'text/javascript'}); + pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(workerBlob); } componentDidUpdate(prevProps) {