日期:2014-05-17  浏览次数:20610 次

PDF中文处理个问题?
最近项目中需要将jsp界面转成PDF,现在最苦恼的是 转成的PDF 中文显示不了,或者是一堆 ???号,还有就是文本框中的值无法展示在PDF文件中,有过这方面开发经验的前辈们,欢迎指点,小弟先谢过了!
在网上找到的处理老是报错:如下:
Java code
plumbing.render INFO:: Using CSS implementation from: org.xhtmlrenderer.context.StyleReference
plumbing.exception WARNING:: Unhandled exception. tables
org.xhtmlrenderer.util.XRRuntimeException: tables
    at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:96)
    at test.byd.com.dcc.unittest.TestPdf.main(TestPdf.java:114)
Caused by: java.lang.NoSuchFieldException: tables
    at java.lang.Class.getDeclaredField(Unknown Source)
    at org.xhtmlrenderer.pdf.TrueTypeUtil.extractTables(TrueTypeUtil.java:66)
    at org.xhtmlrenderer.pdf.TrueTypeUtil.populateDescription(TrueTypeUtil.java:77)
    at org.xhtmlrenderer.pdf.ITextFontResolver.addFont(ITextFontResolver.java:94)
    ... 1 more
plumbing.load INFO:: TIME: parse stylesheets  140ms
plumbing.match INFO:: media = print
plumbing.match INFO:: Matcher created with 119 selectors
plumbing.exception WARNING:: IO problem for file:/public/global/images/byd_logo.png


------解决方案--------------------
依稀记得iText处理亚洲字体需要引用一个jar
名字忘了,大概叫iTextAsian什么的
------解决方案--------------------
我记得引入中文包之后,还需要在程序中装入中文的时候设置字符集的,,
------解决方案--------------------
Java code

private static Cell newCell(String str,int align,Color color){
        Cell cell=null;
        try {
            cell = new Cell(new Paragraph(str,setChineseFont()));
            cell.setHorizontalAlignment(align);
            cell.setUseAscender(false); 
            cell.setVerticalAlignment(Element.ALIGN_CENTER);//上下居中
            cell.setBackgroundColor(color);
            //cell.setBorderWidth(1);
        } catch (BadElementException e) {
            e.printStackTrace();
        }
        return cell;
    }
    /** 
    * 设置中文 
    * @author maHai
    * @return Font 
    */ 
    private static Font setChineseFont() { 
    BaseFont base = null; 
    Font fontChinese = null; 
    try { 
    base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED); 
    fontChinese = new Font(base, 12, Font.NORMAL); 
    } catch (DocumentException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    return fontChinese; 
    }

------解决方案--------------------
以前做练习的时候和LZ遇到过同样的问题,
后来就没有继续下去了!!