Commit 9975dcd1 by jianan

新单跟进73

parent 506522ed
......@@ -12,11 +12,13 @@ import com.lowagie.text.pdf.PdfWriter;
import com.yd.common.exception.BusinessException;
import com.yd.csf.service.dto.PolicyReportData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Service;
import java.awt.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
@Service
......@@ -356,27 +358,16 @@ public class PolicyReportPdfService {
try {
BaseFont baseFont = null;
// 方法1: 使用绝对路径
String absolutePath = "D:\\IdeaProjects\\yd-csf\\yd-csf-service\\src\\main\\resources\\font\\DFKai-SB.ttf";
// 方法2: 使用classpath相对路径(如果字体文件在resources目录下)
String resourcePath = "font/DFKai-SB.ttf";
java.io.File fontFile = new java.io.File(absolutePath);
if (fontFile.exists()) {
try {
baseFont = BaseFont.createFont(absolutePath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
log.info("成功从绝对路径加载繁体楷体: {}", absolutePath);
} catch (Exception e) {
log.warn("绝对路径加载失败,尝试classpath路径");
// 尝试从classpath加载
try {
baseFont = BaseFont.createFont(resourcePath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
log.info("成功从classpath加载繁体楷体: {}", resourcePath);
} catch (IOException ioException) {
log.error("classpath路径加载失败,使用备用字体", ioException);
}
// 优先使用Classpath资源流加载
try {
InputStream fontStream = getClass().getClassLoader().getResourceAsStream("font/DFKai-SB.ttf");
if (fontStream != null) {
byte[] fontData = IOUtils.toByteArray(fontStream);
baseFont = BaseFont.createFont("DFKai-SB.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, BaseFont.CACHED, fontData, null);
log.info("成功从字节数组加载繁体楷体");
}
} catch (Exception e) {
log.warn("Classpath加载失败: {}", e.getMessage());
}
// 如果还是失败,使用备用字体
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment