"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const pdf_lib_1 = require("pdf-lib"); const pdfdataextract_1 = require("pdfdataextract"); const config_1 = require("./config"); const lib_1 = require("./lib"); const timeUtils_1 = require("./lib/timeUtils"); const errors_1 = require("./lib/errors"); const generalUtils_1 = require("./lib/generalUtils"); class PDFparser { constructor(document) { this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () { if (!(this.document instanceof Buffer)) { throw new errors_1.AppError("Document is not Buffer"); } if (!(0, generalUtils_1.isPDF)(this.document)) { throw new errors_1.AppError("Only pdf file type is supported"); } try { const signaturesMeta = yield (0, lib_1.verifyPDF)(this.document); const pdfMeta = yield pdfdataextract_1.PdfData.extract(this.document, config_1.config); const result = { pages: pdfMeta.pages, title: pdfMeta.info.Title || "Unknown", author: pdfMeta.info.Author || "Unknown", creation_date: (0, timeUtils_1.formatPdfTime)(pdfMeta.info.CreationDate), mod_date: (0, timeUtils_1.formatPdfTime)(pdfMeta.info.ModDate), }; if (signaturesMeta) { result["signatures"] = signaturesMeta.signatures; result["expired"] = signaturesMeta.expired; } return result; } catch (error) { throw new errors_1.GeneralError(error); } }); this.insertQrCode = (imgBytes, url, coords, scaleFactor) => __awaiter(this, void 0, void 0, function* () { const pdfDoc = yield pdf_lib_1.PDFDocument.load(this.document); const img = yield pdfDoc.embedPng(imgBytes); const scaled = img.scale(scaleFactor); const pages = pdfDoc.getPages(); console.log(coords); for (let index = 0; index < pages.length; index++) { const page = pages[index]; console.log(coords[index + 1]); console.log(typeof coords[index + 1] !== "undefined"); 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; console.log(x, y); 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; }); this.createPageLinkAnnotation = (page, uri, { imgXPos, imgYPos, imgWidth, imagHeight }) => page.doc.context.register(page.doc.context.obj({ Type: "Annot", Subtype: "Link", Rect: [imgXPos, imgYPos, imgXPos + imgWidth, imgYPos + imagHeight], A: { Type: "Action", S: "URI", URI: pdf_lib_1.PDFString.of(uri), }, })); this.document = document; this.config = config_1.config; } } exports.default = PDFparser;