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();


所以总是不能解析成功



原创粉丝点击