From 1d62a8dfe31edd3984119692ed6006e03bb9b877 Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Wed, 2 Mar 2022 15:47:35 +0200 Subject: [PATCH] multipage support --- dist/pdfParser.js | 30 ++++++++++++++++-------------- src/pdfParser.ts | 30 ++++++++++++++++-------------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/dist/pdfParser.js b/dist/pdfParser.js index 0cd75db..6b7915b 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -51,20 +51,22 @@ class PDFparser { const img = yield pdfDoc.embedPng(imgBytes); const scaled = img.scale(0.2); const pages = pdfDoc.getPages(); - const firstPage = pages[0]; - firstPage.drawImage(img, { - x: firstPage.getWidth() / 2 - scaled.width / 2, - y: firstPage.getHeight() / 2 - scaled.height / 2, - width: scaled.width, - height: scaled.height, - }); - const link = this.createPageLinkAnnotation(firstPage, url, { - imgXPos: firstPage.getWidth() / 2 - scaled.width / 2, - imgYPos: firstPage.getHeight() / 2 - scaled.height / 2, - imgWidth: scaled.width, - imagHeight: scaled.height, - }); - firstPage.node.set(pdf_lib_1.PDFName.of("Annots"), pdfDoc.context.obj([link])); + for (let index = 0; index < pages.length; index++) { + const page = pages[index]; + page.drawImage(img, { + x: page.getWidth() / 2 - scaled.width / 2, + y: page.getHeight() / 2 - scaled.height / 2, + width: scaled.width, + height: scaled.height, + }); + const link = this.createPageLinkAnnotation(page, url, { + imgXPos: page.getWidth() / 2 - scaled.width / 2, + imgYPos: page.getHeight() / 2 - scaled.height / 2, + imgWidth: scaled.width, + imagHeight: scaled.height, + }); + page.node.set(pdf_lib_1.PDFName.of("Annots"), pdfDoc.context.obj([link])); + } const pdfBytes = yield pdfDoc.save(); return pdfBytes; }); diff --git a/src/pdfParser.ts b/src/pdfParser.ts index 998d662..f3b271b 100644 --- a/src/pdfParser.ts +++ b/src/pdfParser.ts @@ -52,23 +52,25 @@ class PDFparser { const pages = pdfDoc.getPages(); - const firstPage = pages[0]; + for (let index = 0; index < pages.length; index++) { + const page = pages[index]; - firstPage.drawImage(img, { - x: firstPage.getWidth() / 2 - scaled.width / 2, - y: firstPage.getHeight() / 2 - scaled.height / 2, - width: scaled.width, - height: scaled.height, - }); + page.drawImage(img, { + x: page.getWidth() / 2 - scaled.width / 2, + y: page.getHeight() / 2 - scaled.height / 2, + width: scaled.width, + height: scaled.height, + }); - const link = this.createPageLinkAnnotation(firstPage, url, { - imgXPos: firstPage.getWidth() / 2 - scaled.width / 2, - imgYPos: firstPage.getHeight() / 2 - scaled.height / 2, - imgWidth: scaled.width, - imagHeight: scaled.height, - }); + const link = this.createPageLinkAnnotation(page, url, { + imgXPos: page.getWidth() / 2 - scaled.width / 2, + imgYPos: page.getHeight() / 2 - scaled.height / 2, + imgWidth: scaled.width, + imagHeight: scaled.height, + }); - firstPage.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link])); + page.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link])); + } const pdfBytes = await pdfDoc.save(); -- GitLab