|
@@ -1,12 +1,12 @@
|
|
|
package org.springblade.common.utils.pdf;
|
|
|
|
|
|
+import cn.hutool.core.io.IoUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.lowagie.text.pdf.BaseFont;
|
|
|
import org.apache.commons.collections4.CollectionUtils;
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springblade.common.utils.pdf.factory.Base64ImgReplacedElementFactory;
|
|
|
-import org.springblade.core.tool.utils.Func;
|
|
|
import org.springblade.core.tool.utils.SpringUtil;
|
|
|
import org.springframework.core.env.Environment;
|
|
|
import org.springframework.core.io.Resource;
|
|
@@ -19,9 +19,8 @@ import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
|
|
|
import org.xhtmlrenderer.pdf.ITextFontResolver;
|
|
|
import org.xhtmlrenderer.pdf.ITextRenderer;
|
|
|
|
|
|
-import java.io.BufferedOutputStream;
|
|
|
+import java.io.ByteArrayOutputStream;
|
|
|
import java.io.File;
|
|
|
-import java.io.FileOutputStream;
|
|
|
import java.io.InputStream;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.HashSet;
|
|
@@ -32,22 +31,20 @@ import java.util.Optional;
|
|
|
* 使用Thymeleaf模板生成pdf
|
|
|
*/
|
|
|
public class ThymeleafPdfUtil {
|
|
|
- //存放字体路径
|
|
|
- private static HashSet<String> fontPaths = new HashSet<>();
|
|
|
-
|
|
|
- //模板前缀,模板存放路径
|
|
|
+ // 存放字体路径
|
|
|
+ private static final HashSet<String> fontPaths = new HashSet<>();
|
|
|
+ // 模板前缀,模板存放路径
|
|
|
private final static String templatePrefix = "templates/";
|
|
|
- //模板后缀
|
|
|
+ // 模板后缀
|
|
|
private final static String templateSuffix = ".html";
|
|
|
- //模板类型
|
|
|
+ // 模板类型
|
|
|
private final static String templateMode = "HTML";
|
|
|
- //模板编码
|
|
|
+ // 模板编码
|
|
|
private final static String templateEncoding = "UTF-8";
|
|
|
+ // 读取运行环境
|
|
|
+ private static final Environment environment = SpringUtil.getBean(Environment.class);
|
|
|
|
|
|
- //读取运行环境
|
|
|
- private static Environment environment = SpringUtil.getBean(Environment.class);
|
|
|
-
|
|
|
- private AbstractConfigurableTemplateResolver templateResolver;
|
|
|
+ private final AbstractConfigurableTemplateResolver templateResolver;
|
|
|
private TemplateEngine templateEngine;
|
|
|
|
|
|
private ThymeleafPdfUtil() {
|
|
@@ -78,49 +75,46 @@ public class ThymeleafPdfUtil {
|
|
|
/**
|
|
|
* 根据模板生成一个PDF
|
|
|
*
|
|
|
- * @param ouputPDF 输出pdf路径
|
|
|
* @param templateName 模板名称
|
|
|
* @param variables 页面参数
|
|
|
* @throws Exception
|
|
|
*/
|
|
|
- public static void generate(File ouputPDF, String templateName, Map variables) throws Exception {
|
|
|
- //为空初始化
|
|
|
+ public static byte[] generateByte(String templateName, Map variables) throws Exception {
|
|
|
+ // 为空初始化
|
|
|
variables = Optional.ofNullable(variables).orElse(new HashMap());
|
|
|
|
|
|
final Context ctx = new Context();
|
|
|
ctx.setVariables(variables);
|
|
|
|
|
|
final TemplateEngine templateEngine = new ThymeleafPdfUtil().getTemplateEngine();
|
|
|
- String htmlContent = templateEngine.process(templateName, ctx);
|
|
|
-
|
|
|
-// System.err.println("htmlContent-1-:" + htmlContent);
|
|
|
|
|
|
- //替换转义
|
|
|
- htmlContent = htmlContent.replaceAll("<", "<").replaceAll(">", ">");
|
|
|
- htmlContent = htmlContent.replaceAll(""", "'");
|
|
|
-
|
|
|
-// System.err.println("htmlContent-2-:" + htmlContent);
|
|
|
+ // 替换转义
|
|
|
+ String htmlContent = templateEngine.process(templateName, ctx).replaceAll("<", "<").replaceAll(">", ">").replaceAll(""", "'");
|
|
|
|
|
|
ITextRenderer renderer = new ITextRenderer();
|
|
|
ITextFontResolver fontResolver = renderer.getFontResolver();
|
|
|
try {
|
|
|
- //获取字体路径
|
|
|
+ // 获取字体路径
|
|
|
getFontPaths();
|
|
|
if (CollectionUtils.isNotEmpty(fontPaths)) {
|
|
|
for (String path : fontPaths) {
|
|
|
- //避免中文为空设置系统字体
|
|
|
+ // 避免中文为空设置系统字体
|
|
|
fontResolver.addFont(path, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
- //解决pdf对图片路径为base64不显示问题
|
|
|
+ // 解决pdf对图片路径为base64不显示问题
|
|
|
renderer.getSharedContext().setReplacedElementFactory(new Base64ImgReplacedElementFactory());
|
|
|
-
|
|
|
renderer.setDocumentFromString(htmlContent);
|
|
|
renderer.layout();
|
|
|
- renderer.createPDF(new BufferedOutputStream(new FileOutputStream(ouputPDF)));
|
|
|
+
|
|
|
+ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
|
|
+ renderer.createPDF(byteArrayOutputStream);
|
|
|
+ byte[] bytes = byteArrayOutputStream.toByteArray();
|
|
|
+ IoUtil.close(byteArrayOutputStream);
|
|
|
+ return bytes;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -177,12 +171,12 @@ public class ThymeleafPdfUtil {
|
|
|
try {
|
|
|
System.err.println(JSONObject.toJSONString(fontPaths));
|
|
|
if (CollectionUtils.isEmpty(fontPaths)) {
|
|
|
- //读取配置
|
|
|
- String fontpath = environment.getProperty("blade.pdf.fontpath");
|
|
|
- if (StringUtils.isBlank(fontpath)) {
|
|
|
+ // 读取配置
|
|
|
+ String fontPath = environment.getProperty("blade.pdf.fontpath");
|
|
|
+ if (StringUtils.isBlank(fontPath)) {
|
|
|
return;
|
|
|
}
|
|
|
- getFiles(fontpath);
|
|
|
+ getFiles(fontPath);
|
|
|
System.err.println("加载到字体路径==" + JSONObject.toJSONString(fontPaths));
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -198,15 +192,15 @@ public class ThymeleafPdfUtil {
|
|
|
public static void getFiles(String path) {
|
|
|
File file = new File(path);
|
|
|
File[] tempList = file.listFiles();
|
|
|
- if (tempList == null || tempList.length <= 0) {
|
|
|
+ if (tempList == null) {
|
|
|
return;
|
|
|
}
|
|
|
- for (int i = 0; i < tempList.length; i++) {
|
|
|
- if (tempList[i].isFile()) {
|
|
|
- fontPaths.add(tempList[i].toString());
|
|
|
+ for (File value : tempList) {
|
|
|
+ if (value.isFile()) {
|
|
|
+ fontPaths.add(value.toString());
|
|
|
}
|
|
|
- if (tempList[i].isDirectory()) {
|
|
|
- //这里就不递归了,
|
|
|
+ if (value.isDirectory()) {
|
|
|
+ // 这里就不递归了,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -226,13 +220,7 @@ public class ThymeleafPdfUtil {
|
|
|
} catch (Exception e) {
|
|
|
return false;
|
|
|
} finally {
|
|
|
- if (inputStream != null) {
|
|
|
- try {
|
|
|
- inputStream.close();
|
|
|
- } catch (Exception e) {
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
+ IoUtil.close(inputStream);
|
|
|
}
|
|
|
}
|
|
|
|