java利用iTextWorker生成pdf

时间:2021-07-28 06:10:19

使用itext生成pdf,在linux环境下,中文全部失踪,因为itext要在linux下支持中文字体需要引入itext-asian, 并添加一个字体类。

 public static class PdfFont extends XMLWorkerFontProvider{
public PdfFont(){
super(null,null);
}
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) { Font FontChinese = null;
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
FontChinese = new Font(bfChinese, 12, Font.NORMAL);
} catch (Exception e) {
e.printStackTrace();
}
if(FontChinese==null)
FontChinese = super.getFont(fontname, encoding, size, style);
return FontChinese;
}}