{"id":9707,"date":"2024-03-25T09:49:19","date_gmt":"2024-03-25T01:49:19","guid":{"rendered":"https:\/\/sdeno.com\/?p=9707"},"modified":"2024-03-25T09:49:19","modified_gmt":"2024-03-25T01:49:19","slug":"js%e6%89%93%e5%8d%b0","status":"publish","type":"post","link":"https:\/\/sdeno.com\/?p=9707","title":{"rendered":"js\u6253\u5370"},"content":{"rendered":"<p>\u5b89\u88c5html2canvas\u3001print-js<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;div ref=\"canvas\" class=\"a4-container\"&gt;\r\n   .\r\n   .\r\n   .\r\n   \/\/\u9700\u8981\u6253\u5370\u7684\u5185\u5bb9\r\n&lt;\/div&gt;\r\n\r\nimport html2canvas from 'html2canvas'\r\nimport printJS from 'print-js'\r\n\r\n\r\n methods: {\r\n\r\n     html2Canvas(printContent, callback){\r\n      \/\/ \u83b7\u53d6dom \u5bbd\u5ea6 \u9ad8\u5ea6\r\n      const width = printContent.clientWidth;\r\n      const height = printContent.clientHeight;\r\n\r\n      \/\/ \u521b\u5efa\u4e00\u4e2acanvas\u8282\u70b9\r\n      const canvas = document.createElement(\"canvas\");\r\n\r\n      const scale = 1; \/\/ \u5b9a\u4e49\u4efb\u610f\u653e\u5927\u500d\u6570\uff0c\u652f\u6301\u5c0f\u6570\uff1b\u8d8a\u5927\uff0c\u56fe\u7247\u6e05\u6670\u5ea6\u8d8a\u9ad8\uff0c\u751f\u6210\u56fe\u7247\u8d8a\u6162\u3002\r\n      canvas.width = width * scale; \/\/ \u5b9a\u4e49canvas \u5bbd\u5ea6 * \u7f29\u653e\r\n      canvas.height = height * scale; \/\/ \u5b9a\u4e49canvas\u9ad8\u5ea6 *\u7f29\u653e\r\n      canvas.style.width = width * scale + \"px\";\r\n      canvas.style.height = height * scale + \"px\";\r\n      canvas.getContext(\"2d\").scale(scale, scale); \/\/ \u83b7\u53d6context,\u8bbe\u7f6escale\r\n\r\n      const scrollTop =\r\n        document.documentElement.scrollTop || document.body.scrollTop; \/\/ \u83b7\u53d6\u6eda\u52a8\u8f74\u6eda\u52a8\u7684\u957f\u5ea6\r\n      const scrollLeft =\r\n        document.documentElement.scrollLeft || document.body.scrollLeft; \/\/ \u83b7\u53d6\u6c34\u5e73\u6eda\u52a8\u8f74\u7684\u957f\u5ea6\r\n\r\n      html2canvas(printContent, {\r\n        canvas,\r\n        backgroundColor: null,\r\n        useCORS: true,\r\n        windowHeight: document.body.scrollHeight,\r\n        scrollX: -scrollLeft, \/\/ \u89e3\u51b3\u6c34\u5e73\u504f\u79fb\u95ee\u9898\uff0c\u9632\u6b62\u6253\u5370\u7684\u5185\u5bb9\u4e0d\u5168\r\n        scrollY: -scrollTop,\r\n        background: \"#ffffff\", \/\/ \u4e00\u5b9a\u8981\u6dfb\u52a0\u80cc\u666f\u989c\u8272\uff0c\u5426\u5219\u51fa\u6765\u7684\u56fe\u7247\uff0c\u80cc\u666f\u5168\u90e8\u90fd\u662f\u900f\u660e\u7684\r\n      })\r\n        .then(canvas =&gt; {\r\n          const url = canvas.toDataURL(\"image\/png\");\r\n          \/\/console.log(\"canvas url :\" + url);\r\n          callback({ url: url });\r\n        })\r\n        .catch(err =&gt; {\r\n          console.error(err);\r\n        });\r\n    },\r\n\r\n\r\n    printImg(url, callback){\r\n      printJS({\r\n        printable: url,\r\n        type: \"image\",\r\n        documentTitle: \"\", \/\/ \u6807\u9898\r\n        style: \"@page{size:auto;margin: 1cm ;}\", \/\/ \u53bb\u9664\u9875\u7709\u9875\u811a\r\n        onStart: () =&gt; {\r\n          console.log(\"\u6253\u5370\u5f00\u59cb\");\r\n        },\r\n        onEnd: () =&gt; {\r\n          console.log(\"\u6253\u5370\u5b8c\u6210\");\r\n        }\r\n      });\r\n    },\r\n\r\n\r\n    saveAsImages() {\r\n      this.html2Canvas(this.$refs.canvas, res =&gt; {\r\n        this.printDomUrl = res.url;\r\n        this.printImg(res.url, printRes =&gt; {});\r\n      });\r\n    }\r\n    \r\n\r\n }<\/pre>\n<p>\u5982\u679c\u9884\u89c8\u6253\u5370\u7684\u5185\u5bb9\u4e0d\u5168\uff0c\u53ef\u4ee5\u914d\u5408\u4fee\u6539\u6837\u5f0f\u4fee\u590d<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5html2canvas\u3001print-js &lt;div ref=&#8221;canvas&#8221; class=&#8221;a4-c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9707","post","type-post","status-publish","format-standard","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/9707","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9707"}],"version-history":[{"count":0,"href":"https:\/\/sdeno.com\/index.php?rest_route=\/wp\/v2\/posts\/9707\/revisions"}],"wp:attachment":[{"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sdeno.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}