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

[WIP]: add browser support

parent 2a3ca79f
Branches
No related tags found
No related merge requests found
File added
import fs from "fs";
import path from "path";
import { describe, it, expect, beforeAll } from "@jest/globals";
import { describe, it, expect } from "@jest/globals";
import PDFparser from "../src/pdfParser";
import { AppError } from "../src/lib/errors";
import { saveAs } from "file-saver";
describe("PDF parser", () => {
it("should return pdf document metadata including signatures", async () => {
......@@ -66,6 +65,7 @@ describe("PDF insert", () => {
);
const qrcode = fs.readFileSync(path.resolve(__dirname, "./qrcode.png"));
const cert = fs.readFileSync(path.resolve(__dirname, "./cert.cer"));
const parser = new PDFparser(file);
......@@ -74,12 +74,22 @@ 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",
sealCoords,
0.25,
"demo:1652778685773:7b893c000300000000b9c455b21b7b47780dc82f9ef63ddc54ce5c282b"
"demo:1652778685773:7b893c000300000000b9c455b21b7b47780dc82f9ef63ddc54ce5c282b",
cert,
"http://rfc3161timestamp.globalsign.com/advanced"
);
fs.writeFileSync(
......
File deleted
......@@ -6,12 +6,18 @@ 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 };
};
declare global {
interface Window {
PDFNet: any;
CoreControls: any;
}
}
class PDFparser {
readonly document;
......@@ -57,25 +63,36 @@ class PDFparser {
coords: SealCoords,
scaleFactor: number,
licenseKey: string,
certPath: string,
certPath: string | ArrayBuffer,
certTSAUrl: string
): Promise<ArrayBuffer> => {
await 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 = await import("@pdftron/pdfnet-node");
lib = pdflib.PDFNet;
}
//will this work ??
await lib.initialize(licenseKey);
try {
buf = await TimestampAndEnableLTV(
this.document,
certPath,
certTSAUrl,
imgBytes,
coords
coords,
lib
);
} catch (error) {
console.log(error);
throw new GeneralError("Could Not sign pdf");
}
await PDFNet.shutdown();
await lib.shutdown();
return buf;
};
}
......
import { PDFNet } from "@pdftron/pdfnet-node";
export const TimestampAndEnableLTV = async (
docBuffer: ArrayBuffer,
certPath: string,
certPath: string | ArrayBuffer,
certTSAUrl: string,
imgBytes: ArrayBuffer,
coords: any
coords: any,
lib: any
): Promise<ArrayBuffer> => {
const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
const doc = await lib.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler();
const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
const tst_config = await lib.TimestampingConfiguration.createFromURL(
certTSAUrl
);
const opts = await PDFNet.VerificationOptions.create(
PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
const opts = await lib.VerificationOptions.create(
lib.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
);
await opts.addTrustedCertificateUString(certPath);
if (typeof certPath === "string") {
await opts.addTrustedCertificateUString(certPath);
} else {
await opts.addTrustedCertificate(certPath);
}
const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
const img = await lib.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib
const imgWidth = 300;
......@@ -40,9 +43,9 @@ export const TimestampAndEnableLTV = async (
await doc.createDigitalSignatureField();
const widgetAnnot =
await PDFNet.SignatureWidget.createWithDigitalSignatureField(
await lib.SignatureWidget.createWithDigitalSignatureField(
doc,
new PDFNet.Rect(
new lib.Rect(
parseFloat(coords[p].x),
parseFloat(coords[p].x + imgWidth),
parseFloat(coords[p].y),
......@@ -55,9 +58,7 @@ export const TimestampAndEnableLTV = async (
await widgetAnnot.createSignatureAppearance(img);
await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
result = await doc.saveMemoryBuffer(
PDFNet.SDFDoc.SaveOptions.e_incremental
);
result = await doc.saveMemoryBuffer(lib.SDFDoc.SaveOptions.e_incremental);
}
return result.buffer;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment