From 5329077189d1f94b3d991a1200fbe05a57951341 Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Tue, 5 Apr 2022 14:36:21 +0300 Subject: [PATCH] add dynamic scale and pos --- dist/pdfParser.d.ts | 8 +++++++- dist/pdfParser.js | 12 ++++-------- src/pdfParser.ts | 21 ++++++++++----------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index 6644c4a..6c26499 100644 --- a/dist/pdfParser.d.ts +++ b/dist/pdfParser.d.ts @@ -1,11 +1,17 @@ /// <reference types="node" /> import { IGetMetaResponse } from "./types"; +declare type SealCoords = { + [key: string]: { + x: number; + y: number; + }; +}; declare class PDFparser { readonly document: any; readonly config: any; constructor(document: Buffer); getPDFMeta: () => Promise<IGetMetaResponse>; - insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: any, scaleFactor: number) => Promise<ArrayBuffer>; + insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number) => Promise<ArrayBuffer>; private createPageLinkAnnotation; } export default PDFparser; diff --git a/dist/pdfParser.js b/dist/pdfParser.js index 2c64a8e..4fa7086 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -52,19 +52,15 @@ class PDFparser { const pages = pdfDoc.getPages(); for (let index = 0; index < pages.length; index++) { const page = pages[index]; - console.log("dynamic x ", coords[index + 1].x); - console.log("dynamic y ", coords[index + 1].y); - console.log("x", page.getWidth() / 2 - scaled.width / 2); - console.log("y", page.getHeight() / 2 - scaled.height / 2); page.drawImage(img, { - x: page.getWidth() / 2 - scaled.width / 2, - y: page.getHeight() / 2 - scaled.height / 2, + x: coords[index + 1].x, + y: coords[index + 1].y, 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, + imgXPos: coords[index + 1].x, + imgYPos: coords[index + 1].y, imgWidth: scaled.width, imagHeight: scaled.height, }); diff --git a/src/pdfParser.ts b/src/pdfParser.ts index d243374..fe1fa8f 100644 --- a/src/pdfParser.ts +++ b/src/pdfParser.ts @@ -6,6 +6,11 @@ import { verifyPDF } from "./lib"; import { formatPdfTime } from "./lib/timeUtils"; import { AppError, GeneralError } from "./lib/errors"; import { isPDF } from "./lib/generalUtils"; + +type SealCoords = { + [key: string]: { x: number; y: number }; +}; + class PDFparser { readonly document; readonly config; @@ -50,7 +55,7 @@ class PDFparser { insertQrCode = async ( imgBytes: ArrayBuffer, url: string, - coords: any, + coords: SealCoords, scaleFactor: number ): Promise<ArrayBuffer> => { const pdfDoc = await PDFDocument.load(this.document); @@ -63,22 +68,16 @@ class PDFparser { for (let index = 0; index < pages.length; index++) { const page = pages[index]; - console.log("dynamic x ", coords[index + 1].x); - console.log("dynamic y ", coords[index + 1].y); - - console.log("x", page.getWidth() / 2 - scaled.width / 2); - console.log("y", page.getHeight() / 2 - scaled.height / 2); - page.drawImage(img, { - x: page.getWidth() / 2 - scaled.width / 2, - y: page.getHeight() / 2 - scaled.height / 2, + x: coords[index + 1].x, + y: coords[index + 1].y, 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, + imgXPos: coords[index + 1].x, + imgYPos: coords[index + 1].y, imgWidth: scaled.width, imagHeight: scaled.height, }); -- GitLab