使用itext和Flying Saucer将html转换为pdf时不会嵌入字体

时间:2013-11-05 12:00:43

标签: java flying-saucer

嗨,这是我的代码从html源生成pdf,html得到了完美但有些是pdf错过了那个字体和文本

try{
        StringBuffer buf = new StringBuffer("");
        buf.append("<html><head><style>@font-face {font-family:gautami;src:url(C:\\Documents and Settings\\Administrator\\Desktop\\font\\gautami.ttf);-fs-pdf-font-embed: embed;-fs-pdf-font-encoding: Identity-H;} .col{color:red;}</style></head><body style='font-family:gautami'>");
        //List<File> fil = fileGallaryDAO.getNews();
       // for (File movie : fil) {
            // create the snippet


            buf.append("<div class='col'> &#3120;&#3134;&#3127;&#3149;&#3103;&#3149;&#3120; &#3125;&#3135;&#3117;&#3100;&#3112; &#3093;&#3147;&#3128;&#3074; &#3120;&#3134;&#3100;&#3149;&#3119;&#3134;&#3074;&#3095; &#3128;&#3125;&#3120;&#3107;</div>");

            // use the snippet for the HTML page
       // }

        buf.append("<div class='col'>fgfdhd</div></body></html>");
        ITextRenderer renderer = new ITextRenderer();
        renderer.getFontResolver().addFont (
            "C:\\Documents and Settings\\Administrator\\Desktop\\font\\gautami.ttf",
             "UTF-8",
             BaseFont.EMBEDDED
        );
        PrintStream out = new PrintStream(new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\htmlpdf\\new.html"));
         out.println(buf.toString());
         out.flush();
         out.close();
        //renderer.setDocument(buf.toString());
        renderer.setDocument(new java.io.File("C:\\Documents and Settings\\Administrator\\Desktop\\htmlpdf\\new.html"));
        String outputFile = "C:\\Documents and Settings\\Administrator\\Desktop\\htmlpdf\\newpdf.pdf";
        OutputStream os = new FileOutputStream(outputFile);

        renderer.layout();
        renderer.createPDF(os);
        os.flush();
        os.close();
    }catch(Exception e){
        e.printStackTrace();
    }
    }

html文件输出:

రాష్ట్ర విభజన కోసం రాజ్యాంగ సవరణ
fgfdhd

pdf文件输出:

fgfdhd

甚至字体都没有嵌入pdf中,错过了文字?

0 个答案:

没有答案