package com.dpkj.modules.print.utils; import com.itextpdf.text.pdf.BaseFont; import java.io.InputStream; /** * @author 余文财 * @description 字体加载 * @date 2025.6.24 */ public class FontLoader { public static BaseFont loadFont(String resourcePath) { try (InputStream fontStream = FontLoader.class.getResourceAsStream(resourcePath)) { if (fontStream == null) { throw new IllegalArgumentException("字体文件未找到: " + resourcePath); } // 创建支持中文的 BaseFont return BaseFont.createFont( resourcePath, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, false, fontStream.readAllBytes(), null ); } catch (Exception e) { throw new RuntimeException("字体加载失败: " + resourcePath, e); } } }