From b72d38473a39be40f2a0ca04c3ab7b861723b9c8 Mon Sep 17 00:00:00 2001 From: zboris12 Date: Mon, 19 Sep 2022 11:53:54 +0900 Subject: [PATCH] Fix garbled multi-bytes characters in the properties of the signature. --- zgapdfsigner.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/zgapdfsigner.js b/zgapdfsigner.js index eea2057..bb75c2b 100644 --- a/zgapdfsigner.js +++ b/zgapdfsigner.js @@ -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 0xFF){ + flg = true; + break; + } + } + if(flg){ + return PDFLib.PDFHexString.fromText(str); + }else{ + return PDFLib.PDFString.of(str); + } + } }, VisualSignature: class {