Fix garbled multi-bytes characters in the properties of the signature.

pull/2/head
zboris12 2022-09-19 11:53:54 +09:00
parent 5b0aeadc53
commit b72d38473a
1 changed files with 25 additions and 4 deletions

View File

@ -86,13 +86,13 @@ PdfSigner: class {
}),
};
if(this.opt.reason){
signObj["Reason"] = PDFLib.PDFString.of(this.opt.reason);
signObj["Reason"] = this.convToPDFString(this.opt.reason);
}
if(this.opt.location){
signObj["Location"] = PDFLib.PDFString.of(this.opt.location);
signObj["Location"] = this.convToPDFString(this.opt.location);
}
if(this.opt.contact){
signObj["ContactInfo"] = PDFLib.PDFString.of(this.opt.contact);
signObj["ContactInfo"] = this.convToPDFString(this.opt.contact);
}
var signatureDictRef = pdfdoc.context.register(pdfdoc.context.obj(signObj));
@ -103,7 +103,7 @@ PdfSigner: class {
"FT": "Sig",
"Rect": visign.getSignRect(),
"V": signatureDictRef,
"T": PDFLib.PDFString.of(this.opt.signame ? this.opt.signame : "Signature1"),
"T": this.convToPDFString(this.opt.signame ? this.opt.signame : "Signature1"),
"F": 132,
"P": page.ref,
};
@ -239,6 +239,27 @@ PdfSigner: class {
return Zga.rawToU8arr(pdfstr);
}
/**
* @private
* @param {string} str
* @return {PDFLib.PDFString|PDFLib.PDFHexString}
*/
convToPDFString(str){
// Check if there is a multi-bytes char in the string.
var flg = false;
for(var i=0; i<str.length; i++){
if(str.charCodeAt(i) > 0xFF){
flg = true;
break;
}
}
if(flg){
return PDFLib.PDFHexString.fromText(str);
}else{
return PDFLib.PDFString.of(str);
}
}
},
VisualSignature: class {