Skip to content

Commit c9c9aa6

Browse files
committed
[minor] support passing arbitrary pdfjs-dist options
related to #12
1 parent d95e232 commit c9c9aa6

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

src/read-pdf.test.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1+
import {assertTypeOf} from 'run-time-assertions';
12
import {assert} from 'chai';
23
import {existsSync} from 'fs';
34
import {join} from 'path';
4-
import {ReadonlyDeep} from 'type-fest';
5-
import {PdfProgressData, readPdfPages, readPdfText} from './read-pdf';
5+
import type {ReadonlyDeep} from 'type-fest';
6+
import {PdfProgressData, ReadPdfTextParams, readPdfPages, readPdfText} from './read-pdf';
67
import {nodeModulesDir, sampleFilesDir} from './repo-paths.test-helper';
8+
import type {TypedArray} from 'pdfjs-dist/types/src/display/api';
79

810
type PdfTestFile = {
911
filePath: string;
@@ -112,6 +114,9 @@ describe(readPdfPages.name, () => {
112114
progressCallback(progressData) {
113115
allProgressData.push(progressData);
114116
},
117+
options: {
118+
isEvalSupported: false,
119+
},
115120
});
116121

117122
assert.isAbove(allProgressData.length, 0, 'got no progress data');
@@ -142,3 +147,13 @@ describe(readPdfText.name, () => {
142147
});
143148
});
144149
});
150+
151+
describe('ReadPdfTextParams', () => {
152+
it('matches expected types', () => {
153+
assertTypeOf<Required<ReadPdfTextParams>['data']>().toEqualTypeOf<
154+
string | number[] | ArrayBuffer | TypedArray
155+
>();
156+
assertTypeOf<Required<ReadPdfTextParams>['url']>().toEqualTypeOf<string | URL>();
157+
assertTypeOf<Required<ReadPdfTextParams>['filePath']>().toEqualTypeOf<string | URL>();
158+
});
159+
});

src/read-pdf.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ export type ReadPdfTextParams = PartialWithUndefined<{
4848
* Example: /home/ubuntu/this-repo/node_modules/pdfjs-dist
4949
*/
5050
pathToPdfJsDistNodeModule: string;
51+
/**
52+
* All options that the Mozilla's `pdfjs-dist` package supports. This will override any options
53+
* that this package passes to `pdfjs-dist`.
54+
*/
55+
options: Partial<Omit<DocumentInitParameters, 'data' | 'url'>>;
5156
}> &
5257
RequireExactlyOne<{
5358
/** File path to the PDF file to read. */
54-
filePath: string;
59+
filePath: NonNullable<DocumentInitParameters['url']>;
5560
/** URL to the PDF. */
56-
url: string;
61+
url: NonNullable<DocumentInitParameters['url']>;
5762
/** PDF file data that has already been read from a PDF file. */
58-
data: BinaryData;
59-
/** All other options that the Mozilla `pdfjs-dist` package supports. */
60-
allOptions: DocumentInitParameters;
63+
data: NonNullable<DocumentInitParameters['data']>;
6164
}>;
6265

6366
/**
@@ -73,6 +76,7 @@ export async function readPdfPages({
7376
pathToPdfJsDistNodeModule,
7477
progressCallback,
7578
url,
79+
options,
7680
}: ReadPdfTextParams): Promise<PdfPage[]> {
7781
const documentLoadingTask = getDocument({
7882
data,
@@ -82,6 +86,7 @@ export async function readPdfPages({
8286
standardFontDataUrl: pathToPdfJsDistNodeModule
8387
? join(pathToPdfJsDistNodeModule, 'standard_fonts')
8488
: undefined,
89+
...options,
8590
});
8691
if (progressCallback) {
8792
documentLoadingTask.onProgress = progressCallback;

0 commit comments

Comments
 (0)