iText Freemarker模板生成导出PDF及部署到Linux

2024-06-21 06:08

本文主要是介绍iText Freemarker模板生成导出PDF及部署到Linux,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.工程结构


2.maven

  <dependencies><dependency><groupId>javax.servlet</groupId><artifactId>servlet-api</artifactId><version>2.5</version><scope>provided</scope></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.11</version></dependency><dependency><groupId>com.itextpdf.tool</groupId><artifactId>xmlworker</artifactId><version>5.5.11</version></dependency><dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency><!--freemarker--><dependency><groupId>org.freemarker</groupId><artifactId>freemarker</artifactId><version>2.3.26-incubating</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency></dependencies>

3 PDFKit.java

package com.servlet;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;import javax.servlet.http.HttpServletResponse;import java.io.*;
import java.nio.charset.Charset;public class PDFKit {public static class AsianFontProvider extends XMLWorkerFontProvider {@Overridepublic Font getFont(final String fontname, String encoding, float size, final int style) {String fntname = fontname;if (fntname == null) {/*使用的windows里的宋体,可将其文件放资源文件中引入*/String classpath = PDFKit.class.getClassLoader().getResource("").getPath();String fontPath =classpath+"fonts/simsun.ttc";fntname = fontPath;}if (size == 0) {size = 4;}System.out.println(fntname);return super.getFont(fntname, encoding, size, style);}}/*** 生成PDF到输出流中(ServletOutputStream用于下载PDF)* @param data 输入到FTL中的数据* @param response HttpServletResponse* @return*/public  void exportToResponse(Object data, HttpServletResponse response){try{String classpath = PDFKit.class.getClassLoader().getResource("").getPath();String filePath = classpath + "templates/hello.ftl";String html= FreeMarkerUtil.getContent(filePath,data);System.out.println(html);OutputStream out = null;out = response.getOutputStream();Document document = new Document(PageSize.A4);PdfWriter writer = PdfWriter.getInstance(document, out);convertToPDF(writer,document,html);// out.close();}catch (Exception ex){ex.printStackTrace();}}/*** @description PDF文件生成*/private  void convertToPDF(PdfWriter writer,Document document,String htmlString){//获取字体路径document.open();try {XMLWorkerHelper.getInstance().parseXHtml(writer,document,new ByteArrayInputStream(htmlString.getBytes()),XMLWorkerHelper.class.getResourceAsStream("/default.css"),Charset.forName("UTF-8"),new AsianFontProvider());} catch (IOException e) {e.printStackTrace();}finally {document.close();}}}

FreemarkerUtil.java

    public static String getContent(String templatePath,Object data){String templateFileName=getTemplateName(templatePath);String templateFilePath=getTemplatePath(templatePath);if(StringUtils.isEmpty(templatePath)){throw new RuntimeException("templatePath can not be empty!");}try{Configuration config = new Configuration(Configuration.VERSION_2_3_25);config.setDefaultEncoding("UTF-8");config.setDirectoryForTemplateLoading(new File(templateFilePath));config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);config.setLogTemplateExceptions(false);Template template = config.getTemplate(templateFileName);StringWriter writer = new StringWriter();template.process(data, writer);writer.flush();String html = writer.toString();return html;}catch (Exception ex){throw new RuntimeException("FreeMarkerUtil process fail",ex);}}

DownloadServlet.java

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PDFKit kit=new PDFKit();String fileName = "广西XXXX系统健康状况.pdf";response.setContentType("application/pdf");try {response.setHeader("Content-Disposition", "attachment;filename="+ new String(fileName.getBytes("gb2312"), "ISO8859-1"));} catch (UnsupportedEncodingException e) {// TODO Auto-generated catch blocke.printStackTrace();}Map<String,String> a = new HashMap<String,String>();a.put("name", "中文字符");kit.exportToResponse(a,response);}
参考以下资料
http://blog.csdn.net/zxz547388910/article/details/74315277
ITEXT5.5.8转html为pdf文档解决linux不显示中文问题

https://segmentfault.com/a/1190000009160184
java根据模板动态生成PDF

http://blog.csdn.net/jys1109/article/details/10066747
使用itext将html生成pdf中文换行问题解决方案2

这篇关于iText Freemarker模板生成导出PDF及部署到Linux的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1080407

相关文章

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Linux中的计划任务(crontab)使用方式

《Linux中的计划任务(crontab)使用方式》:本文主要介绍Linux中的计划任务(crontab)使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、前言1、linux的起源与发展2、什么是计划任务(crontab)二、crontab基础1、cro

Python将博客内容html导出为Markdown格式

《Python将博客内容html导出为Markdown格式》Python将博客内容html导出为Markdown格式,通过博客url地址抓取文章,分析并提取出文章标题和内容,将内容构建成html,再转... 目录一、为什么要搞?二、准备如何搞?三、说搞咱就搞!抓取文章提取内容构建html转存markdown

vue使用docxtemplater导出word

《vue使用docxtemplater导出word》docxtemplater是一种邮件合并工具,以编程方式使用并处理条件、循环,并且可以扩展以插入任何内容,下面我们来看看如何使用docxtempl... 目录docxtemplatervue使用docxtemplater导出word安装常用语法 封装导出方

Linux换行符的使用方法详解

《Linux换行符的使用方法详解》本文介绍了Linux中常用的换行符LF及其在文件中的表示,展示了如何使用sed命令替换换行符,并列举了与换行符处理相关的Linux命令,通过代码讲解的非常详细,需要的... 目录简介检测文件中的换行符使用 cat -A 查看换行符使用 od -c 检查字符换行符格式转换将

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子