Skip to content

Commit

Permalink
Working on text signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
zboris12 committed Aug 4, 2024
1 parent fa4eb2d commit fd2b97d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 9 deletions.
58 changes: 58 additions & 0 deletions closure/pdflib-ext.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ PDFLib.PDFDocument.prototype.embedPng = function(png){};
* @returns {Promise<PDFLib.PDFImage>}
*/
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>}
*/
Expand Down Expand Up @@ -359,6 +381,8 @@ PDFLib.PDFImage.prototype.ref;
PDFLib.PDFFont = function(){};
/** @type {PDFLib.PDFRef} */
PDFLib.PDFFont.prototype.ref;
/** @type {string} */
PDFLib.PDFFont.prototype.name;
/** @constructor */
PDFLib.StandardFonts = function(){};

Expand Down Expand Up @@ -398,8 +422,42 @@ var PdfDrawimgOption;
/**
* @param {string} name
* @param {PdfDrawimgOption} options
* @return {Array<PDFLib.PDFOperator>}
*/
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
Expand Down
2 changes: 1 addition & 1 deletion closure/zb-externs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ var SignAreaInfo;
* imgData: (Array<number>|Uint8Array|ArrayBuffer|string|undefined),
* imgType: (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),
* font: (PDFLib.PDFFont|undefined),
* }}
Expand Down
1 change: 1 addition & 0 deletions lib/zganode.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const m_h = {
const z = require("./zgaindex.js");
z.forge = require("node-forge");
z.PDFLib = require("pdf-lib");
z.fontkit = require("@pdf-lib/fontkit");
/**
* @param {string} url
* @param {UrlFetchParams} params
Expand Down
78 changes: 73 additions & 5 deletions lib/zgapdfsigner.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ if(z.forge){
if(z.PDFLib){
var PDFLib = z.PDFLib;
}
if(z.fontkit){
var fontkit = z.fontkit;
}
//Only for nodejs End//

/** @type {Object<string, TsaServiceInfo>} */
Expand Down Expand Up @@ -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} */
var cert = _this.loadP12cert(_this.opt.p12cert, _this.opt.pwd);
/** @type {Zga.CertsChain} */
Expand Down Expand Up @@ -362,6 +381,8 @@ z.PdfSigner = class{
var dmydoc = await _this.addDss(pdfdoc);
if(dmydoc){
z.log("In order to enable LTV, DSS informations has been added to the pdf.");
}else{
await pdfdoc.flush();
}
// Clear ltv
_this.opt.ltv = 0;
Expand Down Expand Up @@ -1268,7 +1289,7 @@ z.SignatureCreator = class{
createStream(pdfdoc, signame){
if(!this.drawinf){
return null;
}else if(!(this.drawinf.img || (this.drawinf.font && this.drawinf.font))){
}else if(!(this.drawinf.img || this.drawinf.text)){
return null;
}

Expand Down Expand Up @@ -1316,10 +1337,18 @@ z.SignatureCreator = class{
};
sigOprs = sigOprs.concat(PDFLib.drawImage(imgName, this.calcDrawImgInf(pgrot, areainf)));
}
if(this.drawinf.font){
rscObj["Font"] = {
[fontName]: this.drawinf.font.ref,
};
if(this.drawinf.text){
/** @type {PDFLib.PDFHexString|undefined} */
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);
Expand Down Expand Up @@ -1445,6 +1474,45 @@ z.SignatureCreator = class{
}
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;
}
};

}
Expand Down
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zgapdfsigner",
"version": "2.5.2",
"version": "2.6.0",
"author": "zboris12",
"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",
Expand Down Expand Up @@ -33,9 +33,10 @@
"test": "node test4node.js"
},
"dependencies": {
"@pdf-lib/fontkit": "^1.1.1",
"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": {
"google-closure-compiler": "^20231112.0.0"
Expand Down

0 comments on commit fd2b97d

Please sign in to comment.