From c7b432b633576fa7aa106f4f25a335c98b2e091f Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Tue, 1 Mar 2022 17:59:41 +0200 Subject: [PATCH] insertQrcode --- dist/pdfParser.d.ts | 1 + dist/pdfParser.js | 14 ++++++++++++++ package.json | 1 + src/pdfParser.ts | 18 ++++++++++++++++++ yarn.lock | 31 ++++++++++++++++++++++++++++++- 5 files changed, 64 insertions(+), 1 deletion(-) diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index 7cf3fa1..adf5929 100644 --- a/dist/pdfParser.d.ts +++ b/dist/pdfParser.d.ts @@ -5,5 +5,6 @@ declare class PDFparser { readonly config: any; constructor(document: Buffer); getPDFMeta: () => Promise<IgetMetaResponse>; + insertQrCode: (imgBytes: ArrayBuffer) => Promise<ArrayBuffer>; } export default PDFparser; diff --git a/dist/pdfParser.js b/dist/pdfParser.js index 0677a9c..87278fd 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true }); const verify_pdf_1 = __importDefault(require("@ninja-labs/verify-pdf")); const pdfdataextract_1 = require("pdfdataextract"); const config_1 = require("./config"); +const { PDFDocument } = require("pdf-lib"); class PDFparser { constructor(document) { this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () { @@ -44,6 +45,19 @@ class PDFparser { throw new Error("Could not get pdf metadata"); } }); + 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); + imagePage.drawImage(img, { + x: 0, + y: 0, + width: imagePage.getWidth(), + height: imagePage.getHeight(), + }); + const pdfBytes = yield pdfDoc.save(); + return pdfBytes; + }); this.document = document; this.config = config_1.config; } diff --git a/package.json b/package.json index 4aa891d..d7816df 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ }, "dependencies": { "@ninja-labs/verify-pdf": "^0.3.9", + "pdf-lib": "^1.17.1", "pdfdataextract": "^3.2.0" } } diff --git a/src/pdfParser.ts b/src/pdfParser.ts index 4a89503..15465b3 100644 --- a/src/pdfParser.ts +++ b/src/pdfParser.ts @@ -2,6 +2,7 @@ import verifyPDF from "@ninja-labs/verify-pdf"; import { PdfData } from "pdfdataextract"; import { config } from "./config"; import { IgetMetaResponse } from "./types"; +const { PDFDocument } = require("pdf-lib"); class PDFparser { readonly document; @@ -39,6 +40,23 @@ class PDFparser { throw new Error("Could not get pdf metadata"); } }; + + insertQrCode = async (imgBytes: ArrayBuffer): Promise<ArrayBuffer> => { + const pdfDoc = await PDFDocument.load(this.document); + const img = await pdfDoc.embedPng(imgBytes); + const imagePage = pdfDoc.insertPage(0); + + imagePage.drawImage(img, { + x: 0, + y: 0, + width: imagePage.getWidth(), + height: imagePage.getHeight(), + }); + + const pdfBytes = await pdfDoc.save(); + + return pdfBytes; + }; } export default PDFparser; diff --git a/yarn.lock b/yarn.lock index f58a781..46e1b62 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1077,6 +1077,20 @@ dependencies: node-forge "^0.10.0" +"@pdf-lib/standard-fonts@^1.0.0": + version "1.0.0" + resolved "https://registry.yarnpkg.com/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz#8ba691c4421f71662ed07c9a0294b44528af2d7f" + integrity sha512-hU30BK9IUN/su0Mn9VdlVKsWBS6GyhVfqjwl1FjZN4TxP6cCw0jP2w7V3Hf5uX7M0AZJ16vey9yE0ny7Sa59ZA== + dependencies: + pako "^1.0.6" + +"@pdf-lib/upng@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@pdf-lib/upng/-/upng-1.0.1.tgz#7dc9c636271aca007a9df4deaf2dd7e7960280cb" + integrity sha512-dQK2FUMQtowVP00mtIksrlZhdFXQZPC+taih1q4CvPZ5vqdxR/LKBaFg0oAfzd1GlHZXXSPdQfzQnt+ViGvEIQ== + dependencies: + pako "^1.0.10" + "@sinonjs/commons@^1.7.0": version "1.8.1" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.1.tgz#e7df00f98a203324f6dc7cc606cad9d4a8ab2217" @@ -3864,6 +3878,11 @@ p-try@^2.0.0: resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ== +pako@^1.0.10, pako@^1.0.11, pako@^1.0.6: + version "1.0.11" + resolved "https://registry.yarnpkg.com/pako/-/pako-1.0.11.tgz#6c9599d340d54dfd3946380252a35705a6b992bf" + integrity sha512-4hLB8Py4zZce5s4yd9XzopqwVv/yGNhV1Bl8NTmCq1763HeK2+EwVTv+leGeL13Dnh2wfbqowVPXCIO0z4taYw== + parent-module@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/parent-module/-/parent-module-1.0.1.tgz#691d2709e78c79fae3a156622452d00762caaaa2" @@ -3921,6 +3940,16 @@ path-type@^4.0.0: resolved "https://registry.yarnpkg.com/path-type/-/path-type-4.0.0.tgz#84ed01c0a7ba380afe09d90a8c180dcd9d03043b" integrity sha512-gDKb8aZMDeD/tZWs9P6+q0J9Mwkdl6xMV8TjnGP3qJVJ06bdMgkbBlLU8IdfOsIsFz2BW1rNVT3XuNEl8zPAvw== +pdf-lib@^1.17.1: + version "1.17.1" + resolved "https://registry.yarnpkg.com/pdf-lib/-/pdf-lib-1.17.1.tgz#9e7dd21261a0c1fb17992580885b39e7d08f451f" + integrity sha512-V/mpyJAoTsN4cnP31vc0wfNA1+p20evqqnap0KLoRUN0Yk/p3wN52DOEsL4oBFcLdb76hlpKPtzJIgo67j/XLw== + dependencies: + "@pdf-lib/standard-fonts" "^1.0.0" + "@pdf-lib/upng" "^1.0.1" + pako "^1.0.11" + tslib "^1.11.1" + pdfdataextract@^3.2.0: version "3.2.0" resolved "https://registry.yarnpkg.com/pdfdataextract/-/pdfdataextract-3.2.0.tgz#c40d59fb0de4ed6e63a1da08f87d4f4550e841ec" @@ -4769,7 +4798,7 @@ tr46@^2.0.2: dependencies: punycode "^2.1.1" -tslib@^1.8.1, tslib@^1.9.0: +tslib@^1.11.1, tslib@^1.8.1, tslib@^1.9.0: version "1.14.1" resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== -- GitLab