From 5620d5b338bd3e68d014c836b8ac9902a1e2ff40 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Fri, 15 Apr 2022 13:52:28 +0300
Subject: [PATCH] add parse xy properly

---
 dist/pdfParser.js | 34 +++++++++++++++++++++-------------
 src/pdfParser.ts  | 43 +++++++++++++++++++++++++++----------------
 2 files changed, 48 insertions(+), 29 deletions(-)

diff --git a/dist/pdfParser.js b/dist/pdfParser.js
index c700323..cde9a65 100644
--- a/dist/pdfParser.js
+++ b/dist/pdfParser.js
@@ -52,19 +52,27 @@ class PDFparser {
             const pages = pdfDoc.getPages();
             for (let index = 0; index < pages.length; index++) {
                 const page = pages[index];
-                page.drawImage(img, {
-                    x: parseFloat(coords[index + 1].x),
-                    y: parseFloat(coords[index + 1].y),
-                    width: scaled.width,
-                    height: scaled.height,
-                });
-                const link = this.createPageLinkAnnotation(page, url, {
-                    imgXPos: parseFloat(coords[index + 1].x),
-                    imgYPos: parseFloat(coords[index + 1].y),
-                    imgWidth: scaled.width,
-                    imagHeight: scaled.height,
-                });
-                page.node.set(pdf_lib_1.PDFName.of("Annots"), pdfDoc.context.obj([link]));
+                const x = typeof coords[index + 1] !== "undefined"
+                    ? parseFloat(coords[index + 1].x)
+                    : null;
+                const y = typeof typeof coords[index + 1] !== "undefined"
+                    ? parseFloat(coords[index + 1].y)
+                    : null;
+                if (x && y) {
+                    page.drawImage(img, {
+                        x,
+                        y,
+                        width: scaled.width,
+                        height: scaled.height,
+                    });
+                    const link = this.createPageLinkAnnotation(page, url, {
+                        imgXPos: x,
+                        imgYPos: y,
+                        imgWidth: scaled.width,
+                        imagHeight: scaled.height,
+                    });
+                    page.node.set(pdf_lib_1.PDFName.of("Annots"), pdfDoc.context.obj([link]));
+                }
             }
             const pdfBytes = yield pdfDoc.save();
             return pdfBytes;
diff --git a/src/pdfParser.ts b/src/pdfParser.ts
index 1f61058..2c7ab14 100644
--- a/src/pdfParser.ts
+++ b/src/pdfParser.ts
@@ -67,22 +67,33 @@ class PDFparser {
 
     for (let index = 0; index < pages.length; index++) {
       const page = pages[index];
-
-      page.drawImage(img, {
-        x: parseFloat(coords[index + 1].x),
-        y: parseFloat(coords[index + 1].y),
-        width: scaled.width,
-        height: scaled.height,
-      });
-
-      const link = this.createPageLinkAnnotation(page, url, {
-        imgXPos: parseFloat(coords[index + 1].x),
-        imgYPos: parseFloat(coords[index + 1].y),
-        imgWidth: scaled.width,
-        imagHeight: scaled.height,
-      });
-
-      page.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link]));
+      const x =
+        typeof coords[index + 1] !== "undefined"
+          ? parseFloat(coords[index + 1].x)
+          : null;
+
+      const y =
+        typeof typeof coords[index + 1] !== "undefined"
+          ? parseFloat(coords[index + 1].y)
+          : null;
+
+      if (x && y) {
+        page.drawImage(img, {
+          x,
+          y,
+          width: scaled.width,
+          height: scaled.height,
+        });
+
+        const link = this.createPageLinkAnnotation(page, url, {
+          imgXPos: x,
+          imgYPos: y,
+          imgWidth: scaled.width,
+          imagHeight: scaled.height,
+        });
+
+        page.node.set(PDFName.of("Annots"), pdfDoc.context.obj([link]));
+      }
     }
 
     const pdfBytes = await pdfDoc.save();
-- 
GitLab