package com.crc.commonreport.util; import java.awt.Insets; import java.io.ByteArrayOutputStream; import java.io.StringReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.zefer.pd4ml.PD4Constants; import org.zefer.pd4ml.PD4ML; import com.crc.accidentreport.util.WordUtil; public class HtmlToPdf { private static final Log log = LogFactory.getLog(WordUtil.class); public static byte[] generatePDF(StringReader strReader) { try { ByteArrayOutputStream os = new ByteArrayOutputStream(); PD4ML pd4ml = new PD4ML(); pd4ml.setPageInsets(new Insets(5, 5, 5, 5)); pd4ml.setHtmlWidth(900); pd4ml.setPageSize(PD4Constants.A4); // A4竖向显示 // pd4ml.changePageOrientation(PD4Constants.A4)横向显示 pd4ml.useTTF("java:fonts", true); pd4ml.setDefaultTTFs("SimHei", "YouYuan", "SimSun"); // fonts.jar的配置文件中的值,用于中文乱码 pd4ml.enableDebugInfo(); pd4ml.render(strReader, os); // 如果用上面的fos,就是写出到指定的文件 return os.toByteArray(); } catch (Exception e) { e.printStackTrace(); log.error("HtmlToPdf(方法:generatePDF,异常错误信息代码:" + e.getClass().getName() + "###"+ e.getMessage()); } return null; } public static void main(String[] args) throws Exception { StringBuffer sb = new StringBuffer(); sb.append("<!DOCTYPE html>"); sb.append("<html lang=\"en\">"); sb.append("<head>"); sb.append("<meta charset=\"UTF-8\" />"); sb.append("<meta http-equiv=\"X-UA-Compatible\" content=\"IE=9;IE=8;Chrome=1;IE=EDGE\" />"); sb.append("<title>法律报告导出模板</title>"); sb.append("<meta content=\"width=device-width, initial-scale=1\" name=\"viewport\" />"); sb.append("<style>.level-title { font-family: 华文仿宋; font-weight: bold; font-size: 22px; padding-top: 30px}.level-second-title { font-family: 华文仿宋; font-weight: bold; font-size: 20px; padding-left: 30px; padding-top: 25px}.level-second-title span { padding-right: 10px}.law-level-cont { font-family: 华文仿宋; font-size: 20px; text-indent: 30px; line-height: 24px; padding-top: 10px; word-wrap: break-word}</style>"); sb.append("</head>"); sb.append("<body class=\"table-body\" style=\"background:#fff;\">"); sb.append("<div class=\"start-issue\">"); sb.append("<div class=\"relieve-print-title\" style=\"text-align:center\">"); sb.append("<h2>华润集团2017年11月报告</h2>"); sb.append("</div> "); sb.append("<div class=\"legal-opinion-box\"> "); sb.append("<div class=\"panel panel-default floor\">"); sb.append("<div class=\"start-issue-mes form-horizontal clearfix\"> "); sb.append("<div class=\"level-title\">"); sb.append("一、投资并购"); sb.append("</div>"); sb.append("<div class=\"level-second-title\">"); sb.append("<span>1.</span>当月首次参与的投资并购项目的数目:"); sb.append("</div>"); sb.append("<div class=\"law-level-cont\">"); sb.append("<p><br /></p>"); sb.append("<table border=\"1\" style=\"text-align:center; width:90%\">"); sb.append("<tbody>"); sb.append("<tr>"); sb.append("<td>5</td>"); sb.append("<td>5</td>"); sb.append("<td>5</td>"); sb.append("<td>5</td>"); sb.append("<td>5</td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append(" <td>7</td>"); sb.append(" <td>7</td>"); sb.append("<td>7</td>"); sb.append("<td>7</td>"); sb.append("<td>7</td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("</tr>"); sb.append("<tr>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("<td><br /></td>"); sb.append("</tr>"); sb.append("</tbody>"); sb.append("</table>"); sb.append("<p><br /></p>"); sb.append("</div>"); sb.append("<div class=\"level-title\">"); sb.append("二、合同管理"); sb.append("</div>"); sb.append("<div class=\"level-second-title\">"); sb.append("<span>1.</span>合同管理制度性文件的制定、修订情况:"); sb.append("</div>"); sb.append("<div class=\"law-level-cont\"></div>"); sb.append("<div class=\"level-title\">"); sb.append("三、一级报告项"); sb.append("</div>"); sb.append("<div class=\"level-second-title\">"); sb.append("<span>1.</span>二级报告项01:"); sb.append("</div>"); sb.append("<div class=\"law-level-cont\"></div>"); sb.append("<div class=\"level-second-title\">"); sb.append("<span>2.</span>二级报告项02:"); sb.append("</div>"); sb.append("<div class=\"law-level-cont\"></div> "); sb.append("</div> "); sb.append("</div> "); sb.append("</div> "); sb.append("</div>"); sb.append("</body>"); sb.append("</html>"); StringReader strReader = new StringReader(sb.toString()); FileOperationUtil.writeFileFromByte(generatePDF(strReader), "D:\\test.pdf"); System.out.println(2); } }
需导入包:pd4ml_demo.jar ss_css2.jar fonts.jar