diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index 7cf3fa1f704c09bf3aabdf59cfee08c219013112..adf5929c0c3a5b7e11ea9209004735310428f387 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 0677a9c0ef9275384253b8e9d036e4d48c760e43..87278fd1b612ac43c3287c4279dad21b61844baf 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 4aa891d0e1eb5be0f8341b944383016481d15da5..d7816df41b548e55d53e8a55129f0f7cb9d061d1 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 4a89503a8cc0ca1ee1fa69000b38c324e4f248c2..15465b3e6154c26e219736b070bc8167d361028c 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 f58a7813763ce0b3f07f9759e8fd2ef1a4dbdfa8..46e1b62c920f9879ef23406fb847dae02679fbd7 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==