AAE-34470 Ignore content type of fetched pdfjs worker (#10818)

* AAE-34470 Ignore content type of fetched pdfjs worker

* AAE-34470 Fix units

* AAE-34470 Harness instances of pdfjs worker

* AAE-34470 Harness instances of pdfjs worker

* AAE-34470 Clean docs up

* AAE-34470 Synchronize ngOnChanges

* AAE-34470 Flush not tick

* AAE-34470 Revert modern bundle for pdf_viewer.mjs
This commit is contained in:
Wojciech Duda 2025-04-24 17:18:11 +02:00 committed by GitHub
parent 7b67c7cf3a
commit 3d598cd0c2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 28 deletions

View File

@ -186,10 +186,9 @@ npm install pdfjs-dist
```ts ```ts
// PDF.js // PDF.js
require('pdfjs-dist/web/compatibility.js');
const pdfjsLib = require('pdfjs-dist'); const pdfjsLib = require('pdfjs-dist');
pdfjsLib.PDFJS.workerSrc = './pdf.worker.js'; pdfjsLib.PDFJS.workerSrc = './pdf.worker.min.mjs';
require('pdfjs-dist/web/pdf_viewer.js'); require('pdfjs-dist/web/pdf_viewer.mjs');
``` ```
- Update the `plugins` section of the `webpack.common.js` file with the following lines: - Update the `plugins` section of the `webpack.common.js` file with the following lines:
@ -198,8 +197,8 @@ require('pdfjs-dist/web/pdf_viewer.js');
new CopyWebpackPlugin([ new CopyWebpackPlugin([
... ...
{ {
from: 'node_modules/pdfjs-dist/build/pdf.worker.js', from: 'node_modules/pdfjs-dist/build/pdf.worker.min.mjs',
to: 'pdf.worker.js' to: 'pdf.worker.min.mjs'
} }
]) ])
``` ```

View File

@ -451,6 +451,8 @@ describe('Test PdfViewer - User interaction', () => {
const appConfig: AppConfigService = TestBed.inject(AppConfigService); const appConfig: AppConfigService = TestBed.inject(AppConfigService);
appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10; appConfig.config['adf-viewer.pdf-viewer-scaling'] = 10;
component['setupPdfJsWorker'] = () => Promise.resolve();
component.urlFile = './fake-test-file.pdf'; component.urlFile = './fake-test-file.pdf';
fixture.detectChanges(); fixture.detectChanges();
component.ngOnChanges({ urlFile: { currentValue: './fake-test-file.pdf' } } as any); component.ngOnChanges({ urlFile: { currentValue: './fake-test-file.pdf' } } as any);

View File

@ -47,7 +47,7 @@ import { RenderingQueueServices } from '../../services/rendering-queue.services'
import { PdfPasswordDialogComponent } from '../pdf-viewer-password-dialog/pdf-viewer-password-dialog'; import { PdfPasswordDialogComponent } from '../pdf-viewer-password-dialog/pdf-viewer-password-dialog';
import { PdfThumbListComponent } from '../pdf-viewer-thumbnails/pdf-viewer-thumbnails.component'; import { PdfThumbListComponent } from '../pdf-viewer-thumbnails/pdf-viewer-thumbnails.component';
import * as pdfjsLib from 'pdfjs-dist/build/pdf.min.mjs'; import * as pdfjsLib from 'pdfjs-dist/build/pdf.min.mjs';
import { PDFViewer, EventBus } from 'pdfjs-dist/web/pdf_viewer.mjs'; import { EventBus, PDFViewer } from 'pdfjs-dist/web/pdf_viewer.mjs';
import { OnProgressParameters, PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api'; import { OnProgressParameters, PDFDocumentLoadingTask, PDFDocumentProxy } from 'pdfjs-dist/types/src/display/api';
export type PdfScaleMode = 'init' | 'page-actual' | 'page-width' | 'page-height' | 'page-fit' | 'auto'; export type PdfScaleMode = 'init' | 'page-actual' | 'page-width' | 'page-height' | 'page-fit' | 'auto';
@ -115,6 +115,8 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
totalPages: number; totalPages: number;
loadingPercent: number; loadingPercent: number;
pdfViewer: any; pdfViewer: any;
pdfJsWorkerUrl: string;
pdfJsWorkerInstance: Worker;
currentScaleMode: PdfScaleMode = 'init'; currentScaleMode: PdfScaleMode = 'init';
MAX_AUTO_SCALE: number = 1.25; MAX_AUTO_SCALE: number = 1.25;
@ -159,7 +161,7 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
this.pdfjsWorkerDestroy$ this.pdfjsWorkerDestroy$
.pipe( .pipe(
catchError(() => null), catchError(() => null),
switchMap(() => from(this.destroyPdJsWorker())) switchMap(() => from(this.destroyPfdJsWorker()))
) )
.subscribe(() => {}); .subscribe(() => {});
} }
@ -224,8 +226,7 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
} }
executePdf(pdfOptions: any) { executePdf(pdfOptions: any) {
this.pdfjsLib.GlobalWorkerOptions.workerSrc = 'pdf.worker.min.mjs'; this.setupPdfJsWorker().then(() => {
this.loadingTask = this.pdfjsLib.getDocument(pdfOptions); this.loadingTask = this.pdfjsLib.getDocument(pdfOptions);
this.loadingTask.onPassword = (callback, reason) => { this.loadingTask.onPassword = (callback, reason) => {
@ -250,6 +251,24 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
}) })
.then(() => this.scalePage('init')) .then(() => this.scalePage('init'))
.catch(() => this.error.emit()); .catch(() => this.error.emit());
});
}
private async setupPdfJsWorker(): Promise<void> {
if (this.pdfJsWorkerInstance) {
await this.destroyPfdJsWorker();
} else if (!this.pdfJsWorkerUrl) {
this.pdfJsWorkerUrl = await this.getPdfJsWorker();
}
this.pdfJsWorkerInstance = new Worker(this.pdfJsWorkerUrl, { type: 'module' });
this.pdfjsLib.GlobalWorkerOptions.workerPort = this.pdfJsWorkerInstance;
}
private async getPdfJsWorker(): Promise<string> {
const response = await fetch('./pdf.worker.min.mjs');
const workerScript = await response.text();
const blob = new Blob([workerScript], { type: 'application/javascript' });
return URL.createObjectURL(blob);
} }
initPDFViewer(pdfDocument: PDFDocumentProxy) { initPDFViewer(pdfDocument: PDFDocumentProxy) {
@ -297,15 +316,23 @@ export class PdfViewerComponent implements OnChanges, OnDestroy {
this.pdfjsWorkerDestroy$.next(true); this.pdfjsWorkerDestroy$.next(true);
} }
this.pdfjsWorkerDestroy$.complete(); this.pdfjsWorkerDestroy$.complete();
this.revokePdfJsWorkerUrl();
} }
private async destroyPdJsWorker() { private async destroyPfdJsWorker() {
if (this.loadingTask.destroy) { if (this.loadingTask.destroy) {
await this.loadingTask.destroy(); await this.loadingTask.destroy();
} }
if (this.pdfJsWorkerInstance) {
this.pdfJsWorkerInstance.terminate();
}
this.loadingTask = null; this.loadingTask = null;
} }
private revokePdfJsWorkerUrl(): void {
URL.revokeObjectURL(this.pdfJsWorkerUrl);
}
toggleThumbnails() { toggleThumbnails() {
this.showThumbnails = !this.showThumbnails; this.showThumbnails = !this.showThumbnails;
} }