Working on text signature.

sigtext
zboris12 2024-08-04 20:07:21 +09:00
parent fa4eb2d5c0
commit fd2b97d5b3
6 changed files with 154 additions and 9 deletions

View File

@ -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>}
*/
@ -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(){};
@ -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

View File

@ -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),
* }}

View File

@ -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

View File

@ -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>} */
@ -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} */
@ -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;
@ -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;
}
@ -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);
@ -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;
}
};
}

17
package-lock.json generated
View File

@ -9,6 +9,7 @@
"version": "2.5.2",
"license": "MIT",
"dependencies": {
"@pdf-lib/fontkit": "^1.1.1",
"follow-redirects": "1.15.6",
"node-forge": "1.3.1",
"pdf-lib": "1.17.1"
@ -17,6 +18,14 @@
"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": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",
@ -380,6 +389,14 @@
}
},
"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": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/@pdf-lib/standard-fonts/-/standard-fonts-1.0.0.tgz",

View File

@ -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",
@ -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"