// TrustedURL starting let trustedURL; if (window.trustedTypes && trustedTypes.createPolicy) { const policy = trustedTypes.createPolicy('myPolicy', { createScriptURL: (input) => { return input; } }); trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'); } else { trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'; } // Download script starting - EDITED AND IMPROVED BY cirippo let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { // 使用新的全域變數名稱 const { jsPDF } = window.jspdf; console.log(jsPDF.version); let pdf = new jsPDF('p', 'pt', [2400, 3393]); let elements = document.getElementsByTagName("img"); for (let i = 0; i < elements.length; i++) { let img = elements[i]; console.log("add img ", img); if (!/^blob:/.test(img.src)) { console.log("invalid src"); continue; } let can = document.createElement('canvas'); let con = can.getContext("2d"); can.width = 3200; can.height = 4524; con.drawImage(img, 0, 0); let imgData = can.toDataURL("image/jpeg", 1.0); pdf.addImage(imgData, 'JPEG', 0, 0); if (i !== elements.length - 1) { pdf.addPage(); } } pdf.save("download.pdf"); console.log('PDF generated and saved'); }; jspdfScript.src = trustedURL; document.body.appendChild(jspdfScript); |
- 按F12。
- 在開發者工具的來源(Source)中view底下的這些圖檔,隨便點一張是該PDF頁面的圖,確認每頁的基礎尺寸。
P.S.:可看到尺寸是800*450 像素(px或pixels)。800就是橫的長度;450則是直的長度。順便提一下,基本上都是橫的長度乘直的長度。 - 將橫和直的長度都各乘4,並記住數值。
P.S.:別問為什麼乘4,這是試出來的。像在示範例子中會得出3200*1800 px,姑且先記叫做尺寸1。 - 隨便打開一個px to pt的單位換算計算機,將步驟三中得到的數值去換算,也把這些換算後的數值記住。
P.S.:我自己習慣用 https://pixelsconverter.com/px-to-pt 這個網站做換算,提供給大家參考看看。3200*1800 px換算後得出,2400*1350 pt,一樣記住,之後稱做尺寸2。
// TrustedURL starting let trustedURL; if (window.trustedTypes && trustedTypes.createPolicy) { const policy = trustedTypes.createPolicy('myPolicy', { createScriptURL: (input) => { return input; } }); trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'); } else { trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'; } // Download script starting - EDITED AND IMPROVED BY cirippo let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { // 使用新的全域變數名稱 const { jsPDF } = window.jspdf; console.log(jsPDF.version); let pdf = new jsPDF('p', 'pt', [2400, 3393]); let elements = document.getElementsByTagName("img"); for (let i = 0; i < elements.length; i++) { let img = elements[i]; console.log("add img ", img); if (!/^blob:/.test(img.src)) { console.log("invalid src"); continue; } let can = document.createElement('canvas'); let con = can.getContext("2d"); can.width = 3200; can.height = 4524; con.drawImage(img, 0, 0); let imgData = can.toDataURL("image/jpeg", 1.0); pdf.addImage(imgData, 'JPEG', 0, 0); if (i !== elements.length - 1) { pdf.addPage(); } } pdf.save("download.pdf"); console.log('PDF generated and saved'); }; jspdfScript.src = trustedURL; document.body.appendChild(jspdfScript); |
- 這句當中的第一項參數p,是指portrait,代表PDF每頁都是直的,講更明白一點,就是指橫的長度小於直的長度。但很明顯我們現在詳細教學的範例每頁都是橫的,那這時候就要把p換成l,l是指landscape,代表PDF的每頁都是橫的,也就是指橫的長度大於直的長度。
- 接著看pt,代表points,用於設定基礎單位,這個基礎單位可換成cm、mm或inch(實際上要打in)等單位。因為這邊設pt,所以要把尺寸1換算成pt,這也是為何我們在剛剛的檢查尺寸及其相關前置作業的步驟中要開單位換算計算機。換其他基礎單位就要用不同單位的換算計算機喔。
- 2400和3393則是在YT影片的簡易教學中示範約A4尺寸的pt尺寸,既然我們剛才有記住尺寸2,這時就要把尺寸2的數值換上去。2400改成2400(剛好不變);3393改成1350。
- 這兩句就更好解釋,width代表寬,就是所謂橫的長度;height代表高,也就是直的長度。看到這個3200是不是很眼熟?這個正是對應尺寸1,不過3200和4524是用於約A4尺寸的數值,因此一樣我們這邊要將尺寸1改上去,3200改3200(在此範例剛好不變);4524改1800。
// TrustedURL starting let trustedURL; if (window.trustedTypes && trustedTypes.createPolicy) { const policy = trustedTypes.createPolicy('myPolicy', { createScriptURL: (input) => { return input; } }); trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'); } else { trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'; } // Download script starting - EDITED AND IMPROVED BY cirippo let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { // 使用新的全域變數名稱 const { jsPDF } = window.jspdf; console.log(jsPDF.version); let pdf = new jsPDF('l', 'pt', [2400, 1350]); let elements = document.getElementsByTagName("img"); for (let i = 0; i < elements.length; i++) { let img = elements[i]; console.log("add img ", img); if (!/^blob:/.test(img.src)) { console.log("invalid src"); continue; } let can = document.createElement('canvas'); let con = can.getContext("2d"); can.width = 3200; can.height = 1800; con.drawImage(img, 0, 0); let imgData = can.toDataURL("image/jpeg", 1.0); pdf.addImage(imgData, 'JPEG', 0, 0); if (i !== elements.length - 1) { pdf.addPage(); } } pdf.save("download.pdf"); console.log('PDF generated and saved'); }; jspdfScript.src = trustedURL; document.body.appendChild(jspdfScript); |
// TrustedURL starting let trustedURL; if (window.trustedTypes && trustedTypes.createPolicy) { const policy = trustedTypes.createPolicy('myPolicy', { createScriptURL: (input) => { return input; } }); trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'); } else { trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'; } // Download script starting - EDITED AND IMPROVED BY cirippo let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { // 使用新的全域變數名稱 const { jsPDF } = window.jspdf; console.log(jsPDF.version); let pdf = new jsPDF('p', 'px', [2400, 3393]); let elements = document.getElementsByTagName("img"); for (let i = 0; i < elements.length; i++) { let img = elements[i]; console.log("add img ", img); if (!/^blob:/.test(img.src)) { console.log("invalid src"); continue; } let can = document.createElement('canvas'); let con = can.getContext("2d"); can.width = 3200; can.height = 4524; con.drawImage(img, 0, 0, can.width, can.height); let imgData = can.toDataURL("image/jpeg", 1.0); pdf.addImage(imgData, 'JPEG', 0, 0, 3200, 4524); if (i !== elements.length - 1) { pdf.addPage(); } } pdf.save("download.pdf"); console.log('PDF generated and saved'); }; jspdfScript.src = trustedURL; document.body.appendChild(jspdfScript); |
// TrustedURL starting let trustedURL; if (window.trustedTypes && trustedTypes.createPolicy) { const policy = trustedTypes.createPolicy('myPolicy', { createScriptURL: (input) => { return input; } }); trustedURL = policy.createScriptURL('https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'); } else { trustedURL = 'https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js'; } // Download script starting - EDITED AND IMPROVED BY cirippo let jspdfScript = document.createElement("script"); jspdfScript.onload = function () { // 使用新的全域變數名稱 const { jsPDF } = window.jspdf; console.log(jsPDF.version); let pdf = new jsPDF('l', 'px', [3200, 1800]); let elements = document.getElementsByTagName("img"); for (let i = 0; i < elements.length; i++) { let img = elements[i]; console.log("add img ", img); if (!/^blob:/.test(img.src)) { console.log("invalid src"); continue; } let can = document.createElement('canvas'); let con = can.getContext("2d"); can.width = 3200; can.height = 1800; con.drawImage(img, 0, 0, can.width, can.height); let imgData = can.toDataURL("image/jpeg", 1.0); pdf.addImage(imgData, 'JPEG', 0, 0, 3200, 1800); if (i !== elements.length - 1) { pdf.addPage(); } } pdf.save("download.pdf"); console.log('PDF generated and saved'); }; jspdfScript.src = trustedURL; document.body.appendChild(jspdfScript); |