Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support version specific pdf.worker.js url #971

Merged
merged 2 commits into from
Feb 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,17 @@ In your code update `path` to the worker to be for example `/pdf.worker.js`
```typescript
(window as any).pdfWorkerSrc = '/pdf.worker.js';
```

*This should be set before `pdf-viewer` component is rendered.*

If you ever have a (super rare) edge case where you run in an environment that multiple
components are somehow loaded within the same web page, sharing the same window,
but using different versions of pdf.worker, support has been added. You can do the
above, except that you can append the specific version of pdfjs required and override the
custom path *just for that version*. This way setting the global window var won't conflict.
```typescript
(window as any)["pdfWorkerSrc2.14.305"] = '/pdf.worker.js';
```

## Search in the PDF

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build --configuration production=true",
"build": "ng build --configuration production",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this value of "production" should match what's in angular.json. not sure how this worked before this change with the latest version of angular. if i'm somehow out of sync on angular versions or something, feel free to discard this change.

"build:stats": "ng build --stats-json",
"analyze": "webpack-bundle-analyzer dist/stats.json",
"test": "ng test --watch false",
Expand Down
50 changes: 50 additions & 0 deletions src/app/pdf-viewer/pdf-viewer.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { Component } from '@angular/core';
import { PdfViewerComponent } from './pdf-viewer.component';
import { PdfViewerModule } from './pdf-viewer.module';

import { GlobalWorkerOptions } from 'pdfjs-dist';
import * as PDFJS from 'pdfjs-dist/build/pdf';

@Component({
template: `
<pdf-viewer></pdf-viewer>
Expand Down Expand Up @@ -140,4 +143,51 @@ describe('AppComponent', () => {
});
});
});

describe('pdf.worker location', () => {
const curPdfJsVersion = (PDFJS as any).version;

beforeEach(() => {
(window as any).pdfWorkerSrc = undefined;
(window as any)["pdfWorkerSrc1.2.3"] = undefined;
(window as any)[`pdfWorkerSrc${curPdfJsVersion}`] = undefined;

});

it('should default to the cdn', () => {
pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;

expect(GlobalWorkerOptions.workerSrc).toBe(`https://cdn.jsdelivr.net/npm/pdfjs-dist@${curPdfJsVersion
}/legacy/build/pdf.worker.min.js`);
})

it('should support global override', () => {
(window as any).pdfWorkerSrc = 'globaloverride';

pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;

expect(GlobalWorkerOptions.workerSrc).toBe('globaloverride');
})

it('should default to the cdn when version override does not match version', () => {
(window as any)["pdfWorkerSrc1.2.3"] = 'globaloverride';

pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;

expect(GlobalWorkerOptions.workerSrc).toBe(`https://cdn.jsdelivr.net/npm/pdfjs-dist@${curPdfJsVersion
}/legacy/build/pdf.worker.min.js`);
})

it('should take version override with version match', () => {
(window as any)[`pdfWorkerSrc${curPdfJsVersion}`] = 'globaloverride';

pdfViewerFixture = TestBed.createComponent(PdfViewerComponent);
pdfViewer = pdfViewerFixture.debugElement.componentInstance;

expect(GlobalWorkerOptions.workerSrc).toBe(`globaloverride`);
})
})
});
9 changes: 7 additions & 2 deletions src/app/pdf-viewer/pdf-viewer.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,14 +227,19 @@ export class PdfViewerComponent

let pdfWorkerSrc: string;

if (
const pdfJsVersion = (PDFJS as any).version;
const versionSpecificPdfWorkerUrl = window[`pdfWorkerSrc${pdfJsVersion}`];

if (versionSpecificPdfWorkerUrl) {
pdfWorkerSrc = versionSpecificPdfWorkerUrl;
} else if (
window.hasOwnProperty('pdfWorkerSrc') &&
typeof (window as any).pdfWorkerSrc === 'string' &&
(window as any).pdfWorkerSrc
) {
pdfWorkerSrc = (window as any).pdfWorkerSrc;
} else {
pdfWorkerSrc = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${(PDFJS as any).version
pdfWorkerSrc = `https://cdn.jsdelivr.net/npm/pdfjs-dist@${pdfJsVersion
}/legacy/build/pdf.worker.min.js`;
}

Expand Down