使用freemaker和itext从html模板生成pdf文件
来源:互联网 发布:sql中union和union all 编辑:程序博客网 时间:2024/05/02 00:30
这里主要需要生成一个合同的pdf文档,拿到的合同格式是word文档,为了便于处理,我们用DW将其内容手动转为html。
所以接下来的问题主要就是如何从一个html的合同模板变成一个pdf文件。
首先我们定义好freemaker的util方法,使其易于调用
public class TemplateUtils {private static FreeMarkerConfigurer templateEngine = new FreeMarkerConfigurer();static {try {templateEngine.setConfiguration(templateEngine.createConfiguration());TemplateLoader loader = new FileTemplateLoader(new ClassPathResource("template").getFile());templateEngine.getConfiguration().setTemplateLoader(loader);} catch (IOException e) {e.printStackTrace();} catch (TemplateException e) {e.printStackTrace();}}public static String generateTemplateContent(String templateName, Map map) {try {Template t = templateEngine.getConfiguration().getTemplate(templateName);return FreeMarkerTemplateUtils.processTemplateIntoString(t, map);} catch (Exception e) {e.printStackTrace();}return null;}}调用时只需要传入模板文件名和一个参数的map就行了
String content = TemplateUtils.generateTemplateContent("contractTemplate.ftl", map);
其次,定义html转pdf 的util类
public class Html2Pdf {public static void print2Pdf(String templateHtml, String outputPath)throws IOException {File src = new File(templateHtml);if (!src.exists()) {throw new IllegalArgumentException("Can not find source html file."+ templateHtml);}String url = null;OutputStream os = null;ITextRenderer renderer = null;Document doc = new Document();// try {// PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(// new File(outputPath)));// doc.open();// BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",// BaseFont.NOT_EMBEDDED);// Font font = new Font(bf, 12, Font.NORMAL);// doc.add(new Paragraph(FileUtils.readFileToString(new File(// templateHtml)), font));// doc.close();// } catch (Exception e) {// e.printStackTrace();// }try {url = src.toURI().toURL().toString();os = new FileOutputStream(outputPath);//BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",//"UniGB-UCS2-H", false);renderer = new ITextRenderer();ITextFontResolver fontResolver = renderer.getFontResolver();fontResolver.addFont("C:/Windows/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);renderer.setDocument(url);renderer.layout();renderer.createPDF(os);os.close();} catch (Exception e) {throw new IOException(e);}}}这里我们用了fontResolver添加了中文的字体,这样pdf就能识别中文并转换了。
但是,这时候还没完,转换的中文问题还是没有解决,我们必须在html文档中加入这样一句标识所有body中的字体为simsun这种字体。
<style type="text/css" mce_bogus="1">body {font-family: SimSun;}</style>
- 使用freemaker和itext从html模板生成pdf文件
- itext使用模板生成pdf文件
- itext使用模板生成pdf文件
- itext使用html标签生成pdf文件
- 使用iText生成pdf文件
- 使用iText生成PDF文件
- Freemaker 模板生成html文件带barcode4j条形码并转PDF
- java 使用itextpdf.jar和itext-asian.jar根据pdf模板填写数据后生成一个新的pdf文件
- java-pdf-(itext+adobe acrobat+pdf模板)生成pdf文件
- itext html 生成pdf
- Java使用Freemarker和iText生成PDF文件
- itext pdf模板生成pdf
- 使用itext组件生成PDF文件
- 【Java】使用iText生成PDF文件
- Itext生成PDF文件
- Java IText使用PDF模板 生成 输出PDF
- 【java】 iText使用PDF模板生成输出PDF
- 【java】 iText使用PDF模板生成输出PDF
- java获取当前时间和日期的方法
- jQuery 中的动画
- 杭电1012
- VC:控制工具条的停靠位置
- 详细解析IOS中的CGContextAddArcToPoint方法的使用
- 使用freemaker和itext从html模板生成pdf文件
- Qt4.8.2 拖放技术
- [Effective DSA] 链表的反转
- Extjs-Flash报表 横向柱状图
- ubuntu10.04安装低版本的gcc
- NVIDIA官方中文版GPU编程指南v2.20(转载自GZeasy)
- 基于命令行模式进行开发ANDROID应用
- C++内存分配秘籍—new,malloc,GlobalAlloc详解
- poj 1925 spiderman