UNPKG

@ironsoftware/ironpdf

Version:

IronPDF for Node

143 lines 8.39 kB
"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()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getVerifiedSignatures = exports.getSignatureCount = exports.signPdf = void 0; const fs_1 = __importDefault(require("fs")); const access_1 = require("../../access"); const util_1 = require("../util"); function signPdf(id, signature) { return __awaiter(this, void 0, void 0, function* () { const client = yield access_1.Access.ensureConnection(); const existing_sigs = yield getSignatureCount(id); return new Promise((resolve, reject) => { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y; const stream = client.Pdfium_Signature_Sign((err, value) => { if (err) { reject(`${err.name}/n${err.message}`); } else if (value) { if (value === null || value === void 0 ? void 0 : value.exception) { (0, util_1.handleRemoteException)(value.exception, reject); } resolve(); } }); const info = { document: { documentId: id }, password: (_a = signature.certificatePassword) !== null && _a !== void 0 ? _a : "", signingLocation: (_b = signature.signingLocation) !== null && _b !== void 0 ? _b : "", signingReason: (_c = signature.signingReason) !== null && _c !== void 0 ? _c : "", signaturePermission: { enumValue: 0 }, timeStampUrl: (_d = signature.timeStampUrl) !== null && _d !== void 0 ? _d : "", pageIndex: (_f = (_e = signature.signatureImage) === null || _e === void 0 ? void 0 : _e.SignatureImagePageIndex) !== null && _f !== void 0 ? _f : 0, internalName: "Signature" + (1 + existing_sigs + 1), signatureImageW: (_j = (_h = (_g = signature.signatureImage) === null || _g === void 0 ? void 0 : _g.SignatureImagePosition) === null || _h === void 0 ? void 0 : _h.width) !== null && _j !== void 0 ? _j : 100, signatureImageH: (_m = (_l = (_k = signature.signatureImage) === null || _k === void 0 ? void 0 : _k.SignatureImagePosition) === null || _l === void 0 ? void 0 : _l.height) !== null && _m !== void 0 ? _m : 100, signatureImageX: (_q = (_p = (_o = signature.signatureImage) === null || _o === void 0 ? void 0 : _o.SignatureImagePosition) === null || _p === void 0 ? void 0 : _p.x) !== null && _q !== void 0 ? _q : 0, signatureImageY: (_t = (_s = (_r = signature.signatureImage) === null || _r === void 0 ? void 0 : _r.SignatureImagePosition) === null || _s === void 0 ? void 0 : _s.y) !== null && _t !== void 0 ? _t : 0, }; const time = (_u = signature.signatureDate) === null || _u === void 0 ? void 0 : _u.getTime(); if (time) { info.signatureDate = { nanos: (time % 1000) * 1e6, seconds: time / 1000, }; } stream.write({ info: info }); let certBuffer = undefined; if (signature.certificateBuffer) { certBuffer = signature.certificateBuffer; } else if (signature.certificatePath) { certBuffer = fs_1.default.readFileSync(signature.certificatePath); } if (certBuffer) { (0, util_1.chunkBuffer)(certBuffer).forEach((chunk) => { stream.write({ certificateFileBytesChunk: chunk }); }); } let imageBuffer = undefined; if ((_v = signature.signatureImage) === null || _v === void 0 ? void 0 : _v.SignatureImageBuffer) { imageBuffer = (_w = signature.signatureImage) === null || _w === void 0 ? void 0 : _w.SignatureImageBuffer; } else if ((_x = signature.signatureImage) === null || _x === void 0 ? void 0 : _x.SignatureImagePath) { imageBuffer = fs_1.default.readFileSync((_y = signature.signatureImage) === null || _y === void 0 ? void 0 : _y.SignatureImagePath); } if (imageBuffer) { (0, util_1.chunkBuffer)(imageBuffer).forEach((chunk) => { stream.write({ signatureImageChunk: chunk }); }); } stream.end(); }); }); } exports.signPdf = signPdf; function getSignatureCount(id) { return __awaiter(this, void 0, void 0, function* () { const client = yield access_1.Access.ensureConnection(); return new Promise((resolve, reject) => { client.Pdfium_Signature_GetSignatureCount({ document: { documentId: id } }, (err, value) => { if (err) { reject(`${err.name}/n${err.message}`); } else if (value) { if (value === null || value === void 0 ? void 0 : value.exception) { (0, util_1.handleRemoteException)(value.exception, reject); } resolve(value.result == undefined ? 0 : value.result); } }); }); }); } exports.getSignatureCount = getSignatureCount; function getVerifiedSignatures(id) { return __awaiter(this, void 0, void 0, function* () { const client = yield access_1.Access.ensureConnection(); return new Promise((resolve, reject) => { const stream = client.Pdfium_Signature_GetVerifiedSignature((err, value) => { var _a, _b; if (err) { reject(`${err.name}/n${err.message}`); } else if (value) { if (value === null || value === void 0 ? void 0 : value.exception) { (0, util_1.handleRemoteException)(value.exception, reject); } const signatures = ((_b = (_a = value.verifiedSignatures) === null || _a === void 0 ? void 0 : _a.verifiedSignatures) !== null && _b !== void 0 ? _b : []).map((sig) => { var _a, _b, _c, _d, _e, _f, _g, _h; return ({ signatureName: (_a = sig.signatureName) !== null && _a !== void 0 ? _a : "", signingContact: (_b = sig.signingContact) !== null && _b !== void 0 ? _b : "", signingReason: (_c = sig.signingReason) !== null && _c !== void 0 ? _c : "", signingLocation: (_d = sig.signingLocation) !== null && _d !== void 0 ? _d : "", signingDate: sig.signingDate ? new Date(Number((_e = sig.signingDate.seconds) !== null && _e !== void 0 ? _e : 0) * 1000 + Math.floor(Number((_f = sig.signingDate.nanos) !== null && _f !== void 0 ? _f : 0) / 1e6)) : undefined, isValid: (_g = sig.isValid) !== null && _g !== void 0 ? _g : false, filter: (_h = sig.filter) !== null && _h !== void 0 ? _h : "", }); }); resolve(signatures); } }); stream.write({ info: { document: { documentId: id } } }); stream.end(); }); }); } exports.getVerifiedSignatures = getVerifiedSignatures; //# sourceMappingURL=signing.js.map