9.1.16
com.itextpdf
itextpdf
5.5.13
资源文件处理工具类
import org.springframework.util.ResourceUtils;
import java.io.File;
import java.io.FileNotFoundException;
/**
-
@Description: 项目静态资源文件工具类
-
仅可用于包含在web项目中的资源文件路径,资源文件必须放置于 web 模块下
-
@Author: junqiang.lu
-
@Date: 2019/1/4
*/
public class ResourceFileUtil {
/**
-
获取资源文件
-
@param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
-
eg: "templates/pdf_export_demo.ftl"
-
@return
-
@throws FileNotFoundException
*/
public static File getFile(String relativePath) throws FileNotFoundException {
if (relativePath == null || relativePath.length() == 0) {
return null;
}
if (relativePath.startsWith(“/”)) {
relativePath = relativePath.substring(1);
}
File file = ResourceUtils.getFile(ResourceUtils.CLASSPATH_URL_PREFIX
- relativePath);
return file;
}
/**
-
获取资源绝对路径
-
@param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
-
eg: "templates/pdf_export_demo.ftl"
-
@return
-
@throws FileNotFoundException
*/
public static String getAbsolutePath(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getAbsolutePath();
}
/**
-
获取资源父级目录
-
@param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
-
eg: "templates/pdf_export_demo.ftl"
-
@return
-
@throws FileNotFoundException
*/
public static String getParent(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getParent();
}
/**
-
获取资源文件名
-
@param relativePath 资源文件相对路径(相对于 resources路径,路径 + 文件名)
-
eg: "templates/pdf_export_demo.ftl"
-
@return
-
@throws FileNotFoundException
*/
public static String getFileName(String relativePath) throws FileNotFoundException {
return getFile(relativePath).getName();
}
}
PDF 生成工具类
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.BaseFont;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import org.xhtmlrenderer.pdf.ITextFontResolver;
import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.*;
import java.util.Map;
/**
-
@Description: pdf 导出工具类
-
@Author: junqiang.lu
-
@Date: 2018/12/25
*/
public class PDFUtil {
private PDFUtil(){}
private volatile static Configuration configuration;
static {
if (configuration == null) {
synchronized (PDFUtil.class) {
if (configuration == null) {
configuration = new Configuration(Configuration.VERSION_2_3_28);
}
}
}
}
/**
-
freemarker 引擎渲染 html
-
@param dataMap 传入 html 模板的 Map 数据
-
@param ftlFilePath html 模板文件相对路径(相对于 resources路径,路径 + 文件名)
-
eg: "templates/pdf_export_demo.ftl"
-
@return
*/
public static String freemarkerRender(Map<String, Object> dataMap, String ftlFilePath) {
Writer out = new StringWriter();
configuration.setDefaultEncoding(“UTF-8”);
configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
try {
configuration.setDirectoryForTemplateLoading(new File(ResourceFileUtil.getParent(ftlFilePath)));
configuration.setLogTemplateExceptions(false);
configuration.setWrapUncheckedExceptions(true);
Template template = configuration.getTemplate(ResourceFileUtil.getFileName(ftlFilePath));
template.process(dataMap, out);
out.flush();
return out.toString();
} catch (IOException e) {
e.printStackTrace();
} catch (TemplateException e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
-
使用 iText 生成 PDF 文档
-
@param htmlTmpStr html 模板文件字符串
-
@param fontFile 所需字体文件(相对路径+文件名)
-
*/
public static byte[] createPDF(String htmlTmpStr, String fontFile) {
ByteArrayOutputStream outputStream = null;
byte[] result = null;
try {
outputStream = new ByteArrayOutputStream();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(htmlTmpStr);
ITextFontResolver fontResolver = renderer.getFontResolver();
// 解决中文支持问题,需要所需字体(ttc)文件
fontResolver.addFont(ResourceFileUtil.getAbsolutePath(fontFile),BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
renderer.layout();
renderer.createPDF(outputStream);
result=outputStream.toByteArray();
if(outputStream != null) {
outputStream.flush();
outputStream.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
}
PDF 导出所需资源文件
PDF 导出需要 freemarker 的渲染模板 ftl 文件
关于 ftl 模板的一些信息: ftl 模板使用和 html 一致的标签,和 html 不同的是 ftl 模板文件可以添加独有的逻辑元算标签, 如 if-else, for 等,逻辑运算标签具体可参考 freemarker 官方文档:
https://freemarker.apache.org/docs/ref_directives.html
这是我的模板:pz.ftl