Working on text signature.
parent
fa4eb2d5c0
commit
fd2b97d5b3
|
@ -60,6 +60,28 @@ PDFLib.PDFDocument.prototype.embedPng = function(png){};
|
||||||
* @returns {Promise<PDFLib.PDFImage>}
|
* @returns {Promise<PDFLib.PDFImage>}
|
||||||
*/
|
*/
|
||||||
PDFLib.PDFDocument.prototype.embedJpg = function(jpg){};
|
PDFLib.PDFDocument.prototype.embedJpg = function(jpg){};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef
|
||||||
|
* {{
|
||||||
|
* customName: (string|undefined),
|
||||||
|
* features: (Object<string, boolean>|undefined),
|
||||||
|
* subset: (boolean|undefined),
|
||||||
|
* }}
|
||||||
|
*/
|
||||||
|
var EmbedFontOptions;
|
||||||
|
/**
|
||||||
|
* @param {ArrayBuffer|Uint8Array} font
|
||||||
|
* @param {EmbedFontOptions=} options
|
||||||
|
* @returns {Promise<PDFLib.PDFFont>}
|
||||||
|
*/
|
||||||
|
PDFLib.PDFDocument.prototype.embedFont = function(font, options){};
|
||||||
|
/**
|
||||||
|
* @param {string} font
|
||||||
|
* @param {string=} customName
|
||||||
|
* @returns {PDFLib.PDFFont}
|
||||||
|
*/
|
||||||
|
PDFLib.PDFDocument.prototype.embedStandardFont = function(font, customName){};
|
||||||
/**
|
/**
|
||||||
* @returns {Promise<number>}
|
* @returns {Promise<number>}
|
||||||
*/
|
*/
|
||||||
|
@ -359,6 +381,8 @@ PDFLib.PDFImage.prototype.ref;
|
||||||
PDFLib.PDFFont = function(){};
|
PDFLib.PDFFont = function(){};
|
||||||
/** @type {PDFLib.PDFRef} */
|
/** @type {PDFLib.PDFRef} */
|
||||||
PDFLib.PDFFont.prototype.ref;
|
PDFLib.PDFFont.prototype.ref;
|
||||||
|
/** @type {string} */
|
||||||
|
PDFLib.PDFFont.prototype.name;
|
||||||
/** @constructor */
|
/** @constructor */
|
||||||
PDFLib.StandardFonts = function(){};
|
PDFLib.StandardFonts = function(){};
|
||||||
|
|
||||||
|
@ -398,8 +422,42 @@ var PdfDrawimgOption;
|
||||||
/**
|
/**
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
* @param {PdfDrawimgOption} options
|
* @param {PdfDrawimgOption} options
|
||||||
|
* @return {Array<PDFLib.PDFOperator>}
|
||||||
*/
|
*/
|
||||||
PDFLib.drawImage = function(name, options){};
|
PDFLib.drawImage = function(name, options){};
|
||||||
|
/**
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
PDFLib.Color = function(){};
|
||||||
|
/**
|
||||||
|
* @typedef
|
||||||
|
* {{
|
||||||
|
* color: PDFLib.Color,
|
||||||
|
* font: string,
|
||||||
|
* graphicsState: (string|undefined),
|
||||||
|
* lineHeight: number,
|
||||||
|
* size: number,
|
||||||
|
* rotate: (PDFLib.Rotation|undefined),
|
||||||
|
* xSkew: (PDFLib.Rotation|undefined),
|
||||||
|
* ySkew: (PDFLib.Rotation|undefined),
|
||||||
|
* x: number,
|
||||||
|
* y: number,
|
||||||
|
* }}
|
||||||
|
*/
|
||||||
|
var DrawLinesOfTextOptions;
|
||||||
|
/**
|
||||||
|
* @param {Array<PDFLib.PDFHexString>} lines
|
||||||
|
* @param {DrawLinesOfTextOptions} options
|
||||||
|
* @return {Array<PDFLib.PDFOperator>}
|
||||||
|
*/
|
||||||
|
PDFLib.drawLinesOfText = function(lines, options){};
|
||||||
|
/**
|
||||||
|
* @param {number} red
|
||||||
|
* @param {number} green
|
||||||
|
* @param {number} blue
|
||||||
|
* @return {PDFLib.Color}
|
||||||
|
*/
|
||||||
|
PDFLib.rgb = function(red, green, blue){};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @constructor
|
* @constructor
|
||||||
|
|
|
@ -26,7 +26,7 @@ var SignAreaInfo;
|
||||||
* imgData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
|
* imgData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
|
||||||
* imgType: (string|undefined),
|
* imgType: (string|undefined),
|
||||||
* text: (string|undefined),
|
* text: (string|undefined),
|
||||||
* fontData: (PDFLib.StandardFonts|Array<number>|Uint8Array|ArrayBuffer|string|undefined),
|
* fontData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
|
||||||
* img: (PDFLib.PDFImage|undefined),
|
* img: (PDFLib.PDFImage|undefined),
|
||||||
* font: (PDFLib.PDFFont|undefined),
|
* font: (PDFLib.PDFFont|undefined),
|
||||||
* }}
|
* }}
|
||||||
|
|
|
@ -6,6 +6,7 @@ const m_h = {
|
||||||
const z = require("./zgaindex.js");
|
const z = require("./zgaindex.js");
|
||||||
z.forge = require("node-forge");
|
z.forge = require("node-forge");
|
||||||
z.PDFLib = require("pdf-lib");
|
z.PDFLib = require("pdf-lib");
|
||||||
|
z.fontkit = require("@pdf-lib/fontkit");
|
||||||
/**
|
/**
|
||||||
* @param {string} url
|
* @param {string} url
|
||||||
* @param {UrlFetchParams} params
|
* @param {UrlFetchParams} params
|
||||||
|
|
|
@ -12,6 +12,9 @@ if(z.forge){
|
||||||
if(z.PDFLib){
|
if(z.PDFLib){
|
||||||
var PDFLib = z.PDFLib;
|
var PDFLib = z.PDFLib;
|
||||||
}
|
}
|
||||||
|
if(z.fontkit){
|
||||||
|
var fontkit = z.fontkit;
|
||||||
|
}
|
||||||
//Only for nodejs End//
|
//Only for nodejs End//
|
||||||
|
|
||||||
/** @type {Object<string, TsaServiceInfo>} */
|
/** @type {Object<string, TsaServiceInfo>} */
|
||||||
|
@ -329,6 +332,22 @@ z.PdfSigner = class{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(_this.opt.drawinf && _this.opt.drawinf.text && _this.opt.drawinf.fontData && !_this.opt.drawinf.font){
|
||||||
|
/** @type {Uint8Array|ArrayBuffer|string} */
|
||||||
|
var fontData2 = null;
|
||||||
|
if(Array.isArray(_this.opt.drawinf.fontData)){
|
||||||
|
fontData2 = new Uint8Array(_this.opt.drawinf.fontData);
|
||||||
|
}else{
|
||||||
|
fontData2 = _this.opt.drawinf.fontData;
|
||||||
|
}
|
||||||
|
if(typeof fontData2 == "string"){
|
||||||
|
_this.opt.drawinf.font = pdfdoc.embedStandardFont(fontData2);
|
||||||
|
}else{
|
||||||
|
pdfdoc.registerFontkit(fontkit);
|
||||||
|
_this.opt.drawinf.font = await pdfdoc.embedFont(fontData2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/** @type {forge_cert} */
|
/** @type {forge_cert} */
|
||||||
var cert = _this.loadP12cert(_this.opt.p12cert, _this.opt.pwd);
|
var cert = _this.loadP12cert(_this.opt.p12cert, _this.opt.pwd);
|
||||||
/** @type {Zga.CertsChain} */
|
/** @type {Zga.CertsChain} */
|
||||||
|
@ -362,6 +381,8 @@ z.PdfSigner = class{
|
||||||
var dmydoc = await _this.addDss(pdfdoc);
|
var dmydoc = await _this.addDss(pdfdoc);
|
||||||
if(dmydoc){
|
if(dmydoc){
|
||||||
z.log("In order to enable LTV, DSS informations has been added to the pdf.");
|
z.log("In order to enable LTV, DSS informations has been added to the pdf.");
|
||||||
|
}else{
|
||||||
|
await pdfdoc.flush();
|
||||||
}
|
}
|
||||||
// Clear ltv
|
// Clear ltv
|
||||||
_this.opt.ltv = 0;
|
_this.opt.ltv = 0;
|
||||||
|
@ -1268,7 +1289,7 @@ z.SignatureCreator = class{
|
||||||
createStream(pdfdoc, signame){
|
createStream(pdfdoc, signame){
|
||||||
if(!this.drawinf){
|
if(!this.drawinf){
|
||||||
return null;
|
return null;
|
||||||
}else if(!(this.drawinf.img || (this.drawinf.font && this.drawinf.font))){
|
}else if(!(this.drawinf.img || this.drawinf.text)){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1316,10 +1337,18 @@ z.SignatureCreator = class{
|
||||||
};
|
};
|
||||||
sigOprs = sigOprs.concat(PDFLib.drawImage(imgName, this.calcDrawImgInf(pgrot, areainf)));
|
sigOprs = sigOprs.concat(PDFLib.drawImage(imgName, this.calcDrawImgInf(pgrot, areainf)));
|
||||||
}
|
}
|
||||||
if(this.drawinf.font){
|
if(this.drawinf.text){
|
||||||
rscObj["Font"] = {
|
/** @type {PDFLib.PDFHexString|undefined} */
|
||||||
[fontName]: this.drawinf.font.ref,
|
var txt = undefined;
|
||||||
};
|
if(this.drawinf.font){
|
||||||
|
rscObj["Font"] = {
|
||||||
|
[fontName]: this.drawinf.font.ref,
|
||||||
|
};
|
||||||
|
txt = this.drawinf.font.encodeText(this.drawinf.text);
|
||||||
|
}else{
|
||||||
|
txt = PDFLib.PDFHexString.fromText(this.drawinf.text);
|
||||||
|
}
|
||||||
|
sigOprs = sigOprs.concat(PDFLib.drawLinesOfText([txt], this.calcDrawTextInf(pgrot, areainf, this.drawinf.font ? fontName : "Courier8")));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.rect = this.calcRect(pgrot.angle, areainf);
|
this.rect = this.calcRect(pgrot.angle, areainf);
|
||||||
|
@ -1445,6 +1474,45 @@ z.SignatureCreator = class{
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate informations for drawing text after rotate
|
||||||
|
*
|
||||||
|
* @private
|
||||||
|
* @param {PDFLib.Rotation} rot
|
||||||
|
* @param {SignAreaInfo} areainf // { x, y, w, h }
|
||||||
|
* @param {string=} font
|
||||||
|
* @return {DrawLinesOfTextOptions}
|
||||||
|
*/
|
||||||
|
calcDrawTextInf(rot, areainf, font){
|
||||||
|
/** @type {DrawLinesOfTextOptions} */
|
||||||
|
var ret = {
|
||||||
|
"x": 0,
|
||||||
|
"y": 10,
|
||||||
|
"color": PDFLib.rgb(0, 0, 0),
|
||||||
|
"font": font,
|
||||||
|
"lineHeight": 35,
|
||||||
|
"size": 35,
|
||||||
|
"rotate": rot,
|
||||||
|
"xSkew": PDFLib.degrees(0),
|
||||||
|
"ySkew": PDFLib.degrees(0),
|
||||||
|
};
|
||||||
|
switch(rot.angle){
|
||||||
|
case 90:
|
||||||
|
ret["x"] = areainf.w;
|
||||||
|
break;
|
||||||
|
case 180:
|
||||||
|
case -180:
|
||||||
|
ret["x"] = areainf.w;
|
||||||
|
ret["y"] = areainf.h;
|
||||||
|
break;
|
||||||
|
case 270:
|
||||||
|
case -90:
|
||||||
|
ret["y"] = areainf.h;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
"version": "2.5.2",
|
"version": "2.5.2",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pdf-lib/fontkit": "^1.1.1",
|
||||||
"follow-redirects": "1.15.6",
|
"follow-redirects": "1.15.6",
|
||||||
"node-forge": "1.3.1",
|
"node-forge": "1.3.1",
|
||||||
"pdf-lib": "1.17.1"
|
"pdf-lib": "1.17.1"
|
||||||
|
@ -17,6 +18,14 @@
|
||||||
"google-closure-compiler": "^20231112.0.0"
|
"google-closure-compiler": "^20231112.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/@pdf-lib/fontkit": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@pdf-lib/fontkit/-/fontkit-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-KjMd7grNapIWS/Dm0gvfHEilSyAmeLvrEGVcqLGi0VYebuqqzTbgF29efCx7tvx+IEbG3zQciRSWl3GkUSvjZg==",
|
||||||
|
"dependencies": {
|
||||||
|
"pako": "^1.0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/@pdf-lib/standard-fonts": {
|
"node_modules/@pdf-lib/standard-fonts": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
|
||||||
|
@ -380,6 +389,14 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pdf-lib/fontkit": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/@pdf-lib/fontkit/-/fontkit-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-KjMd7grNapIWS/Dm0gvfHEilSyAmeLvrEGVcqLGi0VYebuqqzTbgF29efCx7tvx+IEbG3zQciRSWl3GkUSvjZg==",
|
||||||
|
"requires": {
|
||||||
|
"pako": "^1.0.6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"@pdf-lib/standard-fonts": {
|
"@pdf-lib/standard-fonts": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "zgapdfsigner",
|
"name": "zgapdfsigner",
|
||||||
"version": "2.5.2",
|
"version": "2.6.0",
|
||||||
"author": "zboris12",
|
"author": "zboris12",
|
||||||
"description": "A javascript tool to sign a pdf or set protection to a pdf in web browser, Google Apps Script and nodejs.",
|
"description": "A javascript tool to sign a pdf or set protection to a pdf in web browser, Google Apps Script and nodejs.",
|
||||||
"homepage": "https://github.com/zboris12/zgapdfsigner",
|
"homepage": "https://github.com/zboris12/zgapdfsigner",
|
||||||
|
@ -33,9 +33,10 @@
|
||||||
"test": "node test4node.js"
|
"test": "node test4node.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pdf-lib/fontkit": "^1.1.1",
|
||||||
"follow-redirects": "1.15.6",
|
"follow-redirects": "1.15.6",
|
||||||
"pdf-lib": "1.17.1",
|
"node-forge": "1.3.1",
|
||||||
"node-forge": "1.3.1"
|
"pdf-lib": "1.17.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"google-closure-compiler": "^20231112.0.0"
|
"google-closure-compiler": "^20231112.0.0"
|
||||||
|
|
Loading…
Reference in New Issue