网上有好多的代码实例,好多都说可以解决中文以及样式问题,然而并不能,好不容易找到一篇,可以解决的,真心不容易,赶紧贴出来,备份一下。
import com.itextpdf.text.pdf.BaseFont; import org.xhtmlrenderer.pdf.ITextFontResolver; import org.xhtmlrenderer.pdf.ITextRenderer; import java.io.*; /** * 文件格式转换工具类 * * @author lbj * * 2015-10-8 上午10:52:22 */ public class FileTypeConvertUtil { /** * 将HTML转成PD格式的文件。html文件的格式比较严格 * @param htmlFile * @param pdfFile * @throws Exception */ // <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> public static void html2pdf(String htmlFile, String pdfFile) throws Exception { // step 1 String url = new File(htmlFile).toURI().toURL().toString(); System.out.println(url); // step 2 OutputStream os = new FileOutputStream(pdfFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(getXmlString()); // renderer.setDocument(url); // step 3 解决中文支持 ITextFontResolver fontResolver = renderer.getFontResolver(); if("linux".equals(getCurrentOperatingSystem())){ fontResolver.addFont("/usr/share/fonts/chiness/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); }else{ fontResolver.addFont("C:\Users\Spring\Desktop\simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); fontResolver.addFont("C:\Users\Spring\Desktop\arial.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); } renderer.layout(); renderer.createPDF(os); os.close(); System.out.println("create pdf done!!"); } public static String getCurrentOperatingSystem(){ String os = System.getProperty("os.name").toLowerCase(); System.out.println("---------当前操作系统是-----------" + os); return os; } public static String getXmlString() { String xmlString="<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> " + "<html lang="en" xmlns="http://www.w3.org/1999/xhtml"> " + "<head> " + " <meta charset="UTF-8"/> " + " <title>Hello World</title> " + " <style> " + " table.table-separate th{ " + " font-weight:bold; " + " font-size:14px; " + " border-top:1px solid #F3EDE9 !important; " + " } " + " table.table-separate td{ " + " padding: 13px 0; " + " font-weight:100; " + " } " + " .table-separate td.tit{ " + " background-color: #f4f9fe; " + " font-weight:normal; " + " padding:22px 0; " + " 15%; " + " } " + " .table-separate td.cont{ " + " text-align: left; " + " padding:16px 22px; " + " 85%; " + " line-height:175%; " + " } " + " .table-separate.no-border th{ " + " border:none; " + " text-align: left; " + " } " + " .table-separate.no-border td{ " + " text-align: left; " + " border:none; " + " } " + " @page { " + " size:210mm 297mm;//纸张大小A4 " + " margin: 0.25in; " + " -fs-flow-bottom: "footer"; " + " -fs-flow-left: "left"; " + " -fs-flow-right: "right"; " + " border: thin solid black; " + " padding: 1em; " + " } " + " #footer { " + " font-size: 90%; font-style: italic; " + " position: absolute; top: 0; left: 0; " + " -fs-move-to-flow: "footer"; " + " } " + " #pagenumber:before { " + " content: counter(page); " + " } " + " #pagecount:before {content: counter(pages); " + " } " + " table { " + " border-collapse: collapse; " + " table-layout: fixed; " + " word-break:break-all; " + " font-size: 10px; " + " 100%; " + " text-align: center; " + " } " + " td { " + " word-break:break-all; " + " word-wrap : break-word; " + " } " + " </style> " + " </head> " + "<body style = "font-family: SimSun;"> " + "<div id="footer" style=""> Page <span id="pagenumber"/> of <span id="pagecount"/> </div> " + "<div id="main"> " + " <div style="max-600px;margin:0 auto;padding:10px;"> " + " <div style="text-align: center; padding: 5mm 0;"> " + " <div style="font-weight: bold; font-size: 30px;"> HI Fudi&More</div> " + " <div> THANK YOU FOR SHOPPING WITH Fudi&More!</div> " + " </div> " + " <div style="border: 1px solid black; background-color: #f8f8f8; padding: 4mm;"> " + " <div style="font-size: 17px; font-weight: bold; border-bottom: 1px solid black; padding-bottom: 5mm;"> ORDER DETAILS</div> " + " <div style="padding-top: 10px;"> " + " <div><strong>Order: </strong>D-8C2Y Placed on 29/09/2019 10:04</div> " + " <div><strong>Carrier: </strong>Delivery</div> " + " <div><strong>Payment: </strong>Cash Payment</div> " + " </div> " + " </div> " + " <div style="margin-top: 4mm;"> " + " <table class="table-separate" cellpadding="0" cellspacing="0" style="max-600px;margin:0 auto;padding:10px;"> " + " <thead> " + " <tr style="text-align: center; height: 40px;"> " + " <th style=" 90px; background-color: #f8f8f8; border-top: 1px solid black; border-left: 1px solid black; border-right: 1px solid black;"> " + " Reference " + " </th> " + " <th colspan="2" style="background-color: #f8f8f8; border-top: 1px solid black; border-right: 1px solid black;">Product</th> " + " <th style=" 110px; background-color: #f8f8f8; border-top: 1px solid black; border-right: 1px solid black;">Unit price</th> " + " <th style=" 80px; background-color: #f8f8f8; border-top: 1px solid black; border-right: 1px solid black;">Quantity</th> " + " <th style=" 90px; background-color: #f8f8f8; border-top: 1px solid black; border-right: 1px solid black;">Total price</th> " + " </tr> " + " </thead> " + " <tbody> " + " <tr style="text-align: center; "> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black;"> " + " Main " + " </td> " + " <td colspan="2" " + " style="border-top: 1px solid black; border-bottom:1px solid black; border-right: 1px solid black; text-align: left; padding: 010px;"> " + " SweetSour Chicken " + " </td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 7.00</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">1</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 7.00</td> " + " </tr> " + " <tr style="text-align: center; "> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black;"> " + " Main " + " </td> " + " <td colspan="2" " + " style="border-top: 1px solid black; border-bottom:1px solid black; border-right: 1px solid black; text-align: left; padding: 010px;"> " + " Black Bean Stir Fry " + " </td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 9.00</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">1</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 9.00</td> " + " </tr> " + " <tr style="text-align: center; "> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black;"> " + " Pizzas " + " </td> " + " <td colspan="2" " + " style="border-top: 1px solid black; border-bottom:1px solid black; border-right: 1px solid black; text-align: left; padding: 010px;"> " + " Test Design Your Own 8" Pizza " + " </td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 6.00</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">1</td> " + " <td style="border-top: 1px solid black; border-bottom: 1px solid black; border-right: 1px solid black;">€ 6.00</td> " + " </tr> " + " </tbody> " + " <tfoot> " + " <tr style="text-align: center; height: 8mm;"> " + " <td colspan="5" " + " style="text-align: right; 90px; background-color: #f8f8f8; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 0 10px;"> " + " Item: " + " </td> " + " <td style="background-color: #f8f8f8; border-bottom: 1px solid black; border-right: 1px solid black;">3</td> " + " </tr> " + " <tr style="text-align: center; height: 8mm;"> " + " <td colspan="5" " + " style="text-align: right; 90px; background-color: #f8f8f8; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 0 10px;"> " + " Subtotal: " + " </td> " + " <td style="background-color: #f8f8f8; border-bottom: 1px solid black; border-right: 1px solid black;">€24.00</td> " + " </tr> " + " <tr style="text-align: center; height: 8mm;"> " + " <td colspan="5" " + " style="text-align: right; 90px; background-color: #f8f8f8; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 0 10px;"> " + " Deliver Fee: " + " </td> " + " <td style="background-color: #f8f8f8; border-bottom: 1px solid black; border-right: 1px solid black;">+€2.00</td> " + " </tr> " + " <tr style="text-align: center; height: 8mm;"> " + " <td colspan="5" " + " style="text-align: right; 90px; background-color: #f8f8f8; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 0 10px;"> " + " Discount: " + " </td> " + " <td style="background-color: #f8f8f8; border-bottom: 1px solid black; border-right: 1px solid black;">-€0.00</td> " + " </tr> " + " <tr style="text-align: center; height: 8mm;"> " + " <td colspan="5" " + " style="text-align: right; 90px; background-color: #f8f8f8; border-bottom: 1px solid black; border-left: 1px solid black; border-right: 1px solid black; padding: 0 10px;"> " + " Total: " + " </td> " + " <td style="background-color: #f8f8f8; border-bottom: 1px solid black; border-right: 1px solid black;">€24.00</td> " + " </tr> " + " </tfoot> " + " </table> " + " </div> " + " <div> " + " <div style="border: 1px solid black; background-color: #f8f8f8; padding:5mm; margin-top: 5mm;"> " + " <div style="font-size: 17px; font-weight: bold; border-bottom: 1px solid black; padding-bottom: 15px;"> DELIVERY ADDRESS</div> " + " <div style="padding-top: 10px;"> " + " <div><strong>guan</strong> ☎ <strong>13656690321</strong></div> " + " <div> 1024/ Edenhall,ModelFarmRd,Cork,爱尔兰,A 2048</div> " + " </div> " + " </div> " + " </div> " + " <div style="font-size: 13px;"><p>You can review your order and download your invoice from the "<a target="_blank" " + " href="http://www.fudiandmore.ie/#/FudiIndex/Order1">Order " + " history</a>"section of your customer account by clicking "<a target="_blank" href="http://www.fudiandmore.ie/#/FudiIndex/Personalcenter1">My " + " account</a>" on ourshop.</p></div> " + " <hr style="border- 5px;"/> " + " <div> Fudi,More powered by <a target="_blank" href="http://www.fudiandmore.ie">A2BLiving</a></div> " + " </div> " + "</div> " + "</body> " + "</html>"; StringBuffer stringBuffer=new StringBuffer(); return xmlString; } public static void main(String[] args) { // String htmlFile = "/home/lbj/sign.jsp"; // String pdfFile = "/home/lbj/sign.pdf"; String htmlFile = "C:\Users\Spring\Desktop\qqq.html"; String pdfFile = "C:\Users\Spring\Desktop\testoone2.pdf"; try { FileTypeConvertUtil.html2pdf(htmlFile, pdfFile); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } } }
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.9</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.5.9</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf-itext5</artifactId> <version>9.0.3</version> </dependency>
这就完了.直接运行没问题。还有比如 & 在正文中的要转义 & 要不然报错,格式一定要正确,</>结束符一定要有,标准也要有
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
这个缺了你可以试试,出了问题你自己该,还有一点很重要,body 中指定字体,不指定的,中文你就不要想它出现了。还有一些 分页啥的,样式里写好就行,要是超宽了,那就自己看着实例的样式改改,大概就好了。