Skip to content

Commit

Permalink
[minor] support passing arbitrary pdfjs-dist options
Browse files Browse the repository at this point in the history
related to #12
  • Loading branch information
electrovir committed May 8, 2024
1 parent d95e232 commit c9c9aa6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
19 changes: 17 additions & 2 deletions src/read-pdf.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {assertTypeOf} from 'run-time-assertions';
import {assert} from 'chai';
import {existsSync} from 'fs';
import {join} from 'path';
import {ReadonlyDeep} from 'type-fest';
import {PdfProgressData, readPdfPages, readPdfText} from './read-pdf';
import type {ReadonlyDeep} from 'type-fest';
import {PdfProgressData, ReadPdfTextParams, readPdfPages, readPdfText} from './read-pdf';
import {nodeModulesDir, sampleFilesDir} from './repo-paths.test-helper';
import type {TypedArray} from 'pdfjs-dist/types/src/display/api';

type PdfTestFile = {
filePath: string;
Expand Down Expand Up @@ -112,6 +114,9 @@ describe(readPdfPages.name, () => {
progressCallback(progressData) {
allProgressData.push(progressData);
},
options: {
isEvalSupported: false,
},
});

assert.isAbove(allProgressData.length, 0, 'got no progress data');
Expand Down Expand Up @@ -142,3 +147,13 @@ describe(readPdfText.name, () => {
});
});
});

describe('ReadPdfTextParams', () => {
it('matches expected types', () => {
assertTypeOf<Required<ReadPdfTextParams>['data']>().toEqualTypeOf<
string | number[] | ArrayBuffer | TypedArray
>();
assertTypeOf<Required<ReadPdfTextParams>['url']>().toEqualTypeOf<string | URL>();
assertTypeOf<Required<ReadPdfTextParams>['filePath']>().toEqualTypeOf<string | URL>();
});
});
15 changes: 10 additions & 5 deletions src/read-pdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,19 @@ export type ReadPdfTextParams = PartialWithUndefined<{
* Example: /home/ubuntu/this-repo/node_modules/pdfjs-dist
*/
pathToPdfJsDistNodeModule: string;
/**
* All options that the Mozilla's `pdfjs-dist` package supports. This will override any options
* that this package passes to `pdfjs-dist`.
*/
options: Partial<Omit<DocumentInitParameters, 'data' | 'url'>>;
}> &
RequireExactlyOne<{
/** File path to the PDF file to read. */
filePath: string;
filePath: NonNullable<DocumentInitParameters['url']>;
/** URL to the PDF. */
url: string;
url: NonNullable<DocumentInitParameters['url']>;
/** PDF file data that has already been read from a PDF file. */
data: BinaryData;
/** All other options that the Mozilla `pdfjs-dist` package supports. */
allOptions: DocumentInitParameters;
data: NonNullable<DocumentInitParameters['data']>;
}>;

/**
Expand All @@ -73,6 +76,7 @@ export async function readPdfPages({
pathToPdfJsDistNodeModule,
progressCallback,
url,
options,
}: ReadPdfTextParams): Promise<PdfPage[]> {
const documentLoadingTask = getDocument({
data,
Expand All @@ -82,6 +86,7 @@ export async function readPdfPages({
standardFontDataUrl: pathToPdfJsDistNodeModule
? join(pathToPdfJsDistNodeModule, 'standard_fonts')
: undefined,
...options,
});
if (progressCallback) {
documentLoadingTask.onProgress = progressCallback;
Expand Down

0 comments on commit c9c9aa6

Please sign in to comment.