From bb95c5e3347f39153919ab7ce829efd6a12f655c Mon Sep 17 00:00:00 2001 From: Zdravko Iliev <zdravko.iliev@vereign.com> Date: Fri, 27 May 2022 17:35:06 +0300 Subject: [PATCH] build --- __tests__/index.test.ts | 8 -------- dist/pdfParser.d.ts | 8 +++++++- dist/pdfParser.js | 42 ++++++++++++++++++++++++++++++++++++----- dist/utils.d.ts | 2 +- dist/utils.js | 22 ++++++++++++--------- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index f94dc61..45af598 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -74,14 +74,6 @@ describe("PDF insert", () => { "2": { x: "261.6", y: "384.84" }, }; - // imgBytes: ArrayBuffer, - // url: string, - // coords: SealCoords, - // scaleFactor: number, - // licenseKey: string, - // certPath: string, - // certTSAUrl: string - const resultPDF = await parser.insertQrCode( qrcode.buffer, "vereign.com", diff --git a/dist/pdfParser.d.ts b/dist/pdfParser.d.ts index a3fffde..5872b5b 100644 --- a/dist/pdfParser.d.ts +++ b/dist/pdfParser.d.ts @@ -6,10 +6,16 @@ declare type SealCoords = { y: string; }; }; +declare global { + interface Window { + PDFNet: any; + CoreControls: any; + } +} declare class PDFparser { readonly document: any; constructor(document: Buffer); getPDFMeta: () => Promise<IGetMetaResponse>; - insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string, certTSAUrl: string) => Promise<ArrayBuffer>; + insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string | ArrayBuffer, certTSAUrl: string) => Promise<ArrayBuffer>; } export default PDFparser; diff --git a/dist/pdfParser.js b/dist/pdfParser.js index d5404c5..8fc69cb 100644 --- a/dist/pdfParser.js +++ b/dist/pdfParser.js @@ -1,4 +1,27 @@ "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; 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) { @@ -16,7 +39,6 @@ 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* () { @@ -47,16 +69,26 @@ class PDFparser { } }); 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; + let lib, buf; + if (typeof window !== `undefined`) { + lib = window.PDFNet; + const CoreControls = window.CoreControls; + CoreControls.setWorkerPath("webviewer/core"); + } + else { + const pdflib = yield Promise.resolve().then(() => __importStar(require("@pdftron/pdfnet-node"))); + lib = pdflib.PDFNet; + } + //will this work ?? + yield lib.initialize(licenseKey); try { - buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords); + buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords, lib); } catch (error) { console.log(error); throw new errors_1.GeneralError("Could Not sign pdf"); } - yield pdfnet_node_1.PDFNet.shutdown(); + yield lib.shutdown(); return buf; }); this.document = document; diff --git a/dist/utils.d.ts b/dist/utils.d.ts index ce9bafb..f60352a 100644 --- a/dist/utils.d.ts +++ b/dist/utils.d.ts @@ -1 +1 @@ -export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any) => Promise<ArrayBuffer>; +export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string | ArrayBuffer, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any, lib: any) => Promise<ArrayBuffer>; diff --git a/dist/utils.js b/dist/utils.js index 3fc0eaa..939db34 100644 --- a/dist/utils.js +++ b/dist/utils.js @@ -10,14 +10,18 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TimestampAndEnableLTV = void 0; -const pdfnet_node_1 = require("@pdftron/pdfnet-node"); -const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords) => __awaiter(void 0, void 0, void 0, function* () { - const doc = yield pdfnet_node_1.PDFNet.PDFDoc.createFromBuffer(docBuffer); +const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords, lib) => __awaiter(void 0, void 0, void 0, function* () { + const doc = yield lib.PDFDoc.createFromBuffer(docBuffer); doc.initSecurityHandler(); - const tst_config = yield pdfnet_node_1.PDFNet.TimestampingConfiguration.createFromURL(certTSAUrl); - const opts = yield pdfnet_node_1.PDFNet.VerificationOptions.create(pdfnet_node_1.PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving); - yield opts.addTrustedCertificateUString(certPath); - const img = yield pdfnet_node_1.PDFNet.Image.createFromMemory2(doc, imgBytes); + const tst_config = yield lib.TimestampingConfiguration.createFromURL(certTSAUrl); + const opts = yield lib.VerificationOptions.create(lib.VerificationOptions.SecurityLevel.e_compatibility_and_archiving); + if (typeof certPath === "string") { + yield opts.addTrustedCertificateUString(certPath); + } + else { + yield opts.addTrustedCertificate(certPath); + } + const img = yield lib.Image.createFromMemory2(doc, imgBytes); //make this dynamic with canvas lib const imgWidth = 300; const imgHeight = 300; @@ -32,11 +36,11 @@ const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords } const page = yield doc.getPage(p); const doctimestamp_signature_field = yield doc.createDigitalSignatureField(); - const widgetAnnot = yield pdfnet_node_1.PDFNet.SignatureWidget.createWithDigitalSignatureField(doc, new pdfnet_node_1.PDFNet.Rect(parseFloat(coords[p].x), parseFloat(coords[p].x + imgWidth), parseFloat(coords[p].y), parseFloat(coords[p].y + imgHeight)), doctimestamp_signature_field); + const widgetAnnot = yield lib.SignatureWidget.createWithDigitalSignatureField(doc, new lib.Rect(parseFloat(coords[p].x), parseFloat(coords[p].x + imgWidth), parseFloat(coords[p].y), parseFloat(coords[p].y + imgHeight)), doctimestamp_signature_field); yield page.annotPushBack(widgetAnnot); yield widgetAnnot.createSignatureAppearance(img); yield doctimestamp_signature_field.timestampOnNextSave(tst_config, opts); - result = yield doc.saveMemoryBuffer(pdfnet_node_1.PDFNet.SDFDoc.SaveOptions.e_incremental); + result = yield doc.saveMemoryBuffer(lib.SDFDoc.SaveOptions.e_incremental); } return result.buffer; }); -- GitLab