{"id":546,"date":"2025-11-26T09:38:54","date_gmt":"2025-11-26T01:38:54","guid":{"rendered":"https:\/\/www.hyw.life\/?p=546"},"modified":"2025-12-31T17:34:44","modified_gmt":"2025-12-31T09:34:44","slug":"jspdf-%e8%87%aa%e5%ae%9a%e4%b9%89%e9%a1%b5%e9%9d%a2%e5%a4%a7%e5%b0%8f","status":"publish","type":"post","link":"https:\/\/www.hyw.life\/?p=546","title":{"rendered":"jsPDF \u81ea\u5b9a\u4e49\u9875\u9762\u5927\u5c0f"},"content":{"rendered":"\n<span id=\"h2-0\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h2 class=\"wp-block-heading\">\u4f7f\u7528\u6807\u51c6\u9875\u9762\u5c3a\u5bf8<\/h2>\n\n\n\n<p>\u8981\u4f7f\u7528\u6807\u51c6\u9875\u9762\u5c3a\u5bf8\uff0c\u60a8\u53ef\u4ee5\u5728\u521b\u5efa<code>jsPDF<\/code>\u5b9e\u4f8b\u65f6\u6307\u5b9a\u9875\u9762\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2aA4\u5927\u5c0f\u7684PDF\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const doc = new jsPDF({\n    orientation: 'portrait', \/\/ \u9875\u9762\u65b9\u5411\uff0c\u53ef\u4ee5\u662f 'portrait' \u6216 'landscape'\n    unit: 'mm',             \/\/ \u9875\u9762\u5355\u4f4d\uff0c\u53ef\u4ee5\u662f 'pt' (\u70b9), 'mm' (\u6beb\u7c73), 'cm' (\u5398\u7c73), 'in' (\u82f1\u5bf8)\n    format: 'a4'            \/\/ \u9875\u9762\u683c\u5f0f\uff0c\u53ef\u4ee5\u662f 'a0', 'a1', 'a2', 'a3', 'a4' \u7b49\n});\n<\/code><\/pre>\n\n\n\n<span id=\"h2-1\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h2 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u9875\u9762\u5c3a\u5bf8<\/h2>\n\n\n\n<p>\u5982\u679c\u60a8\u9700\u8981\u81ea\u5b9a\u4e49\u9875\u9762\u5c3a\u5bf8\uff0c\u53ef\u4ee5\u5728\u521b\u5efa<code>jsPDF<\/code>\u5b9e\u4f8b\u65f6\u6307\u5b9a\u5bbd\u5ea6\u548c\u9ad8\u5ea6<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const doc = new jsPDF({\n    orientation: 'portrait', \/\/ \u9875\u9762\u65b9\u5411\n    unit: 'mm',             \/\/ \u9875\u9762\u5355\u4f4d\n    format: &#91;width, height] \/\/ \u81ea\u5b9a\u4e49\u9875\u9762\u5bbd\u5ea6\u548c\u9ad8\u5ea6\uff0c\u4f8b\u5982 &#91;210, 297] \u8868\u793aA4\u5927\u5c0f\u7684\u6beb\u7c73\u5355\u4f4d\u5bbd\u5ea6\u548c\u9ad8\u5ea6\n});\n<\/code><\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u521b\u5efa\u4e00\u4e2a\u5bbd\u5ea6\u4e3a210mm\u3001\u9ad8\u5ea6\u4e3a297mm\u7684A4\u5927\u5c0f\u7684PDF\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const doc = new jsPDF({\n    orientation: 'portrait',\n    unit: 'mm',\n    format: &#91;210, 297]\n});\n<\/code><\/pre>\n\n\n\n<span id=\"h2-2\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h2 class=\"wp-block-heading\">\u4fee\u6539\u5df2\u521b\u5efa\u6587\u6863\u7684\u9875\u9762\u5927\u5c0f<\/h2>\n\n\n\n<p>\u5982\u679c\u60a8\u5df2\u7ecf\u521b\u5efa\u4e86\u4e00\u4e2a<code>jsPDF<\/code>\u6587\u6863\uff0c\u5e76\u5e0c\u671b\u66f4\u6539\u5176\u9875\u9762\u5927\u5c0f\uff08\u5c3d\u7ba1\u8fd9\u4e0d\u662f\u901a\u5e38\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u4e00\u65e6\u6587\u6863\u521b\u5efa\uff0c\u5176\u9875\u9762\u5927\u5c0f\u901a\u5e38\u662f\u56fa\u5b9a\u7684\uff09\uff0c\u60a8\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u5f53\u524d\u9875\u9762\u5e76\u6dfb\u52a0\u4e00\u4e2a\u65b0\u9875\u9762\u6765\u5b9e\u73b0\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>doc.deletePage(1); \/\/ \u5220\u9664\u5f53\u524d\u9875\u9762\uff08\u5982\u679c\u6709\u591a\u4e2a\u9875\u9762\uff09\ndoc.addPage(&#91;newWidth, newHeight]); \/\/ \u6dfb\u52a0\u4e00\u4e2a\u65b0\u9875\u9762\uff0c\u6307\u5b9a\u65b0\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\n<\/code><\/pre>\n\n\n\n<p>\u4f8b\u5982\uff0c\u5c06\u9875\u9762\u5927\u5c0f\u66f4\u6539\u4e3aA5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>doc.deletePage(1); \/\/ \u5220\u9664\u5f53\u524d\u9875\u9762\uff08\u5982\u679c\u6709\u591a\u4e2a\u9875\u9762\uff09\ndoc.addPage('a5'); \/\/ \u6dfb\u52a0\u4e00\u4e2aA5\u5927\u5c0f\u7684\u9875\u9762\n<\/code><\/pre>\n\n\n\n<span id=\"h2-3\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h2 class=\"wp-block-heading\">\u4e0d\u65ad\u9875\u5bfc\u51fa\u6587\u6863<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>exportPDF: function (name) {\n  var element = document.getElementById(\"print_area\");\n  html2canvas(element, {\n      scale: 2,\n      allowTaint: true,\n      scale: window.devicePixelRatio || 1,\n      scrollY: 0,\n      scrollX: 0,\n      onrendered: function (canvas) {\n          var contentWidth = canvas.width;\n          var contentHeight = canvas.height;\n          canvas.imageSmoothingQuality = 'high'\n          canvas = Controller.sharpenCanvas(canvas)\n          var pageData = canvas.toDataURL(\"image\/png\", 2.0);\n          var pdf = new jsPDF({\n              unit: 'mm',\n              format: &#91;contentWidth, contentHeight]\n          });\n          pdf.addImage(pageData, \"PNG\", 0, 0, contentWidth, contentHeight);\n          pdf.save(name);\n          parent.layer.closeAll();\n          parent.layer.msg(\"\u5bfc\u51fa\u6210\u529f\", {\n              icon: 1,\n          });\n      },\n  });\n},\nsharpenCanvas(canvas) {\n  const ctx = canvas.getContext('2d');\n  const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);\n  ctx.putImageData(imageData, 0, 0);\n  return canvas;\n}<\/code><\/pre>\n\n\n\n<span id=\"h2-4\" class=\"heading-anchor\" aria-hidden=\"true\"><\/span><h2 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u4e00\u65e6\u6587\u6863\u88ab\u521b\u5efa\u5e76\u6dfb\u52a0\u4e86\u5185\u5bb9\uff0c\u66f4\u6539\u5176\u9875\u9762\u5927\u5c0f\u4e0d\u662f\u4e00\u4e2a\u63a8\u8350\u7684\u505a\u6cd5\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u7834\u574f\u5df2\u6dfb\u52a0\u5185\u5bb9\u7684\u5e03\u5c40\u3002\u901a\u5e38\uff0c\u6700\u597d\u5728\u6587\u6863\u521b\u5efa\u65f6\u786e\u5b9a\u5408\u9002\u7684\u9875\u9762\u5927\u5c0f\u3002<\/li>\n\n\n\n<li>\u786e\u4fdd\u5728\u8c03\u7528<code>addPage<\/code>\u4e4b\u524d\u5220\u9664\u6240\u6709\u73b0\u6709\u9875\u9762\uff0c\u4ee5\u907f\u514d\u51fa\u73b0\u591a\u4e2a\u9875\u9762\u7684\u60c5\u51b5\u3002\u5982\u679c\u53ea\u662f\u60f3\u66f4\u6539\u73b0\u6709\u9875\u9762\u7684\u5927\u5c0f\u800c\u4e0d\u6dfb\u52a0\u65b0\u9875\u9762\uff0c\u4e0a\u9762\u7684\u5220\u9664\u548c\u6dfb\u52a0\u65b0\u9875\u9762\u7684\u65b9\u6cd5\u53ef\u80fd\u4e0d\u662f\u6700\u4f73\u9009\u62e9\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u91cd\u65b0\u521b\u5efa\u6587\u6863\u53ef\u80fd\u662f\u66f4\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u7075\u6d3b\u5730\u8bbe\u7f6e<code>jsPDF<\/code>\u6587\u6863\u7684\u9875\u9762\u5927\u5c0f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u6807\u51c6\u9875\u9762\u5c3a\u5bf8 \u8981\u4f7f\u7528\u6807\u51c6\u9875\u9762\u5c3a\u5bf8\uff0c\u60a8\u53ef\u4ee5\u5728\u521b\u5efajsPDF\u5b9e\u4f8b\u65f6\u6307\u5b9a\u9875\u9762\u5927\u5c0f\u3002\u4f8b\u5982\uff0c\u8981\u521b\u5efa\u4e00\u4e2aA4\u5927\u5c0f\u7684PD [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":38,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7],"tags":[14,29],"class_list":["post-546","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-qd","category-plug","tag-js","tag-pdf"],"_links":{"self":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=546"}],"version-history":[{"count":3,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/546\/revisions"}],"predecessor-version":[{"id":562,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/posts\/546\/revisions\/562"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=\/wp\/v2\/media\/38"}],"wp:attachment":[{"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hyw.life\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}