Fixed a bug of break line.
parent
0505fd1e0e
commit
de687d8bf7
|
@ -51,6 +51,10 @@ Just import the dependencies and this tool.
|
||||||
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
|
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
|
||||||
<script src="https://cdn.jsdelivr.net/npm/zgapdfsigner/dist/zgapdfsigner.min.js" type="text/javascript"></script>
|
<script src="https://cdn.jsdelivr.net/npm/zgapdfsigner/dist/zgapdfsigner.min.js" type="text/javascript"></script>
|
||||||
```
|
```
|
||||||
|
When using feature of drawing text, importing the fontkit library is necessary.
|
||||||
|
```html
|
||||||
|
<script src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js" type="text/javascript"></script>
|
||||||
|
```
|
||||||
|
|
||||||
### [Google Apps Script](https://developers.google.com/apps-script)
|
### [Google Apps Script](https://developers.google.com/apps-script)
|
||||||
Load the dependencies and this tool.
|
Load the dependencies and this tool.
|
||||||
|
@ -64,6 +68,8 @@ function setTimeout(func, sleep){
|
||||||
var window = globalThis;
|
var window = globalThis;
|
||||||
// Load pdf-lib
|
// Load pdf-lib
|
||||||
eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js").getContentText());
|
eval(UrlFetchApp.fetch("https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js").getContentText());
|
||||||
|
// It is necessary for drawing text feature.
|
||||||
|
eval(UrlFetchApp.fetch("https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js").getContentText());
|
||||||
// Load node-forge
|
// Load node-forge
|
||||||
eval(UrlFetchApp.fetch("https://unpkg.com/node-forge@1.3.1/dist/forge.min.js").getContentText());
|
eval(UrlFetchApp.fetch("https://unpkg.com/node-forge@1.3.1/dist/forge.min.js").getContentText());
|
||||||
// Load ZgaPdfSigner
|
// Load ZgaPdfSigner
|
||||||
|
|
|
@ -1275,7 +1275,7 @@ z.SignatureCreator = class{
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
var j = sarr2[0] ? parseInt(sarr2[0], 10) : 0;
|
var j = sarr2[0] ? parseInt(sarr2[0], 10) : 0;
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
var ed = sarr2[sarr2.length - 1] ? parseInt(sarr2[sarr2.length - 1], 10) : (pgcnt ? pgcnt -1 : j);
|
var ed = sarr2[sarr2.length - 1] ? parseInt(sarr2[sarr2.length - 1], 10) : (pgcnt ? pgcnt - 1 : j);
|
||||||
while(j <= ed){
|
while(j <= ed){
|
||||||
this.pgidxs.push(j);
|
this.pgidxs.push(j);
|
||||||
j++;
|
j++;
|
||||||
|
@ -1721,7 +1721,18 @@ z.SignatureCreator = class{
|
||||||
}else{
|
}else{
|
||||||
/** @type {number} */
|
/** @type {number} */
|
||||||
var width = computeWidthOfText(word);
|
var width = computeWidthOfText(word);
|
||||||
if(currWidth + width > maxWidth){
|
if(width > maxWidth){
|
||||||
|
if(idx > 0){
|
||||||
|
lines.push(currLine);
|
||||||
|
currLine = "";
|
||||||
|
currWidth = 0;
|
||||||
|
}
|
||||||
|
/** @type {SplitLongWordResult} */
|
||||||
|
var slwr = this.splitLongWord(word, width, maxWidth, computeWidthOfText);
|
||||||
|
lines = lines.concat(slwr.words);
|
||||||
|
word = slwr.lastWord;
|
||||||
|
width = slwr.lastWidth;
|
||||||
|
}else if(currWidth + width > maxWidth){
|
||||||
lines.push(currLine);
|
lines.push(currLine);
|
||||||
currLine = "";
|
currLine = "";
|
||||||
currWidth = 0;
|
currWidth = 0;
|
||||||
|
@ -1736,6 +1747,58 @@ z.SignatureCreator = class{
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
* @param {string} word
|
||||||
|
* @param {number} wordWidth
|
||||||
|
* @param {number} maxWidth
|
||||||
|
* @param {function(string):number} computeWidthOfText
|
||||||
|
* @return {SplitLongWordResult}
|
||||||
|
*/
|
||||||
|
splitLongWord(word, wordWidth, maxWidth, computeWidthOfText){
|
||||||
|
/** @type {Array<string>} */
|
||||||
|
var splited = [];
|
||||||
|
/** @type {number} */
|
||||||
|
var wordLen = word.length;
|
||||||
|
while(wordWidth > maxWidth){
|
||||||
|
/** @type {number} */
|
||||||
|
var maxIdx = Math.floor(wordLen * maxWidth / wordWidth) - 1;
|
||||||
|
/** @type {number} */
|
||||||
|
var w = computeWidthOfText(word.substring(0, maxIdx + 1));
|
||||||
|
if(w > maxWidth){
|
||||||
|
while(w > maxWidth){
|
||||||
|
maxIdx--;
|
||||||
|
w -= computeWidthOfText(word.charAt(maxIdx));
|
||||||
|
}
|
||||||
|
maxIdx++;
|
||||||
|
}else{
|
||||||
|
while(w < maxWidth){
|
||||||
|
maxIdx++;
|
||||||
|
if(maxIdx < wordLen){
|
||||||
|
/** @type {number} */
|
||||||
|
var w2 = w + computeWidthOfText(word.charAt(maxIdx));
|
||||||
|
if(w2 > maxWidth){
|
||||||
|
break;
|
||||||
|
}else{
|
||||||
|
w = w2;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
splited.push(word.substring(0, maxIdx));
|
||||||
|
word = word.substring(maxIdx);
|
||||||
|
wordLen -= maxIdx;
|
||||||
|
wordWidth -= w;
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
words: splited,
|
||||||
|
lastWord: word,
|
||||||
|
lastWidth: wordWidth,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @private
|
* @private
|
||||||
* @param {DrawLinesOfTextOptions} opts
|
* @param {DrawLinesOfTextOptions} opts
|
||||||
|
@ -1771,10 +1834,21 @@ z.SignatureCreator = class{
|
||||||
newopts["y"] = w - x;
|
newopts["y"] = w - x;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return newopts;
|
return newopts;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @typedef
|
||||||
|
* {{
|
||||||
|
* words: Array<string>,
|
||||||
|
* lastWord: string,
|
||||||
|
* lastWidth: number,
|
||||||
|
* }}
|
||||||
|
*/
|
||||||
|
var SplitLongWordResult;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Only for nodejs Start//
|
//Only for nodejs Start//
|
||||||
|
|
34
test.html
34
test.html
|
@ -5,6 +5,7 @@
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title> Test for ZgaPdfSigner </title>
|
<title> Test for ZgaPdfSigner </title>
|
||||||
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js" type="text/javascript"></script>
|
<script src="https://unpkg.com/pdf-lib@1.17.1/dist/pdf-lib.min.js" type="text/javascript"></script>
|
||||||
|
<script src="https://unpkg.com/@pdf-lib/fontkit/dist/fontkit.umd.min.js" type="text/javascript"></script>
|
||||||
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
|
<script src="https://unpkg.com/node-forge@1.3.1/dist/forge.min.js" type="text/javascript"></script>
|
||||||
<script src="dist/zgapdfsigner.min.js" type="text/javascript"></script>
|
<script src="dist/zgapdfsigner.min.js" type="text/javascript"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
@ -63,6 +64,10 @@ async function testMe(){
|
||||||
if(img){
|
if(img){
|
||||||
imgType = getFilExt("img");
|
imgType = getFilExt("img");
|
||||||
}
|
}
|
||||||
|
/** @type {string} */
|
||||||
|
var txt = document.getElementById("txt").value;
|
||||||
|
/** @type {ArrayBuffer} */
|
||||||
|
var font = await readFile("font");
|
||||||
|
|
||||||
/** @type {ArrayBuffer} */
|
/** @type {ArrayBuffer} */
|
||||||
var pubcert = await readFile("pubcert");
|
var pubcert = await readFile("pubcert");
|
||||||
|
@ -81,17 +86,30 @@ async function testMe(){
|
||||||
contact: document.getElementById("tContact").value,
|
contact: document.getElementById("tContact").value,
|
||||||
debug: true,
|
debug: true,
|
||||||
};
|
};
|
||||||
if(img){
|
if(img || txt){
|
||||||
sopt.drawinf = {
|
sopt.drawinf = {
|
||||||
area: {
|
area: {
|
||||||
x: 25, // left
|
x: 25, // left
|
||||||
y: 150, // top
|
y: 150, // top
|
||||||
w: 60,
|
w: txt ? undefined : 60,
|
||||||
h: 60,
|
h: txt ? undefined : 100,
|
||||||
},
|
},
|
||||||
// pageidx: 2,
|
pageidx: "-",
|
||||||
imgData: img,
|
imgInfo: img ? {
|
||||||
imgType: imgType,
|
imgData: img,
|
||||||
|
imgType: imgType,
|
||||||
|
} : undefined,
|
||||||
|
textInfo: txt ? {
|
||||||
|
text: txt,
|
||||||
|
fontData: font,
|
||||||
|
color: "f00",
|
||||||
|
size: 16,
|
||||||
|
align: 2,
|
||||||
|
wMax: 80,
|
||||||
|
yOffset: 10,
|
||||||
|
xOffset: 20,
|
||||||
|
noBreaks: "[あいうえおA-Za-z0-9]",
|
||||||
|
} : undefined,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -145,7 +163,7 @@ function test(){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
function clearFiles(){
|
function clearFiles(){
|
||||||
["fff","kkk","img","pubcert"].forEach((a_id) => {
|
["fff","kkk","img","font","pubcert"].forEach((a_id) => {
|
||||||
document.getElementById(a_id).value = "";
|
document.getElementById(a_id).value = "";
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -187,6 +205,8 @@ span.header {
|
||||||
<label>certificate </label><input type="file" id="kkk" /><br />
|
<label>certificate </label><input type="file" id="kkk" /><br />
|
||||||
<label>passphrase </label><input type="password" id="pwd" /><br />
|
<label>passphrase </label><input type="password" id="pwd" /><br />
|
||||||
<label>signature image </label><input type="file" id="img" /><br />
|
<label>signature image </label><input type="file" id="img" /><br />
|
||||||
|
<label>signature text </label><input type="text" id="txt" /><br />
|
||||||
|
<label>text font </label><input type="file" id="font" /><br />
|
||||||
<label>permission </label>
|
<label>permission </label>
|
||||||
<select id="sperm" onchange="changeSperm()">
|
<select id="sperm" onchange="changeSperm()">
|
||||||
<option value="0">No DocMDP</option>
|
<option value="0">No DocMDP</option>
|
||||||
|
|
|
@ -125,7 +125,7 @@ async function addtsa(pdfPath){
|
||||||
/** @type {Uint8Array} */
|
/** @type {Uint8Array} */
|
||||||
var u8dat = await ser.sign(pdf);
|
var u8dat = await ser.sign(pdf);
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
var outPath = m_path.join(__dirname, workpath+"test_tsa.pdf");
|
var outPath = m_path.join(__dirname, workpath+"tsa_"+m_path.basename(pdfPath));
|
||||||
m_fs.writeFileSync(outPath, u8dat);
|
m_fs.writeFileSync(outPath, u8dat);
|
||||||
console.log("Output file: " + outPath);
|
console.log("Output file: " + outPath);
|
||||||
return outPath;
|
return outPath;
|
||||||
|
@ -159,7 +159,7 @@ async function main1(angle){
|
||||||
}
|
}
|
||||||
|
|
||||||
if(pfxPath){
|
if(pfxPath){
|
||||||
await sign_protect(pdfPath, pfxPath, ps, 1, imgPath, "あいうえおか\r\n\nThis is a test of text!\n", fontPath);
|
await sign_protect(pdfPath, pfxPath, ps, 1, imgPath, "あいうえおあいうえおか\r\n\nThis is a test of text!\n", fontPath);
|
||||||
pdfPath = await sign_protect(pdfPath, pfxPath, ps, 2, undefined, "ありがとうご\r\n\nThis is an another test of text!\n", fontPath);
|
pdfPath = await sign_protect(pdfPath, pfxPath, ps, 2, undefined, "ありがとうご\r\n\nThis is an another test of text!\n", fontPath);
|
||||||
await addtsa(pdfPath);
|
await addtsa(pdfPath);
|
||||||
}else{
|
}else{
|
||||||
|
|
Loading…
Reference in New Issue