創(chuàng)建步驟
先要創(chuàng)建模板文件,將要生成的pdf文件的大體內(nèi)容定下來,然后根據(jù)模板文件填充數(shù)據(jù)并存儲為html文件,最后將html文件轉(zhuǎn)換為pdf文件。
## 生成.html文件 ##
思路:首先要要創(chuàng)建模板文件,就是先要定個大體的框架出來就像蓋房子一樣先把地基打好,然后調(diào)用方法將你要動態(tài)添加的數(shù)據(jù)填充到文件中去。 示例模板如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>askforout.html
</title><meta http-equiv="content-type" content="text/html; charset=UTF-8"></meta><style type="text/css">body {font-family: SimSun;}table {word-break:break-all;border-collapse: collapse;table-layout: fixed;font-size: 18px;width:100%;text-align: center;margin-top: 10px;}td {word-break:break-all;word-wrap : break-word;padding:20px;width:100%;height:80px;}</style></head><body><table><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">關(guān)于同意${adept}單位在華召開${actionname}會議的批復(fù)函
<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">${adept}:
<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;"> 你所(中心)關(guān)于《${actionname}的請示》收悉。
<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">經(jīng)研究,原則同意你所(中心)于${strtime}至
<br/>${stoptime}在${address}舉辦${actionname}會議。
<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">請你單位根據(jù)相關(guān)外事管理規(guī)定和財務(wù)管理規(guī)定辦理有關(guān)事宜認(rèn)真組織會議和交流活動,并將會后有關(guān)情況及時上報我司。
<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">此復(fù)。
</span></td></tr><tr><td align="right"><span style="font-size:18.0pt; font-family:SimSun;"><br/>${approvetime}
</span></td></tr></table> </body></html>
</span></td></tr></table> </body>
</html>
注意:
模板文件要保存為.ftl格式
模版文件必須包含這兩句:
!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns=
"http://www.w3.org/1999/xhtml">
因為生成pdf文件必須是標(biāo)準(zhǔn)的html文件。
如果有中文則必須定義相對應(yīng)的字體類型,如下:
font-family:SimSun;不然中文無法顯示。
接下來根據(jù)模板文件填充動態(tài)數(shù)據(jù)并將模板文件存儲為標(biāo)準(zhǔn)的html文件,最后將html文件轉(zhuǎn)換為pdf
文件
kage com.aj.general.util;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.DefaultObjectWrapper;
import freemarker.template.Template;
import freemarker.template.TemplateException;
public class DocUtils {private Configuration configuration =
null;Log logger = LogFactory.getLog(DocUtils.class);String classPath = DocUtils.class.getClassLoader().getResource(
"/").getPath();String rootPath=classPath.substring(
1,classPath.indexOf(
"/WEB-INF/classes"))+
"/public/font/simsun.ttc";
private static DocUtils instances=
new DocUtils();
public static DocUtils
getInstances() {
return instances;}
public DocUtils() {configuration =
new Configuration();configuration.setDefaultEncoding(
"UTF-8");}
private Template
getTemplate(String templateName)
throws IOException {configuration.setClassForTemplateLoading(
this.getClass(),
"/com/template");Template t =
null;t = configuration.getTemplate(templateName);t.setEncoding(
"UTF-8");
return t;}
/*** 為創(chuàng)建pdf文件產(chǎn)生html靜態(tài)頁面* ***/public Boolean
CreateHtml(String htmlname, Map dataMap, Writer outhtmls,String srcpath)
throws Exception{
try { Template t = getTemplate(htmlname);configuration.setObjectWrapper(
new DefaultObjectWrapper()); String lastname=
".html";deleteFiles(srcpath,lastname);t.process(dataMap, outhtmls);}
catch (IOException e) {logger.error(e);
throw new Exception(e);}
catch (TemplateException e) {logger.error(e);
throw new Exception(e);}
finally {
try {outhtmls.close();}
catch (IOException e) {logger.error(e);
throw new Exception(e);}}
return true;}
/***利用IText將已經(jīng)生成的靜態(tài)頁面轉(zhuǎn)化為pdf格式* realpath 項目根目錄* frompath 靜態(tài)頁面路徑* topath 產(chǎn)生pdf批復(fù)函的路徑* srcpath 產(chǎn)生word路徑* **/public Boolean
ToPdf(String realpath,String frompath,String topath,String srcpath)
throws Exception{OutputStream os =
new FileOutputStream(topath);String lastname=
".pdf";deleteFiles(srcpath,lastname);ITextRenderer renderer =
new ITextRenderer(); String url =
new File(frompath).toURI().toURL().toString(); renderer.setDocument(url); ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont(realpath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);renderer.layout(); renderer.createPDF(os); os.flush(); os.close();
return true; }}
總結(jié)
以上是生活随笔為你收集整理的利用html创建pdf文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。