日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

利用html创建pdf文件

發(fā)布時間:2023/12/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 利用html创建pdf文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

創(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;">&nbsp;&nbsp;&nbsp;&nbsp;你所(中心)關(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}&nbsp;&nbsp;&nbsp;&nbsp; </span></td></tr></table> </body></html>&nbsp;&nbsp;&nbsp;&nbsp; </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()); //判斷html是否已經(jīng)存在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);//判斷html是否已經(jīng)存在String lastname=".pdf";deleteFiles(srcpath,lastname);ITextRenderer renderer = new ITextRenderer(); String url = new File(frompath).toURI().toURL().toString(); renderer.setDocument(url); // 解決中文支持問題// System.out.println("-------rootPath:-------"+rootPath+"-------------");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)容還不錯,歡迎將生活随笔推薦給好友。