【问题标题】:Flying Saucer adding embedded font not working on tomcat飞碟添加嵌入式字体在tomcat上不起作用
【发布时间】:2017-01-02 12:36:50
【问题描述】:

我正在使用 Flying Saucer 将 XHTML 转换为 PDF 文件。我的问题是我在 PDF 中缺少一些土耳其语字符。但是我不明白的是,我在junit测试中没有任何问题。我可以毫无问题地将 XHTML 文件转换为包含所有土耳其语字符的 PDF。但是,如果我对部署在 Tomcat 上的 Applicaton 执行相同操作,则会丢失所有土耳其语字符。

我做了一些调试,发现在这两种情况下都找到了“/fonts/arialuni.ttf”。我找到了一些相关的线程和 itext doc 并尝试了所有这些。

http://developers.itextpdf.com/question/how-can-i-load-font-web-infresourcesfontsfoobarttf

Flying Saucer font for unicode characters

转换和添加字体的代码

            String path2 = "/fonts/arialuni.ttf";

           //FontFactory.register(path2);
            ITextRenderer renderer = new ITextRenderer();
          // renderer.getFontResolver().addFontDirectory(path2,true);

            renderer.getFontResolver().addFont(path2, BaseFont.IDENTITY_H,BaseFont.EMBEDDED);


            renderer.setDocumentFromString(docElem.html());
            renderer.layout();
            renderer.createPDF(os);
            renderer.finishPDF();
            baoOut.flush();

并且还为 CSS 应用了以下内容。但结果还是一样。

@font-face {
    src: url('arialuni.ttf'); <!--src: url('/fonts/arialuni.ttf'); -->
    -fs-pdf-font-embed: embed;
    -fs-pdf-font-encoding: Identity-H;
}

body {
    font-family: Arial Unicode MS, Lucida Sans Unicode, Arial, verdana, arial, helvetica, sans-serif;
    font-size: 8.8pt;
}

有谁知道,为什么 arialuni.tff 被发现但没有在 Tomcat 上使用?

【问题讨论】:

  • 请将您的解决方案(EDIT 后面的所有内容)添加为单独的答案并接受它 - 否则人们认为它没有答案...

标签: java fonts itext flying-saucer


【解决方案1】:

终于解决了。在我使用的某些地方

InputStream htmlIS = IOUtils.toInputStream(invoiceHtml);

改为

InputStream htmlIS = IOUtils.toInputStream(invoiceHtml, "UTF-8");

导致问题。

【讨论】:

    猜你喜欢
    • 2018-03-22
    • 2013-05-24
    • 2012-10-01
    • 1970-01-01
    • 2018-11-18
    • 2011-03-20
    • 2011-11-23
    • 2023-04-07
    • 2015-01-19
    相关资源
    最近更新 更多