Commit 75ae2ca5 authored by 徐俊's avatar 徐俊

xujun

parent 1c17fb46
...@@ -77,6 +77,7 @@ public class ProjectInfoToPDF { ...@@ -77,6 +77,7 @@ public class ProjectInfoToPDF {
/** /**
* 加载中文字体 * 加载中文字体
* @param fontPath 字体文件路径(相对于resources目录)
* @return BaseFont 中文字体 * @return BaseFont 中文字体
* @throws RuntimeException 加载字体失败时抛出异常 * @throws RuntimeException 加载字体失败时抛出异常
*/ */
...@@ -85,11 +86,16 @@ public class ProjectInfoToPDF { ...@@ -85,11 +86,16 @@ public class ProjectInfoToPDF {
// 从资源文件夹加载字体 // 从资源文件夹加载字体
InputStream fontStream = ProjectInfoToPDF.class.getResourceAsStream(fontPath); InputStream fontStream = ProjectInfoToPDF.class.getResourceAsStream(fontPath);
if (fontStream == null) { if (fontStream == null) {
throw new FileNotFoundException("在resources/fonts目录下未找到simsun.ttc字体文件"); throw new FileNotFoundException("找不到字体文件:" + fontPath);
} }
// 获取文件名作为临时文件的前缀
String fileName = fontPath.substring(fontPath.lastIndexOf('/') + 1);
String prefix = fileName.substring(0, fileName.lastIndexOf('.'));
String suffix = fileName.substring(fileName.lastIndexOf('.'));
// 将字体文件复制到临时文件 // 将字体文件复制到临时文件
File tempFile = File.createTempFile("simsun", ".ttc"); File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit(); tempFile.deleteOnExit();
try (FileOutputStream fos = new FileOutputStream(tempFile)) { try (FileOutputStream fos = new FileOutputStream(tempFile)) {
...@@ -102,7 +108,7 @@ public class ProjectInfoToPDF { ...@@ -102,7 +108,7 @@ public class ProjectInfoToPDF {
return BaseFont.createFont(tempFile.getAbsolutePath() + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); return BaseFont.createFont(tempFile.getAbsolutePath() + ",0", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("加载字体文件失败,请确保resources/fonts目录下存在simsun.ttc文件:" + e.getMessage(), e); throw new RuntimeException("加载字体文件失败,请确保resources目录下存在字体文件 " + fontPath + ":" + e.getMessage(), e);
} }
} }
......
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