feat: 将pdf使用的字体外挂,减少打包的jar体积

This commit is contained in:
曹鹏飞 2025-09-25 17:55:33 +08:00
parent f1345a3249
commit 05b9005120
4 changed files with 52 additions and 3 deletions

View File

@ -0,0 +1,22 @@
package com.nflg.wms.admin.util;
import java.io.File;
public class PathUtils {
public static String getPath() {
String classPath = PathUtils.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println("classPath:" + classPath);
// 如果是jar包运行获取jar包所在目录
if (classPath.contains(".jar")) {
File jarFile = new File(classPath);
return jarFile.getParent();
}
// 如果是开发环境获取target/classes目录的父目录
File classDir = new File(classPath);
if (classDir.getName().equals("classes")) {
return classDir.getParentFile().getParentFile().getAbsolutePath();
}
return new File(classPath).getAbsolutePath();
}
}

View File

@ -1,20 +1,33 @@
package com.nflg.wms.admin.util; package com.nflg.wms.admin.util;
import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.pdf.BaseFont;
import com.nflg.wms.common.util.VUtil;
import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.FilenameUtils;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpHeaders; import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.xhtmlrenderer.pdf.ITextRenderer; import org.xhtmlrenderer.pdf.ITextRenderer;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
public class PdfGeneratorUtil { public class PdfGeneratorUtil {
private static final String fontPath = "fonts/simsun.ttc"; private static final Set<String> SUPPORTED_EXTENSIONS = new HashSet<>(Arrays.asList(
"ttf", "ttc", "otf", "pfb"
));
public static void generatePdf(String name,String html, HttpServletResponse response) throws Exception { public static void generatePdf(String name,String html, HttpServletResponse response) throws Exception {
URL baseUrl = new ClassPathResource("template/").getURL(); URL baseUrl = new ClassPathResource("template/").getURL();
@ -23,7 +36,7 @@ public class PdfGeneratorUtil {
public static void generatePdf(String name,String html,String baseUrl, HttpServletResponse response) throws Exception { public static void generatePdf(String name,String html,String baseUrl, HttpServletResponse response) throws Exception {
ITextRenderer renderer = new ITextRenderer(); ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); loadFonts(renderer);
renderer.setDocumentFromString(html,baseUrl); renderer.setDocumentFromString(html,baseUrl);
renderer.layout(); renderer.layout();
renderer.createPDF(response.getOutputStream()); renderer.createPDF(response.getOutputStream());
@ -39,9 +52,21 @@ public class PdfGeneratorUtil {
public static void generatePdf(String name,String html,String baseUrl,OutputStream output) throws Exception { public static void generatePdf(String name,String html,String baseUrl,OutputStream output) throws Exception {
ITextRenderer renderer = new ITextRenderer(); ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont(fontPath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); loadFonts(renderer);
renderer.setDocumentFromString(html,baseUrl); renderer.setDocumentFromString(html,baseUrl);
renderer.layout(); renderer.layout();
renderer.createPDF(output); renderer.createPDF(output);
} }
private static void loadFonts(ITextRenderer renderer) throws IOException {
Path fontsDir = Paths.get(PathUtils.getPath(), "fonts");
VUtil.trueThrowBusinessError(!Files.exists(fontsDir) || !Files.isDirectory(fontsDir))
.throwMessage("fonts文件夹不存在: " + fontsDir);
File directory = fontsDir.toFile();
File[] fonts = directory.listFiles((dir, name) -> SUPPORTED_EXTENSIONS.contains(FilenameUtils.getExtension(name)));
VUtil.trueThrowBusinessError(Objects.isNull(fonts) || fonts.length == 0).throwMessage("未找到有效字体");
for (File font : fonts) {
renderer.getFontResolver().addFont(font.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
}
}
} }

View File

@ -35,6 +35,8 @@ public class DeployTest {
sshUtil.connect("192.168.163.84", 22, "root", "CMP2025nf"); sshUtil.connect("192.168.163.84", 22, "root", "CMP2025nf");
//处理主jar包 //处理主jar包
handleFile(sshUtil, localPath + jarName, remotePath + jarName); handleFile(sshUtil, localPath + jarName, remotePath + jarName);
//处理字体目录
handleDir(sshUtil, localPath, remotePath, "fonts");
//处理lib目录 //处理lib目录
handleDir(sshUtil, localPath, remotePath, "lib"); handleDir(sshUtil, localPath, remotePath, "lib");
//执行脚本启动服务 //执行脚本启动服务