{"id":109,"date":"2021-01-18T20:43:00","date_gmt":"2021-01-18T12:43:00","guid":{"rendered":"http:\/\/gjweb.top\/?p=109"},"modified":"2021-01-18T20:43:01","modified_gmt":"2021-01-18T12:43:01","slug":"%e5%b0%86%e8%87%aa%e5%ae%9a%e4%b9%89%e9%a1%b5%e9%9d%a2%e8%bd%ac%e6%8d%a2%e4%b8%bapdf%e6%a0%bc%e5%bc%8f%e5%b9%b6%e5%af%bc%e5%87%ba","status":"publish","type":"post","link":"https:\/\/gjweb.top\/?p=109","title":{"rendered":"\u5c06\u81ea\u5b9a\u4e49\u9875\u9762\u8f6c\u6362\u4e3aPDF\u683c\u5f0f\u5e76\u5bfc\u51fa"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u901a\u8fc7html2Canvas\u548cJsPDF\u5b9e\u73b0<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>npm install html2canvas --save\nnpm install jspdf --save<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728\u4f7f\u7528\u9875\u5f15\u5165<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>import html2Canvas from \"html2canvas\"\nimport JsPDF from \"jspdf\"<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728\u4e8b\u4ef6\u4e2d\u8c03\u7528<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>html2Canvas(document.getElementById(\"pdf\"))\n.then(function(canvas){\n      \/\/\u539f\u59cb\u7684canvas\u9ad8\u3001\u5bbd\n      let contentWidth = canvas.width;\n      let contentHeight = canvas.height;\n      \/\/\u8ba1\u7b97\u540e\u7684\u56fe\u7247\u9ad8\u5ea6 \u4e3aA4\u503c\u7684\u9ad8\u5ea6\n      let imgWidth = 595.28;\n      let imgHeight = 592.28\/contentWidth*contentHeight;\n      \/\/\u8f6c\u6362\u56fe\u7247\u7684\u7c7b\u578b\u548c\u8d28\u91cf1-0\n      let pageData = canvas.toDataURL(&#91;'image\/jpeg', 0]);\n      \/\/\u521b\u5efajsPDF\u5b9e\u4f8b\n      let PDF = new JsPDF('', 'pt', 'a4');\n      \/\/\u6dfb\u52a0\u56fe\u7247\u5730\u5740\uff0c\u7c7b\u578b\uff0c\u7b49\u53c2\u6570\n      PDF.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight);\n      \/\/\u5bfc\u51fapdf\u5e76\u4e0b\u8f7d\n      PDF.save( 'test'+'.pdf');\n  });<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u901a\u8fc7html2Canvas\u548cJsPDF\u5b9e\u73b0 \u5728\u4f7f\u7528\u9875\u5f15\u5165 \u5728\u4e8b\u4ef6\u4e2d\u8c03\u7528<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-109","post","type-post","status-publish","format-standard","hentry","category-vue"],"_links":{"self":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=109"}],"version-history":[{"count":0,"href":"https:\/\/gjweb.top\/index.php?rest_route=\/wp\/v2\/posts\/109\/revisions"}],"wp:attachment":[{"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gjweb.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}