From 52a1720defe541993e20722225ffb22a3fd0318c Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Wed, 2 Mar 2022 12:55:43 +0200 Subject: [PATCH] scale down the qrcode and insert link --- dist/pdfParser.d.ts | 1 + dist/pdfParser.js | 35 +++++++++++++++++++----------- src/pdfParser.ts | 52 +++++++++++++++++++++++++++++++++------------ 3 files changed, 62 insertions(+), 26 deletions(-) diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index adf5929..bac00b0 100644 --- a/dist/pdfParser.d.ts +++ b/dist/pdfParser.d.ts @@ -6,5 +6,6 @@ declare class PDFparser { constructor(document: Buffer); getPDFMeta: () => Promise<IgetMetaResponse>; insertQrCode: (imgBytes: ArrayBuffer) => Promise<ArrayBuffer>; + private createPageLinkAnnotation; } export default PDFparser; diff --git a/dist/pdfParser.js b/dist/pdfParser.js index 6a76d7a..4597eec 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); const verify_pdf_1 = __importDefault(require("@ninja-labs/verify-pdf")); +const pdf_lib_1 = require("pdf-lib"); const pdfdataextract_1 = require("pdfdataextract"); const config_1 = require("./config"); const { PDFDocument } = require("pdf-lib"); @@ -48,25 +49,35 @@ class PDFparser { this.insertQrCode = (imgBytes) => __awaiter(this, void 0, void 0, function* () { const pdfDoc = yield PDFDocument.load(this.document); const img = yield pdfDoc.embedPng(imgBytes); - // const imagePage = pdfDoc.insertPage(0); + const scaled = img.scale(0.5); const pages = pdfDoc.getPages(); const firstPage = pages[0]; - const { width, height } = firstPage.getSize(); firstPage.drawImage(img, { - x: firstPage.getWidth() / 2 - img.width / 2, - y: firstPage.getHeight() / 2 - img.height / 2, - width: img.width, - height: img.height, + x: firstPage.getWidth() / 2 - scaled.width / 2, + y: firstPage.getHeight() / 2 - scaled.height / 2, + width: scaled.width, + height: scaled.height, }); - // firstPage.drawImage(img, { - // x: 0, - // y: 0, - // width: firstPage.getWidth(), - // height: firstPage.getHeight(), - // }); + const link = this.createPageLinkAnnotation(firstPage, "https://pdf-lib.js.org/", { + 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])); const pdfBytes = yield pdfDoc.save(); return pdfBytes; }); + this.createPageLinkAnnotation = (page, uri, { imgXPos, imgYPos, imgWidth, imagHeight }) => page.doc.context.register(page.doc.context.obj({ + Type: "Annot", + Subtype: "Link", + Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight], + A: { + Type: "Action", + S: "URI", + URI: pdf_lib_1.PDFString.of(uri), + }, + })); this.document = document; this.config = config_1.config; } diff --git a/src/pdfParser.ts b/src/pdfParser.ts index bca3074..c6455e0 100644 --- a/src/pdfParser.ts +++ b/src/pdfParser.ts @@ -1,9 +1,9 @@ import verifyPDF from "@ninja-labs/verify-pdf"; +import { PDFName, PDFPage, PDFString } from "pdf-lib"; import { PdfData } from "pdfdataextract"; import { config } from "./config"; import { IgetMetaResponse } from "./types"; const { PDFDocument } = require("pdf-lib"); - class PDFparser { readonly document; readonly config; @@ -44,31 +44,55 @@ class PDFparser { insertQrCode = async (imgBytes: ArrayBuffer): Promise<ArrayBuffer> => { const pdfDoc = await PDFDocument.load(this.document); const img = await pdfDoc.embedPng(imgBytes); - // const imagePage = pdfDoc.insertPage(0); + + const scaled = img.scale(0.5); const pages = pdfDoc.getPages(); - const firstPage = pages[0]; - const { width, height } = firstPage.getSize(); + const firstPage = pages[0]; firstPage.drawImage(img, { - x: firstPage.getWidth() / 2 - img.width / 2, - y: firstPage.getHeight() / 2 - img.height / 2, - width: img.width, - height: img.height, + x: firstPage.getWidth() / 2 - scaled.width / 2, + y: firstPage.getHeight() / 2 - scaled.height / 2, + width: scaled.width, + height: scaled.height, }); - // firstPage.drawImage(img, { - // x: 0, - // y: 0, - // width: firstPage.getWidth(), - // height: firstPage.getHeight(), - // }); + const link = this.createPageLinkAnnotation( + firstPage, + "https://pdf-lib.js.org/", + { + imgXPos: firstPage.getWidth() / 2 - scaled.width / 2, + imgYPos: firstPage.getHeight() / 2 - scaled.height / 2, + imgWidth: scaled.width, + imagHeight: scaled.height, + } + ); + + firstPage.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link])); const pdfBytes = await pdfDoc.save(); return pdfBytes; }; + + private createPageLinkAnnotation = ( + page: PDFPage, + uri: string, + { imgXPos, imgYPos, imgWidth, imagHeight } + ) => + page.doc.context.register( + page.doc.context.obj({ + Type: "Annot", + Subtype: "Link", + Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight], + A: { + Type: "Action", + S: "URI", + URI: PDFString.of(uri), + }, + }) + ); } export default PDFparser; -- GitLab