一、 準備jarcss
commons-collections-3.2.1.jarhtml
commons-lang-2.6.jarjava
core-renderer-chinese-1.0.0.jarapache
iText-2.0.8.jarapp
velocity-1.6.4.jar工具
二、 建立模板result.html字體
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">ui
<html xmlns="http://www.w3.org/1999/xhtml">spa
<head>.net
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
*{font-family: "STFangsong";}
</style>
</head>
<body>
文本內容
</body>
</html>
三、 準備字體D:\ STFANGSO.TTF
四、 調用工具類
五、 import java.io.BufferedOutputStream;
六、 import java.io.File;
七、 import java.io.FileOutputStream;
八、 import java.io.OutputStream;
九、 import java.io.StringWriter;
十、 import java.util.Map;
十一、 import java.util.Properties;
十二、 import org.apache.velocity.VelocityContext;
1三、 import org.apache.velocity.app.Velocity;
1四、 import org.apache.velocity.app.VelocityEngine;
1五、 import org.xhtmlrenderer.pdf.ITextFontResolver;
1六、 import org.xhtmlrenderer.pdf.ITextRenderer;
1七、
1八、 import com.lowagie.text.pdf.BaseFont;
2七、 public class PdfUtils{
2八、
2九、 private static final String PDF = ".pdf";
30、 private static final String HTML = ".html";
3一、
3二、 private static final VelocityEngine ENGINE = new VelocityEngine();
3三、
3四、 static {
3五、 Properties properties = new Properties();
3六、 properties.put(Velocity.FILE_RESOURCE_LOADER_PATH, "D:\\");
3七、 try {
3八、 ENGINE.init(properties);
3九、 } catch (Exception e) {
40、 // TODO Auto-generated catch block
4一、 e.printStackTrace();
4二、 }
4三、 }
4四、
4五、 /**
4六、 * 根據模板在指定目錄中生成文件,模板放在類路徑的"template/pdf/"下
4七、 *
4八、 * @param fileName
4九、 * 要生成文件名,不能爲空,可不帶後綴。
50、 * @param templatePath
5一、 * 模板文件,不能爲空,放在類路徑下的"template/pdf/"中,可不帶後綴。
5二、 * @param model
5三、 * 業務數據模型,可爲空。
5四、 * @return 生成的文件全路徑
5五、 *
5六、 * 若是建立失敗 ,則拋出業務異常
5七、 */
5八、 public static String create(String fileName, String templatePath,
5九、 Map<String, Object> model) {
60、 // 補齊後綴".html"
6一、 if (!templatePath.endsWith(HTML)) {
6二、 templatePath = templatePath + HTML;
6三、 }
6四、 // 補齊後綴".pdf"
6五、 if (!fileName.endsWith(PDF)) {
6六、 fileName = fileName + PDF;
6七、 }
6八、 // 構建保存文件夾
6九、 String savePath = "D:\\";
70、 // 若是保存文件夾不存在,則建立
7一、 File saveFile = new File(savePath);
7二、 if (!saveFile.exists()) {
7三、 saveFile.mkdirs();
7四、 }
7五、 // 構建完整保存路徑
7六、 savePath = savePath + File.separator + fileName;
7七、 // 若是文件已存在,則刪除
7八、 saveFile = new File(savePath);
7九、 if (saveFile.exists()) {
80、 boolean flag = saveFile.delete();
8一、 }
8二、 OutputStream os = null;
8三、 try {
8四、 StringWriter sw = new StringWriter();
8五、 ENGINE.getTemplate(templatePath, "UTF-8").merge(
8六、 new VelocityContext(model), sw);
8七、 String html = sw.toString();
8八、 ITextRenderer renderer = new ITextRenderer();
8九、 renderer.setDocumentFromString(html);
90、 // 解決中文漢字顯示問題
9二、 ITextFontResolver fontResolver = renderer.getFontResolver();
9三、 String baseURL = getBaseURL();
9六、 fontResolver.addFont(baseURL + "STFANGSO.TTF" ,
9七、 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
9八、 // 解決圖片加載問題
9九、 renderer.getSharedContext().setBaseURL(baseURL);
100、 renderer.layout();
10一、 os = new BufferedOutputStream(new FileOutputStream(savePath));
10二、 renderer.createPDF(os);
10三、 os.flush();
10四、 } catch (Exception e) {
10五、 } finally {
10六、 try {
10七、 if (os != null) {
10八、 os.close();
10九、 }
1十、 } catch (Exception e) {
1十一、 }
1十二、 }
11三、 System.out.println(savePath);
11四、 return savePath;
11五、 }
11六、
11七、 private static String getBaseURL() {
11八、 // 解決pdf圖片問題,以絕對路徑加載
11九、 // 注意,直接部署war會致使運行時獲取不到,必須改成部署解壓後的文件夾。
120、 String warPath = "D:\\";
12一、
12二、 return "D:\\";
12三、 }
12四、
12五、 }