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