使用freeMark作为模板导出pdf
pom引入如下:
- <span style="white-space:pre"> </span><dependency>
- <groupId>org.xhtmlrenderer</groupId>
- <artifactId>flying-saucer-pdf-itext5</artifactId>
- <version>9.1.1</version>
- </dependency>
- <dependency>
- <artifactId>freemarker</artifactId>
- <groupId>freemarker</groupId>
- <version>2.3.8</version>
- </dependency>
- package com.test.domains.ceshi.pdf.demo;
- import freemarker.template.Configuration;
- public class FreemarkerConfiguration {
- private static Configuration config = null;
- /**
- * Static initialization.
- *
- * Initialize the configuration of Freemarker.
- */
- static{
- config = new Configuration();
- config.setClassForTemplateLoading(FreemarkerConfiguration.class, "/template/");
- config.setTemplateUpdateDelay(0);
- }
- public static Configuration getConfiguation(){
- return config;
- }
- }
- package com.test.domains.ceshi.pdf.demo;
- import java.io.BufferedWriter;
- import java.io.StringWriter;
- import java.util.Map;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- public class HtmlGenerator {
- /**
- * @param template
- * @param variables
- * @return
- * @throws Exception
- */
- public static String generate(String template, Map params) throws Exception{
- Configuration config = FreemarkerConfiguration.getConfiguation();
- config.setDefaultEncoding("UTF-8");
- Template tp = config.getTemplate(template);
- StringWriter stringWriter = new StringWriter();
- BufferedWriter writer = new BufferedWriter(stringWriter);
- tp.setEncoding("UTF-8");
- tp.process(params, writer);
- String htmlStr = stringWriter.toString();
- writer.flush();
- writer.close();
- return htmlStr;
- }
- }
编写user类用于给前台
- package com.test.domains.ceshi.pdf.demo;
- public class User {
- private String id;
- private String name;
- private String ip;
- private String address;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getIp() {
- return ip;
- }
- public void setIp(String ip) {
- this.ip = ip;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
编写主程序,调用上面文件
- package com.test.domains.ceshi.pdf.demo;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.net.URLEncoder;
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.xhtmlrenderer.pdf.ITextFontResolver;
- import org.xhtmlrenderer.pdf.ITextRenderer;
- import com.itextpdf.text.pdf.BaseFont;
- public class PdfTest {
- public static void main(String[] args) throws Exception {
- List<User> userList = new ArrayList<User>();
- for(int i =0;i<19;i++){
- User user = new User();
- user.setId(i+"");
- user.setName("张三"+i);
- user.setAddress("北京市海点"+i);
- user.setIp("192.168.1."+i);
- userList.add(user);
- }
- exportPDF(userList);
- }
- public static void exportPDF(List<User> userList) throws Exception {
- OutputStream os = null;
- String htmlStr;
- Map<String, Object> params = new HashMap<String, Object>();
- Map data = new HashMap();
- try {
- /**
- * xxx数据生成逻辑
- **/
- data.put("userList", userList);
- data.put("projects", "aaa");
- // 通过freemaker模板生成html
- htmlStr = HtmlGenerator.generate("pdf.ftl", data);
- ITextRenderer renderer = new ITextRenderer();
- String appPath = "";
- try{
- renderer.setDocumentFromString(htmlStr);
- }catch(Exception e){
- }
- // 解决中文支持问题
- ITextFontResolver fontResolver = renderer.getFontResolver();
- fontResolver.addFont("/template" + File.separator + "simsun.ttc", BaseFont.IDENTITY_H,
- BaseFont.NOT_EMBEDDED);
- os = new FileOutputStream(new File("D:/pdf.pdf"));
- renderer.layout();
- renderer.createPDF(os, true);
- os.flush();
- System.err.println("生成pdf成功");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != os) {
- try {
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
至此可完成模板导出pdf文件