Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • code/js-toolbox/lib-pdf
1 result
Show changes
Commits on Source (2)
/// <reference types="node" />
import { IGetMetaResponse } from "./types";
declare type SealCoords = {
[key: string]: {
x: string;
y: string;
};
};
declare global {
interface Window {
PDFNet: any;
......@@ -17,6 +11,5 @@ declare class PDFparser {
constructor(document?: Buffer);
initialize: (licenseKey: string) => Promise<void>;
getPDFMeta: () => Promise<IGetMetaResponse>;
insertQrCode: (imgBytes: ArrayBuffer, url: string, coords: SealCoords, scaleFactor: number, licenseKey: string, certPath: string, certTSAUrl: string) => Promise<ArrayBuffer>;
}
export default PDFparser;
......@@ -15,12 +15,10 @@ const lib_1 = require("./lib");
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);
// await PDFNet.initialize(licenseKey);
});
this.getPDFMeta = () => __awaiter(this, void 0, void 0, function* () {
if (!(this.document instanceof Buffer)) {
......@@ -49,17 +47,6 @@ class PDFparser {
throw new errors_1.GeneralError(error);
}
});
this.insertQrCode = (imgBytes, url, coords, scaleFactor, licenseKey, certPath, certTSAUrl) => __awaiter(this, void 0, void 0, function* () {
let buf;
try {
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");
}
return buf;
});
this.document = document;
}
}
......
export declare const TimestampAndEnableLTV: (docBuffer: ArrayBuffer, certPath: string, certTSAUrl: string, imgBytes: ArrayBuffer, coords: any) => Promise<ArrayBuffer>;
"use strict";
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) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
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);
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);
//make this dynamic with canvas lib
const imgWidth = 300;
const imgHeight = 300;
const pagesForSining = Object.keys(coords).map((k) => {
return parseInt(k);
});
const pages = yield doc.getPageCount();
let result;
for (let p = 1; p <= pages; p++) {
if (!pagesForSining.includes(p)) {
continue;
}
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);
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);
}
return result.buffer;
});
exports.TimestampAndEnableLTV = TimestampAndEnableLTV;
// import { PDFNet } from "@pdftron/pdfnet-node";
// export const TimestampAndEnableLTV = async (
// docBuffer: ArrayBuffer,
// certPath: string,
// certTSAUrl: string,
// imgBytes: ArrayBuffer,
// coords: any
// ): Promise<ArrayBuffer> => {
// const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
// doc.initSecurityHandler();
// const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
// certTSAUrl
// );
// const opts = await PDFNet.VerificationOptions.create(
// PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
// );
// await opts.addTrustedCertificateUString(certPath);
// const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
// //make this dynamic with canvas lib
// const imgWidth = 300;
// const imgHeight = 300;
// const pagesForSining = Object.keys(coords).map((k) => {
// return parseInt(k);
// });
// const pages = await doc.getPageCount();
// let result;
// for (let p = 1; p <= pages; p++) {
// if (!pagesForSining.includes(p)) {
// continue;
// }
// const page = await doc.getPage(p);
// const doctimestamp_signature_field =
// await doc.createDigitalSignatureField();
// const widgetAnnot =
// await PDFNet.SignatureWidget.createWithDigitalSignatureField(
// doc,
// new PDFNet.Rect(
// parseFloat(coords[p].x),
// parseFloat(coords[p].x + imgWidth),
// parseFloat(coords[p].y),
// parseFloat(coords[p].y + imgHeight)
// ),
// doctimestamp_signature_field
// );
// await page.annotPushBack(widgetAnnot);
// await widgetAnnot.createSignatureAppearance(img);
// await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
// result = await doc.saveMemoryBuffer(
// PDFNet.SDFDoc.SaveOptions.e_incremental
// );
// }
// return result.buffer;
// };
This diff is collapsed.
......@@ -5,8 +5,8 @@ import { verifyPDF } from "./lib";
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";
// import { TimestampAndEnableLTV } from "./utils";
// import { PDFNet } from "@pdftron/pdfnet-node";
type SealCoords = {
[key: string]: { x: string; y: string };
......@@ -27,7 +27,7 @@ class PDFparser {
}
initialize = async (licenseKey: string): Promise<void> => {
await PDFNet.initialize(licenseKey);
// await PDFNet.initialize(licenseKey);
};
getPDFMeta = async (): Promise<IGetMetaResponse> => {
......@@ -62,31 +62,31 @@ class PDFparser {
}
};
insertQrCode = async (
imgBytes: ArrayBuffer,
url: string,
coords: SealCoords,
scaleFactor: number,
licenseKey: string,
certPath: string,
certTSAUrl: string
): Promise<ArrayBuffer> => {
let buf;
try {
buf = await TimestampAndEnableLTV(
this.document,
certPath,
certTSAUrl,
imgBytes,
coords
);
} catch (error) {
console.log(error);
throw new GeneralError("Could Not sign pdf");
}
// insertQrCode = async (
// imgBytes: ArrayBuffer,
// url: string,
// coords: SealCoords,
// scaleFactor: number,
// licenseKey: string,
// certPath: string,
// certTSAUrl: string
// ): Promise<ArrayBuffer> => {
// let buf;
// try {
// buf = await TimestampAndEnableLTV(
// this.document,
// certPath,
// certTSAUrl,
// imgBytes,
// coords
// );
// } catch (error) {
// console.log(error);
// throw new GeneralError("Could Not sign pdf");
// }
return buf;
};
// return buf;
// };
}
export default PDFparser;
import { PDFNet } from "@pdftron/pdfnet-node";
// import { PDFNet } from "@pdftron/pdfnet-node";
export const TimestampAndEnableLTV = async (
docBuffer: ArrayBuffer,
certPath: string,
certTSAUrl: string,
imgBytes: ArrayBuffer,
coords: any
): Promise<ArrayBuffer> => {
const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
doc.initSecurityHandler();
const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
certTSAUrl
);
const opts = await PDFNet.VerificationOptions.create(
PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
);
// export const TimestampAndEnableLTV = async (
// docBuffer: ArrayBuffer,
// certPath: string,
// certTSAUrl: string,
// imgBytes: ArrayBuffer,
// coords: any
// ): Promise<ArrayBuffer> => {
// const doc = await PDFNet.PDFDoc.createFromBuffer(docBuffer);
// doc.initSecurityHandler();
// const tst_config = await PDFNet.TimestampingConfiguration.createFromURL(
// certTSAUrl
// );
// const opts = await PDFNet.VerificationOptions.create(
// PDFNet.VerificationOptions.SecurityLevel.e_compatibility_and_archiving
// );
await opts.addTrustedCertificateUString(certPath);
// await opts.addTrustedCertificateUString(certPath);
const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
// const img = await PDFNet.Image.createFromMemory2(doc, imgBytes);
//make this dynamic with canvas lib
const imgWidth = 300;
const imgHeight = 300;
// //make this dynamic with canvas lib
// const imgWidth = 300;
// const imgHeight = 300;
const pagesForSining = Object.keys(coords).map((k) => {
return parseInt(k);
});
// const pagesForSining = Object.keys(coords).map((k) => {
// return parseInt(k);
// });
const pages = await doc.getPageCount();
let result;
for (let p = 1; p <= pages; p++) {
if (!pagesForSining.includes(p)) {
continue;
}
const page = await doc.getPage(p);
// const pages = await doc.getPageCount();
// let result;
// for (let p = 1; p <= pages; p++) {
// if (!pagesForSining.includes(p)) {
// continue;
// }
// const page = await doc.getPage(p);
const doctimestamp_signature_field =
await doc.createDigitalSignatureField();
// const doctimestamp_signature_field =
// await doc.createDigitalSignatureField();
const widgetAnnot =
await PDFNet.SignatureWidget.createWithDigitalSignatureField(
doc,
new 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 =
// await PDFNet.SignatureWidget.createWithDigitalSignatureField(
// doc,
// new PDFNet.Rect(
// parseFloat(coords[p].x),
// parseFloat(coords[p].x + imgWidth),
// parseFloat(coords[p].y),
// parseFloat(coords[p].y + imgHeight)
// ),
// doctimestamp_signature_field
// );
await page.annotPushBack(widgetAnnot);
await widgetAnnot.createSignatureAppearance(img);
await doctimestamp_signature_field.timestampOnNextSave(tst_config, opts);
// await page.annotPushBack(widgetAnnot);
// 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(
// PDFNet.SDFDoc.SaveOptions.e_incremental
// );
// }
return result.buffer;
};
// return result.buffer;
// };
This diff is collapsed.