Skip to content
Snippets Groups Projects
pdfParser.js 4.08 KiB
"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();
            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 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;