Skip to content
Snippets Groups Projects
pdfParser.js 3.09 KiB
Newer Older
  • Learn to ignore specific revisions
  • Zdravko Iliev's avatar
    Zdravko Iliev committed
    "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 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");
    const utils_1 = require("./utils");
    const pdfnet_node_1 = require("@pdftron/pdfnet-node");
    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, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () {
                yield pdfnet_node_1.PDFNet.initialize(licenseKey);
                let buf;
                try {
                    buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords);
                }
                catch (error) {
                    console.log(error);
                    throw new errors_1.GeneralError("Could Not sign pdf");
                }
                yield pdfnet_node_1.PDFNet.shutdown();
                return buf;
            });
            this.document = document;
        }
    }
    exports.default = PDFparser;