itext生成PDF中文为空
来源:互联网 发布:h5场景制作软件 编辑:程序博客网 时间:2024/05/17 16:15
PDF使用Itext,HTML转换为PDF,字体设置由原文决定。
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
html.append("<head>");
html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
html.append("<style type=\"text/css\" media=\"print\">");
html.append("body{");
html.append("font-family: SimSun, Arial Unicode MS,Msyh;margin:50px auto;");
html.append("font-size: 14px;");
public XMLWorkerFontProvider(String fontsPath, HashMap<String, String> fontSubstitutionMap) {
if (fontsPath == null || fontsPath.length() == 0) {super.registerDirectories();
} else if (!fontsPath.equals(DONTLOOKFORFONTS)) {
super.registerDirectory(fontsPath, true);
}
if (fontSubstitutionMap != null) {
this.fontSubstitutionMap = fontSubstitutionMap;
}
}
registerDirectories 加载系统内所有的字体。
public int registerDirectories() {
int count = 0;
String windir = System.getenv("windir");
String fileseparator = System.getProperty("file.separator");
if (windir != null && fileseparator != null) {
count += registerDirectory(windir + fileseparator + "fonts");
}
count += registerDirectory("/usr/share/X11/fonts", true);
count += registerDirectory("/usr/X/lib/X11/fonts", true);
count += registerDirectory("/usr/openwin/lib/X11/fonts", true);
count += registerDirectory("/usr/share/fonts", true);
count += registerDirectory("/usr/X11R6/lib/X11/fonts", true);
count += registerDirectory("/Library/Fonts");
count += registerDirectory("/System/Library/Fonts");
return count;
}
而一般windows下字体是全的,而Linux下的字体如果不全,PDF中中文显示为空。
解决方法:
加载字体jar,itext-asian.jar,但是这个jar中仍然没有SimSun字体,需要重新创建字体。
private class IWXMLWorkerFontProvider extends XMLWorkerFontProvider {
public IWXMLWorkerFontProvider() {
super(null, null);
}
@Override
public Font getFont(final String fontname, String encoding, float size, final int style) {
String fntname = fontname;
try {
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bfChinese, size, style, BaseColor.BLACK);
return font;
} catch (Exception e) {
e.printStackTrace();
}
return null;
return super.getFont(fntname, encoding, size, style);
}
}
public static BaseFont createFont(String name, String encoding, boolean embedded, boolean cached, byte ttfAfm[], byte pfb[], boolean noThrow, boolean forceRead) throws DocumentException, IOException {
String nameBase = getBaseName(name);
encoding = normalizeEncoding(encoding);
boolean isBuiltinFonts14 = BuiltinFonts14.containsKey(name);
boolean isCJKFont = isBuiltinFonts14 ? false : CJKFont.isCJKFont(nameBase, encoding);
........
itext内置14种字体,CJKFont会加载cjk_registry.properties,并从该文件中加载字体。
如果在HTML中设置字体encoding总是CP1252
ChunkCssApplier
public Font applyFontStyles(final Tag t) {
String fontName = null;
String encoding = BaseFont.CP1252;
float size = new FontSizeTranslator().getFontSize(t);
int style = Font.UNDEFINED;
BaseColor color = null;
Map<String, String> rules = t.getCSS();
for (Entry<String, String> entry : rules.entrySet()) {
String key = entry.getKey();
所以总是不能解析成功
- itext生成PDF中文为空
- java使用iText生成pdf:解决中文
- itext 导出pdf 表格生成、目录生成、支持中文
- 生成pdf,使用itext
- iText生成PDF
- itext 生成pdf
- itext 生成pdf
- 使用itext生成pdf
- iText生成PDF
- IText 生成PDF
- itext生成pdf
- iText生成PDF入门
- iText生成PDF入门
- iText生成pdf文书
- iText 生成pdf分页
- iText生成PDF学习
- itext 生成pdf 代码
- itext 生成pdf文档
- Ubuntu16.04 执行sudo apt-get update出现E: Sub-process returned an error code错误
- 树莓派开启ssh远程连接服务
- spring 注解aop切入点表达式怎么排除某些方法
- ExpandableListView以及GridView属性
- spring基础及IOC入门
- itext生成PDF中文为空
- 探索式测试:基本概念
- SLAM学习笔记(四)定位
- highchat钻取前后台交互
- ajax在开发测试时候跨域访问问题。
- 抽象类和接口的区别
- 出售百度贴吧账号的高回头率暴利项目
- Frameset 框架集
- Linux操作系统 第三次实验-Linux文件管理