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

[WIP]: add browser support

parent 2a3ca79f
No related branches found
No related tags found
No related merge requests found
File added
import fs from "fs"; import fs from "fs";
import path from "path"; 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 PDFparser from "../src/pdfParser";
import { AppError } from "../src/lib/errors"; import { AppError } from "../src/lib/errors";
import { saveAs } from "file-saver";
describe("PDF parser", () => { describe("PDF parser", () => {
it("should return pdf document metadata including signatures", async () => { it("should return pdf document metadata including signatures", async () => {
...@@ -66,6 +65,7 @@ describe("PDF insert", () => { ...@@ -66,6 +65,7 @@ describe("PDF insert", () => {
); );
const qrcode = fs.readFileSync(path.resolve(__dirname, "./qrcode.png")); const qrcode = fs.readFileSync(path.resolve(__dirname, "./qrcode.png"));
const cert = fs.readFileSync(path.resolve(__dirname, "./cert.cer"));
const parser = new PDFparser(file); const parser = new PDFparser(file);
...@@ -74,12 +74,22 @@ describe("PDF insert", () => { ...@@ -74,12 +74,22 @@ describe("PDF insert", () => {
"2": { x: "261.6", y: "384.84" }, "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( const resultPDF = await parser.insertQrCode(
qrcode.buffer, qrcode.buffer,
"vereign.com", "vereign.com",
sealCoords, sealCoords,
0.25, 0.25,
"demo:1652778685773:7b893c000300000000b9c455b21b7b47780dc82f9ef63ddc54ce5c282b" "demo:1652778685773:7b893c000300000000b9c455b21b7b47780dc82f9ef63ddc54ce5c282b",
cert,
"http://rfc3161timestamp.globalsign.com/advanced"
); );
fs.writeFileSync( fs.writeFileSync(
......
File deleted
...@@ -6,12 +6,18 @@ import { formatPdfTime } from "./lib/timeUtils"; ...@@ -6,12 +6,18 @@ 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 };
}; };
declare global {
interface Window {
PDFNet: any;
CoreControls: any;
}
}
class PDFparser { class PDFparser {
readonly document; readonly document;
...@@ -57,25 +63,36 @@ class PDFparser { ...@@ -57,25 +63,36 @@ class PDFparser {
coords: SealCoords, coords: SealCoords,
scaleFactor: number, scaleFactor: number,
licenseKey: string, licenseKey: string,
certPath: string, certPath: string | ArrayBuffer,
certTSAUrl: string certTSAUrl: string
): Promise<ArrayBuffer> => { ): Promise<ArrayBuffer> => {
await PDFNet.initialize(licenseKey); let lib, buf;
let 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 { 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");
} }
await PDFNet.shutdown(); 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, certPath: string | ArrayBuffer,
certTSAUrl: string, certTSAUrl: string,
imgBytes: ArrayBuffer, imgBytes: ArrayBuffer,
coords: any coords: any,
lib: any
): Promise<ArrayBuffer> => { ): Promise<ArrayBuffer> => {
const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer); const doc = await lib.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler(); doc.initSecurityHandler();
const tst_config = await PDFNet.TimestampingConfiguration.createFromURL( const tst_config = await lib.TimestampingConfiguration.createFromURL(
certTSAUrl certTSAUrl
); );
const opts = await PDFNet.VerificationOptions.create( const opts = await lib.VerificationOptions.create(
PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving 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 //make this dynamic with canvas lib
const imgWidth = 300; const imgWidth = 300;
...@@ -40,9 +43,9 @@ export const TimestampAndEnableLTV = async ( ...@@ -40,9 +43,9 @@ export const TimestampAndEnableLTV = async (
await doc.createDigitalSignatureField(); await doc.createDigitalSignatureField();
const widgetAnnot = const widgetAnnot =
await PDFNet.SignatureWidget.createWithDigitalSignatureField( await lib.SignatureWidget.createWithDigitalSignatureField(
doc, doc,
new PDFNet.Rect( new lib.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),
...@@ -55,9 +58,7 @@ export const TimestampAndEnableLTV = async ( ...@@ -55,9 +58,7 @@ 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( result = await doc.saveMemoryBuffer(lib.SDFDoc.SaveOptions.e_incremental);
PDFNet.SDFDoc.SaveOptions.e_incremental
);
} }
return result.buffer; return result.buffer;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment