diff --git a/core/src/PdfParser.ts b/core/src/PdfParser.ts index 28f2766..ce8c22b 100644 --- a/core/src/PdfParser.ts +++ b/core/src/PdfParser.ts @@ -10,13 +10,25 @@ export default class PdfParser { this.pdfjs = pdfjs; } - async parse(data: Uint8Array): Promise { + async parseBytes(data: Uint8Array): Promise { + return this.parse(this.params({ data })); + } + + async parseUrl(url: string): Promise { + return this.parse(this.params({ url })); + } + + private params(dataSourceParams: object): object { + const defaultParams = { + cMapUrl: 'cmaps/', + cMapPacked: true, + }; + return { ...defaultParams, ...dataSourceParams }; + } + + async parse(parameter: object): Promise { return this.pdfjs - .getDocument({ - data, - cMapUrl: 'cmaps/', - cMapPacked: true, - }) + .getDocument(parameter) .promise.then((pdfDocument) => { return Promise.all([pdfDocument.getMetadata(), this.extractPagesSequentially(pdfDocument)]); }) diff --git a/core/test/PdfParser.test.ts b/core/test/PdfParser.test.ts index 296f1a2..4804d44 100644 --- a/core/test/PdfParser.test.ts +++ b/core/test/PdfParser.test.ts @@ -6,7 +6,7 @@ const parser = new PdfParser(pdfjs); test('testIt', async () => { const data = fs.readFileSync('../examples/ExamplePdf.pdf', null); - const result = await parser.parse(data); + const result = await parser.parseBytes(data); expect(result.metadata.title()).toEqual('ExamplePdf'); expect(result.metadata.author()).toEqual('Johannes Zillmann'); expect(result.pages.length).toBe(7); diff --git a/ui/public/ExamplePdf.pdf b/ui/public/ExamplePdf.pdf new file mode 100644 index 0000000..0ed0b9e Binary files /dev/null and b/ui/public/ExamplePdf.pdf differ diff --git a/ui/src/Table.svelte b/ui/src/Table.svelte index a23abf3..674122f 100644 --- a/ui/src/Table.svelte +++ b/ui/src/Table.svelte @@ -1,5 +1,5 @@ + +
+
+ Load Example +
+
Debug
+
+ +
{#await upload} -
Parsing {specifiedFile.name}...
+
Parsing {specifiedFileName}...
{:catch error} -
Failed to parse '{specifiedFile.name}': {error.message}
+
Failed to parse '{specifiedFileName}': {error.message}
{/await} {#if rejectionError}
{rejectionError}
diff --git a/ui/src/store.ts b/ui/src/store.ts index cef5dee..0efba6c 100644 --- a/ui/src/store.ts +++ b/ui/src/store.ts @@ -12,7 +12,11 @@ pdfjs.GlobalWorkerOptions.workerSrc = pdfjsWorker; const parser = pdfParser(pdfjs); -export function processUpload(file: File): Promise { +export async function loadExample(): Promise { + return parsePdf(parser.parseUrl('/ExamplePdf.pdf')); +} + +export async function processUpload(file: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onerror = reject; @@ -20,13 +24,15 @@ export function processUpload(file: File): Promise { resolve(reader.result as ArrayBuffer); }; reader.readAsArrayBuffer(file); - }) - .then((buffer) => { - const uintArray = new Uint8Array(buffer as ArrayBuffer); - return parser.parse(uintArray); - }) - .then((result) => { - parseResult.set(result); - return result; - }); + }).then((buffer) => { + const data = new Uint8Array(buffer as ArrayBuffer); + return parsePdf(parser.parseBytes(data)); + }); +} + +async function parsePdf(parsePromise: Promise): Promise { + return parsePromise.then((result) => { + parseResult.set(result); + return result; + }); } diff --git a/ui/tailwind.config.js b/ui/tailwind.config.js index d6662b6..0ab0c88 100644 --- a/ui/tailwind.config.js +++ b/ui/tailwind.config.js @@ -9,6 +9,8 @@ module.exports = { theme: { extend: {}, }, - variants: {}, + variants: { + extend: {}, + }, plugins: [], };