{"id":33581,"date":"2024-11-25T13:54:03","date_gmt":"2024-11-25T05:54:03","guid":{"rendered":"https:\/\/fwq.ai\/blog\/33581\/"},"modified":"2024-11-25T13:54:03","modified_gmt":"2024-11-25T05:54:03","slug":"%e7%a4%ba%e4%be%8b%e5%b0%8f%e7%a8%8b%e5%ba%8f%e7%94%9f%e6%88%90%e6%b5%b7%e6%8a%a5java%e5%90%8e%e7%ab%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/33581\/","title":{"rendered":"\u793a\u4f8b\u5c0f\u7a0b\u5e8f\u751f\u6210\u6d77\u62a5(java\u540e\u7aef)"},"content":{"rendered":"<p><strong><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/052\/5f4a1a7a640bb354.jpg\" class=\"aligncenter\" title=\"\u793a\u4f8b\u5c0f\u7a0b\u5e8f\u751f\u6210\u6d77\u62a5(java\u540e\u7aef)\u63d2\u56fe\" alt=\"\u793a\u4f8b\u5c0f\u7a0b\u5e8f\u751f\u6210\u6d77\u62a5(java\u540e\u7aef)\u63d2\u56fe\" \/><\/strong><\/p>\n<p><strong>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a<\/strong><strong>\u3011<\/strong><\/p>\n<h1>\u5b9e\u73b0\u65b9\u6848<\/h1>\n<blockquote><p>\n  \u524d\u7aef\u4eba\u5458\uff0c\u63d0\u4f9b\u76f8\u5173\u7684html\u9875\u9762\uff0c \u540e\u7aef\u4eba\u5458\u63d0\u4f9b\u63a5\u53e3\uff0c\u524d\u7aef\u4eba\u5458\u901a\u8fc7\u53c2\u6570\u8bbe\u7f6ehtml\u9875\u9762\u9700\u8981\u6e32\u67d3\u7684\u5185\u5bb9\uff0c \u6700\u540e\u4f7f\u7528wkhtmltoimage\u6216\u8005phantomjs \u5bf9html \u8fdb\u884c\u622a\u56fe\u751f\u6210\u6d77\u62a5, \u4e2a\u4eba\u611f\u89c9wkhtmltoiamge \u6bd4phantomjs \u8981\u5feb\u4e00\u70b9\uff0c\u7a33\u5b9a\u4e00\u70b9\u6211\u4e3b\u8981\u8bf4\u4e0bwkhtmltoimage\u7684\u5b9e\u73b0\u65b9\u6848\n<\/p><\/blockquote>\n<h1>\u5b9e\u73b0\u6b65\u9aa4<\/h1>\n<h2>&nbsp; \u5b89\u88c5\u73af\u5883<\/h2>\n<p>&nbsp; &nbsp; \u5b98\u7f51\u5730\u5740\uff1ahttps:\/\/wkhtmltopdf.org\/<\/p>\n<p>&nbsp; &nbsp; &nbsp;windows: \u4e0b\u8f7d\u5b89\u88c5\u5305\u5b89\u88c5\u5373\u53ef<\/p>\n<p>&nbsp; &nbsp; &nbsp;linux:&nbsp; \u4e0b\u8f7d\u5bf9\u5e94\u7684\u5b89\u88c5\u5305 \uff0c\u8fd8\u9700\u5b89\u88c5\u5bf9\u5e94\u4e2d\u6587\u5b57\u4f53\uff08phantomjs \u4e5f\u9700\u8981\u5b89\u88c5\u5b57\u4f53\uff09,<span style=\"color:#f33b45\">html\u4e2d\u9700\u8981\u58f0\u660e\u5f15\u7528<\/span><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<blockquote>\n<p>yum install libjpeg libXrender libXext xorg-x11-fonts-75dpi.noarch xorg-x11-fonts-Type1&nbsp;bitmap-fonts-cjk<\/p>\n<p>rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm<\/p>\n<p>\u5b89\u88c5\u5b57\u4f53<\/p>\n<p>yum install bitmap-fonts-cjk<\/p>\n<p>mkdir \/usr\/share\/fonts\/win<\/p>\n<p>\u62f7\u8d1d\u5b57\u4f53\u5230&nbsp;\/usr\/share\/fonts\/win\u4e0b<\/p>\n<p>cd \/usr\/share\/fonts\/win<br \/>mkfontscale<br \/>mkfontdir<br \/>fc-cache<\/p>\n<\/blockquote>\n<h2>\u76f8\u5173\u4ee3\u7801<\/h2>\n<p>&nbsp;\u5229\u7528java \u6267\u884c\u547d\u4ee4 \u8c03\u7528wkhtmltoImage \u8bbe\u7f6e\u76f8\u5173\u53c2\u6570\uff0c\u5177\u4f53\u53c2\u6570\u67e5\u770bwkhtmltoImage \u547d\u4ee4\u63d0\u793a<\/p>\n<pre>package&nbsp;com.yumingzhu.wxweb.util;\n\nimport&nbsp;java.io.BufferedReader;\nimport&nbsp;java.io.IOException;\nimport&nbsp;java.io.InputStreamReader;\n\n\/**\n&nbsp;*&nbsp;@Description&nbsp;\n&nbsp;*&nbsp;@Author&nbsp;yumigzhu\n&nbsp;*&nbsp;@Date&nbsp;2020\/7\/22&nbsp;20:12\n&nbsp;*\/\n\npublic&nbsp;class&nbsp;CustomWKHtmlToPdfUtil&nbsp;{\n\tprivate&nbsp;static&nbsp;String&nbsp;tempPath&nbsp;=&nbsp;\"C:\/apps\/tmpFile\";\/\/&nbsp;\u56fe\u7247\u4fdd\u5b58\u76ee\u5f55\n\n\tpublic&nbsp;static&nbsp;String&nbsp;getCommand(String&nbsp;htmlToImage,&nbsp;String&nbsp;sourceFilePath,&nbsp;String&nbsp;targetFilePath)&nbsp;{\n\t\t\/\/--quality&nbsp;\u8bbe\u7f6e\u4e3a50&nbsp;\u662f\u6bd4\u8f83\u5408\u9002\u7684\uff0c&nbsp;\u9ed8\u8ba4\u768494&nbsp;\u53ef\u80fd\u4f1a\u5bfc\u81f4\u56fe\u7247\u6587\u4ef6\u8fc7\u5927\n\t\tProcessBuilder&nbsp;pb&nbsp;=&nbsp;new&nbsp;ProcessBuilder(htmlToImage,&nbsp;\"--crop-w\",&nbsp;\"800\",&nbsp;\"--width\",&nbsp;\"800\",\"--quality\",&nbsp;\"50\",\n\t\t\t\tsourceFilePath,&nbsp;targetFilePath);\n\t\tProcess&nbsp;process;\n\t\ttry&nbsp;{\n\t\t\tprocess&nbsp;=&nbsp;pb.start();\n\t\t\t\/\/\u6ce8\u610f\uff0c\u8c03\u7528process.getErrorStream()\u800c\u4e0d\u662fprocess.getInputStream()\n\t\t\tBufferedReader&nbsp;errStreamReader&nbsp;=&nbsp;new&nbsp;BufferedReader(new&nbsp;InputStreamReader(process.getErrorStream()));\n\t\t\tSystem.out.println(\"read&nbsp;errstreamreader\");\n\t\t\tString&nbsp;line&nbsp;=&nbsp;null;\n\t\t\tline&nbsp;=&nbsp;errStreamReader.readLine();\n\t\t\twhile&nbsp;(line&nbsp;!=&nbsp;null)&nbsp;{\n\t\t\t\tSystem.out.println(line);\n\t\t\t\tline&nbsp;=&nbsp;errStreamReader.readLine();\n\t\t\t}\n\t\t\tprocess.destroy();\n\t\t\tSystem.out.println(\"destroyed&nbsp;process\");\n\t\t}&nbsp;catch&nbsp;(IOException&nbsp;e)&nbsp;{\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn&nbsp;targetFilePath;\n\t}\n\n\tpublic&nbsp;static&nbsp;void&nbsp;main(String[]&nbsp;args)&nbsp;throws&nbsp;Exception&nbsp;{\n\t\tString&nbsp;imagePath&nbsp;=&nbsp;tempPath&nbsp;+&nbsp;\"\/\"&nbsp;+&nbsp;System.currentTimeMillis()&nbsp;+&nbsp;\".png\";\/\/\u56fe\u7247\u8def\u5f84\n\t\tSystem.out.println(imagePath);\n\t\tString&nbsp;htmlToImage&nbsp;=&nbsp;\"E:\\softwareAPP\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\";\n\n\t\tCustomWKHtmlToPdfUtil.getCommand(htmlToImage,\n\t\t\t\t\"file:\/\/\/G:\/share\/text_none_title_share\/index.html\",\n\t\t\t\timagePath);\n\n\t\tSystem.out.println(\"\u6267\u884c\u5b8c\u6210\");\n\t}\n}<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2>\u8e29\u5751\u8bb0\u5f55<\/h2>\n<ul>\n<li>\n<p>\u5982\u679chtml\u9875\u9762\u8bbe\u7f6e\u7684\u5bbd\u9ad8\u6bd4\u8f83\u5c0f\uff0c \u8fd9\u6837\u622a\u51fa\u6765\u7684\u56fe\u7247\u4e5f\u4f1a\u6bd4\u8f83\u5c0f\uff0c\u6bd4\u8f83\u6a21\u7cca\uff0c\uff0c&nbsp; \u589e\u5927html \u7684\u5bbd\u9ad8\uff0c\u53ef\u4ee5\u4f7f\u56fe\u7247\u66f4\u6e05\u6670\uff0c\u8fd9\u6837\u4f1a\u5bfc\u81f4\u622a\u51fa\u6765\u7684\u56fe\u7247\u6587\u4ef6\u66f4\u5927\uff0c\u8fd9\u6837\u7528\u6237\u5728\u5c0f\u7a0b\u5e8f\u4e0b\u8f7d\u8fc7\u7a0b\u4f1a\u66f4\u6162\uff0c\u8fd9\u91cc\u9700\u8981\u81ea\u5df1\u6743\u8861<\/p>\n<\/li>\n<li>\n<p>wkhtmlImage \u5bf9 css3&nbsp; linear-gradient&nbsp; \u4e0d\u652f\u6301\uff0c\u4e0d\u80fd\u4f7f\u7528\u6837\u5f0f\u4e0b\u5212\u7ebf\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u56fe\u7247\u4ee3\u66ff<\/p>\n<\/li>\n<li>\n<p>\u4e2d\u6587\u5b57\u4f53\u9700\u8981\u58f0\u660e\u5f15\u7528\uff0c\u624d\u80fd\u751f\u6548<\/p>\n<\/li>\n<\/ul>\n<blockquote>\n<p>\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a<\/p>\n<\/blockquote>\n<p>\u4ee5\u4e0a\u5c31\u662f\u793a\u4f8b\u5c0f\u7a0b\u5e8f\u751f\u6210\u6d77\u62a5(java\u540e\u7aef)\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u76f8\u5173\u5b66\u4e60\u63a8\u8350\uff1a\u3011 \u5b9e\u73b0\u65b9\u6848 \u524d\u7aef\u4eba\u5458\uff0c\u63d0\u4f9b\u76f8\u5173\u7684html\u9875\u9762\uff0c \u540e\u7aef\u4eba\u5458\u63d0\u4f9b\u63a5\u53e3\uff0c\u524d\u7aef\u4eba\u5458\u901a\u8fc7\u53c2\u6570\u8bbe\u7f6ehtml\u9875\u9762\u9700\u8981\u6e32\u67d3\u7684\u5185\u5bb9\uff0c \u6700\u540e\u4f7f\u7528wkhtmltoimage\u6216\u8005phantomjs \u5bf9html \u8fdb\u884c\u622a\u56fe\u751f\u6210\u6d77\u62a5, \u4e2a\u4eba\u611f\u89c9wkhtmltoiamge \u6bd4phantomjs \u8981\u5feb\u4e00\u70b9\uff0c\u7a33\u5b9a\u4e00\u70b9\u6211\u4e3b\u8981\u8bf4\u4e0bwkhtmltoimage\u7684\u5b9e\u73b0\u65b9\u6848 \u5b9e\u73b0\u6b65\u9aa4 &nbsp; \u5b89\u88c5\u73af\u5883 &nbsp; &nbsp; \u5b98\u7f51\u5730\u5740\uff1ahttps:\/\/wkhtmltopdf.org\/ &nbsp; &nbsp; &nbsp;windows: \u4e0b\u8f7d\u5b89\u88c5\u5305\u5b89\u88c5\u5373\u53ef &nbsp; &nbsp; &nbsp;linux:&nbsp; \u4e0b\u8f7d\u5bf9\u5e94\u7684\u5b89\u88c5\u5305 \uff0c\u8fd8\u9700\u5b89\u88c5\u5bf9\u5e94\u4e2d\u6587\u5b57\u4f53\uff08phantomjs \u4e5f\u9700\u8981\u5b89\u88c5\u5b57\u4f53\uff09,html\u4e2d\u9700\u8981\u58f0\u660e\u5f15\u7528 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b yum install libjpeg libXrender libXext xorg-x11-fonts-75dpi.noarch xorg-x11-fonts-Type1&nbsp;bitmap-fonts-cjk rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm \u5b89\u88c5\u5b57\u4f53 yum install bitmap-fonts-cjk mkdir \/usr\/share\/fonts\/win \u62f7\u8d1d\u5b57\u4f53\u5230&nbsp;\/usr\/share\/fonts\/win\u4e0b cd \/usr\/share\/fonts\/winmkfontscalemkfontdirfc-cache \u76f8\u5173\u4ee3\u7801 &nbsp;\u5229\u7528java \u6267\u884c\u547d\u4ee4 \u8c03\u7528wkhtmltoImage \u8bbe\u7f6e\u76f8\u5173\u53c2\u6570\uff0c\u5177\u4f53\u53c2\u6570\u67e5\u770bwkhtmltoImage \u547d\u4ee4\u63d0\u793a package&nbsp;com.yumingzhu.wxweb.util; import&nbsp;java.io.BufferedReader; import&nbsp;java.io.IOException; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-33581","post","type-post","status-publish","format-standard","hentry","category-19"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33581","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=33581"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/33581\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=33581"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=33581"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=33581"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}