From 60b08df6d3c85a7b9348b8c2ee3840a84e6e9363 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Tue, 1 Mar 2022 18:36:19 +0200
Subject: [PATCH] qrcode in center

---
 dist/pdfParser.js | 21 +++++++++++++++------
 src/pdfParser.ts  | 24 ++++++++++++++++++------
 2 files changed, 33 insertions(+), 12 deletions(-)

diff --git a/dist/pdfParser.js b/dist/pdfParser.js
index 87278fd..6a76d7a 100644
--- a/dist/pdfParser.js
+++ b/dist/pdfParser.js
@@ -48,13 +48,22 @@ 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);
-            imagePage.drawImage(img, {
-                x: 0,
-                y: 0,
-                width: imagePage.getWidth(),
-                height: imagePage.getHeight(),
+            // const imagePage = pdfDoc.insertPage(0);
+            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,
             });
+            // firstPage.drawImage(img, {
+            //   x: 0,
+            //   y: 0,
+            //   width: firstPage.getWidth(),
+            //   height: firstPage.getHeight(),
+            // });
             const pdfBytes = yield pdfDoc.save();
             return pdfBytes;
         });
diff --git a/src/pdfParser.ts b/src/pdfParser.ts
index 15465b3..bca3074 100644
--- a/src/pdfParser.ts
+++ b/src/pdfParser.ts
@@ -44,15 +44,27 @@ 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 imagePage = pdfDoc.insertPage(0);
 
-    imagePage.drawImage(img, {
-      x: 0,
-      y: 0,
-      width: imagePage.getWidth(),
-      height: imagePage.getHeight(),
+    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,
     });
 
+    // firstPage.drawImage(img, {
+    //   x: 0,
+    //   y: 0,
+    //   width: firstPage.getWidth(),
+    //   height: firstPage.getHeight(),
+    // });
+
     const pdfBytes = await pdfDoc.save();
 
     return pdfBytes;
-- 
GitLab