创建步骤

先要创建模板文件,将要生成的pdf文件的大体内容定下来,然后根据模板文件填充数据并存储为html文件,最后将html文件转换为pdf文件。
## 生成.html文件 ##
思路:首先要要创建模板文件,就是先要定个大体的框架出来就像盖房子一样先把地基打好,然后调用方法将你要动态添加的数据填充到文件中去。 示例模板如下:

<!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;">关于同意${adept}单位在华召开${actionname}会议的批复函<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;你所(中心)关于《${actionname}的请示》收悉。<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">经研究,原则同意你所(中心)于${strtime}至<br/>${stoptime}在${address}举办${actionname}会议。<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">请你单位根据相关外事管理规定和财务管理规定办理有关事宜认真组织会议和交流活动,并将会后有关情况及时上报我司。<br/></span></td></tr><tr><td align="left"><span style="font-size:18.0pt; font-family:SimSun;float:left;">此复。</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文件必须是标准的html文件。
如果有中文则必须定义相对应的字体类型,如下:
font-family:SimSun;不然中文无法显示。

接下来根据模板文件填充动态数据并将模板文件存储为标准的html文件,最后将html文件转换为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;}/*** 为创建pdf文件产生html静态页面* ***/public  Boolean CreateHtml(String htmlname, Map dataMap, Writer outhtmls,String srcpath) throws Exception{  try {  Template t = getTemplate(htmlname);configuration.setObjectWrapper(new DefaultObjectWrapper()); //判断html是否已经存在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将已经生成的静态页面转化为pdf格式* realpath 项目根目录* frompath 静态页面路径* topath 产生pdf批复函的路径* srcpath 产生word路径* **/public Boolean ToPdf(String realpath,String frompath,String topath,String srcpath)throws Exception{OutputStream os = new FileOutputStream(topath);//判断html是否已经存在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; }}

利用html创建pdf文件相关推荐

  1. 如何用python修改pdf内容_如何利用python将pdf文件转化为txt文件?

    https://www.wukong.com/answer/6579491774144708872/?iid=15906422033&app=news_article&share_an ...

  2. 利用Word制作pdf文件的方法

    利用Word制作pdf文件的方法 一.先用手机照成图片 二.把图片拖到word中 三.生成pdf文件 一.先用手机照成图片 二.把图片拖到word中 三.生成pdf文件 点文件 点导出 点创建PDF ...

  3. ABBYY在MS Office中创建PDF文件的方法

    2019独角兽企业重金招聘Python工程师标准>>> ABBYY PDF Transformer+是一款可创建.编辑及将PDF文件转换为其他可编辑格式的OCR图文识别软件,不仅可以 ...

  4. python使用fpdf创建pdf文件包含:页眉、页脚并嵌入logo图片、设置使用中文字体

    python使用fpdf创建pdf文件包含:页眉.页脚并嵌入logo图片.设置使用中文字体 #python使用fpdf创建页眉.页脚并嵌入logo图片.设置使用中文字体 from fpdf impor ...

  5. 使用iText库创建PDF文件

    前言 译文连接:http://howtodoinjava.com/apache-commons/create-pdf-files-in-java-itext-tutorial/ 对于excel文件的读 ...

  6. puppeteer api_使用Node.js和puppeteer API从URL创建PDF文件

    puppeteer api We will continue using Node.js and puppeteer which is a node library. As we saw in our ...

  7. 如何从服务器上取pdf文件,如何从服务器响应创建pdf文件?

    我在一个应用程序中工作,我需要从服务器上得到的响应创建pdf文件.有没有任何方法可以使用此响应创建pdf?反应如下: %PDF-1.4 %���� 2 0 obj <>st ...

  8. 利用Python提取PDF文件中的文本信息

    如何利用Python提取PDF文件中的文本信息 日常工作中我们经常会用到pdf格式的文件,大多数情况下是浏览或者编辑pdf信息,但有时候需要提取pdf中的文本,如果是单个文件的话还可以通过复制粘贴来直 ...

  9. PHP创建PDF文件(通过FPDF类库)

    最近项目中需要用到PHP创建PDF文档,初步筛选决定使用FPDF这个纯PHP库来实现,类似的还有TCPDF等,有机会也可以试试. FPDF官网:http://www.fpdf.org  logo是个可 ...

  10. 【第三方库】PHP实现创建PDF文件和编辑PDF文件

    目录 引入Setasign/fpdf.Setasign/fpdi 解决写入中文时乱码问题 1.下载并放置中文语言包(他人封装):https://github.com/DCgithub21/cd_FPD ...

最新文章

  1. java学习笔记7--抽象类与抽象方法
  2. iOS开发--TableView详细解释
  3. 阿里云ACM:云原生配置管理利器,让云上的Spring Cloud应用配置管理舞动起来
  4. 中海达gps软件wince_应用|无人机航测15分钟能做啥?中海达PPK告诉你答案
  5. vue——单文件组件
  6. TCP/IP参考模型和五层参考模型
  7. python 对象和json互相转换
  8. SpringBoot+H5微信登陆(网页)
  9. 当PCB设计师遇到爱情,猜猜他板内的阻抗有多大变化
  10. u890安装显示iis服务器,XP纯净版系统下安装用友U8提示iis不符合怎么办
  11. WordPress网站屏蔽后台搜索词广告-奇它博客
  12. 浏览器被劫持怎么解决?关于浏览器被劫持主页的处理方法
  13. 字典(python学习)
  14. 手机端或PC端利用高德地图和腾讯地图获取用户当前位置信息
  15. 测试案例中@SpringBootTest与@RunWith**的含义
  16. 如何修改Github仓库的description
  17. 在axure中实现商品数量加减效果,原型库网站讲师-金乌 解答同学问
  18. 真正的外企风范——毕博
  19. 如何找到与证明素数之一:小素数和概率素数的快速检验
  20. html大二实训大作业:蛋糕甜品网站设计——棕色蛋糕甜品店网页设计(4页) HTML+CSS+JavaScript HTML5期末大作业 web网页设计制作成品

热门文章

  1. pytorch的dataset用法详解
  2. 快速在多个word文件里面检索到关键字
  3. 存储过程与函数的区别
  4. c++使用libiconv
  5. 近世代数——Part2 群:基础与子群
  6. APQC 7.0.5通用版流程框架
  7. excel 个人日常记账——统计报表
  8. 【知识点总结】大数据技术原理与应用
  9. 一道携程SQL笔试题
  10. 【AppScan闪退】