Flying Saucer iTextPDF Chinese Fonts

ぃ、小莉子 提交于 2019-12-10 17:23:37

问题


I'm using FlyingSaucer (R8pre2 version) to convert an XHTML into PDF.

The HTML has Chinese characters and are using MS Mincho fonts.

The document was converted, but all the Chinese characters are missing on the converted PDF.

Any idea how to resolve this problem?

Code:

OutputStream os = new FileOutputStream("output.pdf");
String inputFile = "input.html";
String url = new File(inputFile).toURI().toURL().toString();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(url);
renderer.layout();
renderer.createPDF(os);
os.close();

回答1:


Chinese font is required to be loaded before Chinese font is displayed. For example:

renderer.getFontResolver().addFont("fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
renderer.getFontResolver().addFont("fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


来源:https://stackoverflow.com/questions/16809464/flying-saucer-itextpdf-chinese-fonts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!