diff --git a/dist/pdfParser.js b/dist/pdfParser.js index c7003236239b98724e9bfa55e665409796297d98..cde9a656e7cb71cc5800fe7d03392f12103247a2 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 1f6105869318768331b8164411dfe3a3af5a3472..2c7ab14428bede52d8b31ccf18a6bfd5a93868c6 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();