Skip to content
Snippets Groups Projects
Commit 23a5e9b9 authored by Zdravko Iliev's avatar Zdravko Iliev
Browse files

move initialize to separate method

parent 49c8f2ee
No related branches found
No related tags found
No related merge requests found
...@@ -14,8 +14,9 @@ declare global { ...@@ -14,8 +14,9 @@ declare global {
} }
declare class PDFparser { declare class PDFparser {
readonly document: any; readonly document: any;
constructor(document: Buffer); constructor(document?: Buffer);
initialize: (licenseKey: string) => Promise<void>;
getPDFMeta: () => Promise<IGetMetaResponse>; getPDFMeta: () => Promise<IGetMetaResponse>;
insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string | ArrayBuffer, certTSAUrl: string) => Promise<ArrayBuffer>; insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string, certTSAUrl: string) => Promise<ArrayBuffer>;
} }
export default PDFparser; export default PDFparser;
"use strict"; "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) { var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) { return new (P || (P = Promise))(function (resolve, reject) {
...@@ -39,8 +16,12 @@ const timeUtils_1 = require("./lib/timeUtils"); ...@@ -39,8 +16,12 @@ const timeUtils_1 = require("./lib/timeUtils");
const errors_1 = require("./lib/errors"); const errors_1 = require("./lib/errors");
const generalUtils_1 = require("./lib/generalUtils"); const generalUtils_1 = require("./lib/generalUtils");
const utils_1 = require("./utils"); const utils_1 = require("./utils");
const pdfnet_node_1 = require("@pdftron/pdfnet-node");
class PDFparser { class PDFparser {
constructor(document) { constructor(document) {
this.initialize = (licenseKey) => __awaiter(this, void 0, void 0, function* () {
yield pdfnet_node_1.PDFNet.initialize(licenseKey);
});
this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () { this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
if (!(this.document instanceof Buffer)) { if (!(this.document instanceof Buffer)) {
throw new errors_1.AppError("Document is not Buffer"); throw new errors_1.AppError("Document is not Buffer");
...@@ -69,25 +50,14 @@ class PDFparser { ...@@ -69,25 +50,14 @@ class PDFparser {
} }
}); });
this.insertQrCode = (imgBytes, url, coords, scaleFactor, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () { this.insertQrCode = (imgBytes, url, coords, scaleFactor, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () {
let lib, buf; let buf;
if (typeof window !== "undefined") {
lib = window.PDFNet;
}
else {
const pdflib = yield Promise.resolve().then(() => __importStar(require("@pdftron/pdfnet-node")));
lib = pdflib.PDFNet;
yield lib.initialize(licenseKey);
}
try { try {
buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords, lib); buf = yield (0, utils_1.TimestampAndEnableLTV)(this.document, certPath, certTSAUrl, imgBytes, coords);
} }
catch (error) { catch (error) {
console.log(error); console.log(error);
throw new errors_1.GeneralError("Could Not sign pdf"); throw new errors_1.GeneralError("Could Not sign pdf");
} }
if (typeof window === "undefined") {
yield lib.shutdown();
}
return buf; return buf;
}); });
this.document = document; this.document = document;
......
export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string | ArrayBuffer, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any, lib: any) => Promise<ArrayBuffer>; export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any) => Promise<ArrayBuffer>;
...@@ -10,18 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge ...@@ -10,18 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.TimestampAndEnableLTV = void 0; exports.TimestampAndEnableLTV = void 0;
const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords, lib) => __awaiter(void 0, void 0, void 0, function* () { const pdfnet_node_1 = require("@pdftron/pdfnet-node");
const doc = yield lib.PDFDoc.createFromBuffer(docBuffer); 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);
doc.initSecurityHandler(); doc.initSecurityHandler();
const tst_config = yield lib.TimestampingConfiguration.createFromURL(certTSAUrl); const tst_config = yield pdfnet_node_1.PDFNet.TimestampingConfiguration.createFromURL(certTSAUrl);
const opts = yield lib.VerificationOptions.create(lib.VerificationOptions.SecurityLevel.e_compatibility_and_archiving); const opts = yield pdfnet_node_1.PDFNet.VerificationOptions.create(pdfnet_node_1.PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving);
if (typeof certPath === "string") { yield opts.addTrustedCertificateUString(certPath);
yield opts.addTrustedCertificateUString(certPath); const img = yield pdfnet_node_1.PDFNet.Image.createFromMemory2(doc, imgBytes);
}
else {
yield opts.addTrustedCertificate(certPath);
}
const img = yield lib.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib //make this dynamic with canvas lib
const imgWidth = 300; const imgWidth = 300;
const imgHeight = 300; const imgHeight = 300;
...@@ -36,11 +32,11 @@ const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords ...@@ -36,11 +32,11 @@ const TimestampAndEnableLTV = (docBuffer, certPath, certTSAUrl, imgBytes, coords
} }
const page = yield doc.getPage(p); const page = yield doc.getPage(p);
const doctimestamp_signature_field = yield doc.createDigitalSignatureField(); const doctimestamp_signature_field = yield doc.createDigitalSignatureField();
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); 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);
yield page.annotPushBack(widgetAnnot); yield page.annotPushBack(widgetAnnot);
yield widgetAnnot.createSignatureAppearance(img); yield widgetAnnot.createSignatureAppearance(img);
yield doctimestamp_signature_field.timestampOnNextSave(tst_config, opts); yield doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
result = yield doc.saveMemoryBuffer(lib.SDFDoc.SaveOptions.e_incremental); result = yield doc.saveMemoryBuffer(pdfnet_node_1.PDFNet.SDFDoc.SaveOptions.e_incremental);
} }
return result.buffer; return result.buffer;
}); });
......
...@@ -6,6 +6,7 @@ import { formatPdfTime } from "./lib/timeUtils"; ...@@ -6,6 +6,7 @@ import { formatPdfTime } from "./lib/timeUtils";
import { AppError, GeneralError } from "./lib/errors"; import { AppError, GeneralError } from "./lib/errors";
import { isPDF } from "./lib/generalUtils"; import { isPDF } from "./lib/generalUtils";
import { TimestampAndEnableLTV } from "./utils"; import { TimestampAndEnableLTV } from "./utils";
import { PDFNet } from "@pdftron/pdfnet-node";
type SealCoords = { type SealCoords = {
[key: string]: { x: string; y: string }; [key: string]: { x: string; y: string };
...@@ -21,10 +22,14 @@ declare global { ...@@ -21,10 +22,14 @@ declare global {
class PDFparser { class PDFparser {
readonly document; readonly document;
constructor(document: Buffer) { constructor(document?: Buffer) {
this.document = document; this.document = document;
} }
initialize = async (licenseKey: string): Promise<void> => {
await PDFNet.initialize(licenseKey);
};
getPDFMeta = async (): Promise<IGetMetaResponse> => { getPDFMeta = async (): Promise<IGetMetaResponse> => {
if (!(this.document instanceof Buffer)) { if (!(this.document instanceof Buffer)) {
throw new AppError("Document is not Buffer"); throw new AppError("Document is not Buffer");
...@@ -63,36 +68,23 @@ class PDFparser { ...@@ -63,36 +68,23 @@ class PDFparser {
coords: SealCoords, coords: SealCoords,
scaleFactor: number, scaleFactor: number,
licenseKey: string, licenseKey: string,
certPath: string | ArrayBuffer, certPath: string,
certTSAUrl: string certTSAUrl: string
): Promise<ArrayBuffer> => { ): Promise<ArrayBuffer> => {
let lib, buf; let buf;
if (typeof window !== "undefined") {
lib = window.PDFNet;
} else {
const pdflib = await import("@pdftron/pdfnet-node");
lib = pdflib.PDFNet;
await lib.initialize(licenseKey);
}
try { try {
buf = await TimestampAndEnableLTV( buf = await TimestampAndEnableLTV(
this.document, this.document,
certPath, certPath,
certTSAUrl, certTSAUrl,
imgBytes, imgBytes,
coords, coords
lib
); );
} catch (error) { } catch (error) {
console.log(error); console.log(error);
throw new GeneralError("Could Not sign pdf"); throw new GeneralError("Could Not sign pdf");
} }
if (typeof window === "undefined") {
await lib.shutdown();
}
return buf; return buf;
}; };
} }
......
import { PDFNet } from "@pdftron/pdfnet-node";
export const TimestampAndEnableLTV = async ( export const TimestampAndEnableLTV = async (
docBuffer: ArrayBuffer, docBuffer: ArrayBuffer,
certPath: string | ArrayBuffer, certPath: string,
certTSAUrl: string, certTSAUrl: string,
imgBytes: ArrayBuffer, imgBytes: ArrayBuffer,
coords: any, coords: any
lib: any
): Promise<ArrayBuffer> => { ): Promise<ArrayBuffer> => {
const doc = await lib.PDFDoc.createFromBuffer(docBuffer); const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler(); doc.initSecurityHandler();
const tst_config = await lib.TimestampingConfiguration.createFromURL( const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
certTSAUrl certTSAUrl
); );
const opts = await lib.VerificationOptions.create( const opts = await PDFNet.VerificationOptions.create(
lib.VerificationOptions.SecurityLevel.e_compatibility_and_archiving PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
); );
if (typeof certPath === "string") { await opts.addTrustedCertificateUString(certPath);
await opts.addTrustedCertificateUString(certPath);
} else {
await opts.addTrustedCertificate(certPath);
}
const img = await lib.Image.createFromMemory2(doc, imgBytes); const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib //make this dynamic with canvas lib
const imgWidth = 300; const imgWidth = 300;
...@@ -43,9 +40,9 @@ export const TimestampAndEnableLTV = async ( ...@@ -43,9 +40,9 @@ export const TimestampAndEnableLTV = async (
await doc.createDigitalSignatureField(); await doc.createDigitalSignatureField();
const widgetAnnot = const widgetAnnot =
await lib.SignatureWidget.createWithDigitalSignatureField( await PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc, doc,
new lib.Rect( new PDFNet.Rect(
parseFloat(coords[p].x), parseFloat(coords[p].x),
parseFloat(coords[p].x + imgWidth), parseFloat(coords[p].x + imgWidth),
parseFloat(coords[p].y), parseFloat(coords[p].y),
...@@ -58,7 +55,9 @@ export const TimestampAndEnableLTV = async ( ...@@ -58,7 +55,9 @@ export const TimestampAndEnableLTV = async (
await widgetAnnot.createSignatureAppearance(img); await widgetAnnot.createSignatureAppearance(img);
await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts); await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
result = await doc.saveMemoryBuffer(lib.SDFDoc.SaveOptions.e_incremental); result = await doc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_incremental
);
} }
return result.buffer; return result.buffer;
......
yarn.lock 0 → 100644
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment