关于_java程序生成pdf文件
package com.demo;
import java.awt.Insets;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.StringReader;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
public class ExportPdfDemo {
public static void main(String[] args) throws Exception {
File pdfFile = new File("U:/mess/demo.pdf");
StringBuffer html = new StringBuffer();
html.append("<html>")
.append("<head>")
.append("<meta charset=\"GBK\">")
.append("<title>账单查询</title>")
.append("<style>")
.append(".fl{ float: left;}")
.append(".fr{ float: right;}")
.append(".position_span{ position:absolute; bottom:0; right:0;}")
.append(".col{ color: coral; font-size: 20px; margin: 0 10px;}")
.append(".position_span{ position:absolute; bottom:0; right:0;}")
.append(".blue{ color: #026bb1; margin: 0 6px;}.bdb_ddd{border-bottom: 1px solid #ddd;}")
.append(".bdr_ddd{border-right: 1px solid #ddd;}")
.append(".box_details{margin: 20px auto; width: 650px; font-size: 15px; }")
.append(".box_details{ width: 622px; font-size: 15px; margin: 20px auto;}")
.append(".box_content p{margin:0; padding: 20px 0 14px; position:relative;}")
.append(".content_tab{border-radius:6px;border: 1px solid #ddd; overflow: hidden;}")
.append(".content_tab table{ background: #fff; }")
.append(".content_tab table th{ background: #38A0E8;color: #fff; font-weight: normal; padding: 12px 10px;}")
.append(".content_tab table td{padding: 12px 10px;}")
.append(".content_bottom{margin-top: 10px; color: #999; font-size: 14px;position:relative;}")
.append(".clearfix:after{ display: block; content: \"\"; clear: both;}")
.append(".box_top {background: url(\"/resources/images/zd_top.png\") no-repeat center; width: 622px; height: 102px;}")
.append(".box_content {background: url(\"/resources/images/zd_content.png\") repeat-y; width: 622px;padding: 0 14px; box-sizing: border-box;}")
.append(".box_bottom { background: url(\"/resources/images/zd_bottom.png\") no-repeat center; width: 622px; height: 39px;}")
.append(".content_tab { border-radius: 6px; border: 1px solid #ddd; overflow: hidden;}")
.append(".box_details { font-size: 15px;}")
.append(".content_bottom { color: #999; font-size: 14px;")
.append(".box_details { font-size: 15px;}")
.append("body{font-family: 'KaiTi_GB2312',Helvetica,Tahoma, Lato, arial, sans-serif;color: #666}")
.append(".content_bottom { color: #999; font-size: 14px;margin-top: 10px;color: #999;font-size: 14px }")
.append(".content_tab table tr td { padding: 12px 10px;}")
.append("div.clearfix.content_bottom{width:100%;height:20px;}")
.append("span.fl{width:195px;height:20px;float: left;}")
.append("span.fr{width:195px;height:20px;float: right;}")
.append(".row{width:650px;margin:0 auto;}")
.append("html{width:650px;margin:0 auto;}")
.append("</style>")
.append("</head>")
.append("<body>")
.append("<div class=\"row\"><div class=\"col-sm-12\"><div class=\"box_details clearfix\"><div class=\"box_top\"></div><div class=\"box_content clearfix\">")
.append("<p>jacjk<span class=\"blue\"> </span>客户:</p> <p>您的账单从<span class=\"blue\">2017-05-12</span>至<span class=\"blue\">2017-05-13")
.append(" </span>共消费<span class=\"col\">1258500</span>元,上期余额<span class=\"col\">98511588</span>元,本期余额<span class=\"col\">485147725</span>元。</p>")
.append("<div class=\"content_tab\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"0\"><thead><tr><th>产品</th><th>单价</th><th>收费次数</th><th>消费金额</th></tr></thead>")
.append("<tbody>")
.append("")
.append("</tbody><tfoot><tr><td class=\"\"></td><td class=\"\"></td><td class=\"\"></td><td align=\"center\">总计:<span class=\"col\">58920</span></td>")
.append("</tr></tfoot></table></div> <div class=\"clearfix content_bottom\"><span class=\"fl\">账单编号:2017072600001</span>")
.append(" <span class=\"fr\">账单生成日期:2017-07-27</span> </div></div> <div class=\"box_bottom\"></div></div></div></div>")
.append("</font>")
.append("</body></html>");
StringReader strReader = new StringReader(html.toString());
System.out.println(html);
generatePDF_1(pdfFile, strReader);
}
public static void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception {
ByteArrayOutputStream baos=new ByteArrayOutputStream();
FileOutputStream fos = new FileOutputStream(outputPDFFile);
PD4ML pd4ml = new PD4ML();
pd4ml.setHtmlWidth(650);
pd4ml.setPageSize(PD4Constants.A4);
pd4ml.setPageInsets(new Insets(20, 13, 10, 10));
pd4ml.useTTF("java:fonts", true);
pd4ml.setDefaultTTFs("KaiTi_GB2312", "KaiTi_GB2312", "KaiTi_GB2312");
pd4ml.enableDebugInfo();
pd4ml.render(strReader, baos);
fos.write(baos.toByteArray());
fos.close();
}
}