一、环境:
1、freemarker 2.3.21版本;
2、itext 2.1.7版本;
3、flying-saucer-core 11.0.7版本;
二、实现代码:
/** * Copyright (c) 2006-2016 Hzins Ltd. All Rights Reserved. * * This code is the confidential and proprietary information of * Hzins. You shall not disclose such Confidential Information * and shall use it only in accordance with the terms of the agreements * you entered into with Hzins,http://www.hzins.com. * */package com.hzins.utils.pdf;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.io.StringWriter;import java.util.Map;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.xhtmlrenderer.pdf.ITextFontResolver;import org.xhtmlrenderer.pdf.ITextRenderer;import org.xml.sax.SAXException;import com.lowagie.text.DocumentException;import com.lowagie.text.pdf.BaseFont;import freemarker.cache.StringTemplateLoader;import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;/** ** * * *
* * @author hz15041240 * @date 2016-10-11 下午7:45:33 * @version */public class PdfUtil { /** * ** * 根据Freemarker模板生成PDF文件并返回PDF字节数组 * *
* * @param templeteBuffer freemarker模板文件字符串 * @param templateName 模板名称 * @param map 模板参数 * @param basePath 模板中图片相对基础路径 * @param fontFile 字体文件路径 (simsun.ttc 字体) * @return * @throws IOException * @throws TemplateException * @throws ParserConfigurationException * @throws DocumentException * @throws SAXException * * @author hz15041240 * @date 2016-10-11 下午8:11:29 * @version */ public byte[] generatePdf(String templeteBuffer, String templateName, Mapmap, String basePath, String fontFile) throws IOException, TemplateException, ParserConfigurationException, DocumentException, SAXException { /** ----freemarker模板解析---- **/ Configuration configuration = new Configuration(Configuration.VERSION_2_3_21); configuration.setDefaultEncoding("utf-8"); StringTemplateLoader stringLoader = new StringTemplateLoader(); stringLoader.putTemplate(templateName, templeteBuffer); configuration.setTemplateLoader(stringLoader); Template template = configuration.getTemplate(templateName); StringWriter str = new StringWriter(); template.process(template, str); /** -------生成PDF------- **/ DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document doc = builder.parse(new ByteArrayInputStream(str.toString().getBytes())); ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null); // 解决中文支持问题 ITextFontResolver fontResolver = renderer.getFontResolver(); String simusun = fontFile; if (simusun != null) { fontResolver.addFont(simusun, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.getSharedContext().setFontResolver(fontResolver); } // 解决图片的相对路径问题 renderer.getSharedContext().setBaseURL("file:" + basePath); renderer.layout(); //返回PDF字节数组 一边上传文件服务器 ByteArrayOutputStream os = new ByteArrayOutputStream(1024000); renderer.createPDF(os); // 输出PDF文件 // OutputStream fileStream = new FileOutputStream("d:\\test.pdf"); // fileStream.write(os.toByteArray()); // fileStream.close(); return os.toByteArray(); }}
三:pom.xml详细:
4.0.0 com.xxxx.utils pdf-client-project 1.0.0-SNAPSHOT jar UTF-8 1.7.7 1.1.1 1.9 2.4 ch.qos.logback logback-classic ${logback.version} ch.qos.logback logback-core ${logback.version} org.slf4j slf4j-api ${slf4j-api.version} org.freemarker freemarker 2.3.21 org.xhtmlrenderer flying-saucer-pdf 11.0.7 com.lowagie itext 2.1.7 org.xhtmlrenderer flying-saucer-core 11.0.7 commons-codec commons-codec ${commons-codec.version} commons-io commons-io ${commons-io.version}
示例:
freemarker test.ftl
Document 页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉页眉
<#list insurantList as insurant> 编号1 名称 编号2 编号3 ${insurant.inssueNum} ${insurant.cname} ${insurant.cardNum} ${insurant.brithday}
生成的pdf效果:
图中有间隔是用两张图拼的,中间页就省略;
实现了页眉与页底+分页显示效果;