在项目中使用pdfbox2.0.15版本,将pdf文件转成图片,在windows本地转换正常,发布到linux服务器,转换后图片中的中文出现部分乱码,显示都是方块□□□。
后台日志打印出的日志显示,pdf识别出的字体为STSongStd-Light,服务器没有这个字体库,所以按照默认匹配规则,匹配到了UnDotum-Bold这种字体,经查询,UnDotum-Bold主要为韩语字体,无法匹配所有中文字体,所以出现方块□□□。
2019-12-25 14:50:12.308 [http-nio-9281-exec-9] WARN o.a.p.pdmodel.font.PDCIDFontType0 -Using fallback UnDotum-Bold for CID-keyed font STSongStd-Light
2019-12-25 14:50:12.309 [http-nio-9281-exec-9] WARN o.a.pdfbox.rendering.CIDType0Glyph2D -No glyph for 27748 (CID 0e13) in font STSongStd-Light
根据网上资料显示,因服务器缺少字体库STSongStd-Light,既然是这样,自然是找到对应的字体库放到服务器上(linux添加字体库教程自行google)。经过一番折腾,字体是有了,但是依旧显示乱码,日志还是显示加载的是UnDotum-Bold,此方法不通只能走其他路子了。
通过断点调试,在FontMapperImpl类中发现有个substitutes 列表,存放的是字体名称的映射关系,即一种字体不存在时,可以根据这个映射关系,优先匹配对应的字体,部分源码如下:
final class FontMapperImpl implements FontMapper
{
private static final FontCache fontCache = new FontCache(); // todo: static cache isn't ideal
private FontProvider fontProvider;
private Map<String, FontInfo> fontInfoByName;
private final TrueTypeFont lastResortFont;
/** Map of PostScript name substitutes, in priority order. */
private final Map<String, List<String>> substitutes = new HashMap<String, List<String>>();
FontMapperImpl()
{
// substitutes for standard 14 fonts
substitutes.put("Courier",
Arrays.asList("CourierNew", "CourierNewPSMT", "LiberationMono", "NimbusMonL-Regu"));
substitutes.put("Courier-Bold",
Arrays.asList("CourierNewPS-BoldMT", "CourierNew-Bold", "LiberationMono-Bold",
"NimbusMonL-Bold"));
substitutes.put("Courier-Oblique",
Arrays.asList("CourierNewPS-ItalicMT","CourierNew-Italic",
"LiberationMono-Italic", "NimbusMonL-ReguObli"));
substitutes.put("Courier-BoldOblique",
Arrays.asList("CourierNewPS-BoldItalicMT","CourierNew-BoldItalic",
"LiberationMono-BoldItalic", "NimbusMonL-BoldObli"));
substitutes.put("Helvetica",
Arrays.asList("ArialMT", "Arial", "LiberationSans", "NimbusSanL-Regu"));
substitutes.put("Helvetica-Bold",
Arrays.asList("Arial-BoldMT", "Arial-Bold", "LiberationSans-Bold",
"NimbusSanL-Bold"));
substitutes.put("Helvetica-Oblique",
Arrays.asList("Arial-ItalicMT", "Arial-Italic", "Helvetica-Italic",
"LiberationSans-Italic", "NimbusSanL-ReguItal"));
substitutes.put("Helvetica-BoldOblique",
Arrays.asList("Arial-BoldItalicMT", "Helvetica-BoldItalic",
"LiberationSans-BoldItalic", "NimbusSanL-BoldItal"));
substitutes.put("Times-Roman",
Arrays.asList("TimesNewRomanPSMT", "TimesNewRoman", "TimesNewRomanPS",
"LiberationSerif", "NimbusRomNo9L-Regu"));
substitutes.put("Times-Bold",
Arrays.asList("TimesNewRomanPS-BoldMT", "TimesNewRomanPS-Bold",
"TimesNewRoman-Bold", "LiberationSerif-Bold",
"NimbusRomNo9L-Medi"));
substitutes.put("Times-Italic",
Arrays.asList("TimesNewRomanPS-ItalicMT", "TimesNewRomanPS-Italic",
"TimesNewRoman-Italic", "LiberationSerif-Italic",
"NimbusRomNo9L-ReguItal"));
substitutes.put("Times-BoldItalic",
Arrays.asList("TimesNewRomanPS-BoldItalicMT", "TimesNewRomanPS-BoldItalic",
"TimesNewRoman-BoldItalic", "LiberationSerif-BoldItalic",
"NimbusRomNo9L-MediItal"));
substitutes.put("Symbol", Arrays.asList("Symbol", "SymbolMT", "StandardSymL"));
substitutes.put("ZapfDingbats", Arrays.asList("ZapfDingbatsITC", "Dingbats", "MS-Gothic"));
因该类没有提供相应的接口设置这个substitutes列表,所以只能通过修改源码来实现。
在自己项目中建一个package,路径和FontMapperImpl类所在路径完全一致,并在该包下新建FontMapperImpl类,并将原来的FontMapperImpl类中的代码完全复制过来,在代码中添加一行代码,key为pdf中识别出的字体,value为识别不到时优先匹配的字体,可以匹配多个,排前面则优先匹配:
substitutes.put("STSongStd-Light", Arrays.asList("ArialUnicodeMS","DengXian"));
代码添加后,启动项目,程序会优先加载这个类,经测试,转换后的图片中文显示正常了,问题解决。