繁体   English   中英

使用ITextRenderer使用非拉丁字符从HTML生成PDF不起作用

[英]Generation of PDF from HTML with non-Latin characters using ITextRenderer does not work

这是我花在调查上的第二天没有结果。 至少现在,我能够提出非常具体的问题。

我正在尝试编写一个有效的HTML代码,其中包含使用iText的PDF文件中的一些非拉丁字符,更具体地说,使用Flying Saucer的 ITextRenderer

我的简短示例/代码首先使用以下值初始化字符串变量doc:

String doc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\">"
            + "<body>Some greek characters: Καλημέρα Some greek characters"
            + "</body></html>";

这是我用于调试目的的代码。 我将此字符串保存为HTML文件然后我通过浏览器打开它只是为了仔细检查HTML内容是否有效,我仍然可以读取希腊字符:

//write for debugging purposes in an html file
File newTextFile = new File("C:/work/test.html");
FileWriter fw = new FileWriter(newTextFile);
fw.write(doc);
fw.close();

下一步是尝试在PDF文件中写入此值。 这是我的代码:

ITextRenderer renderer = new ITextRenderer();
    //add some fonts - if paths are not right, an exception will be thrown
    renderer.getFontResolver().addFont("c:/work/fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    renderer.getFontResolver().addFont("c:/work/fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    renderer.getFontResolver().addFont("c:/work/fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    renderer.getFontResolver().addFont("c:/work/fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
            .newInstance();
    documentBuilderFactory.setValidating(false);
    DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
    builder.setEntityResolver(FSEntityResolver.instance());
    org.w3c.dom.Document document = builder.parse(new ByteArrayInputStream(
            doc.toString().getBytes("UTF-8")));

    renderer.setDocument(document, null);
    renderer.layout();
    renderer.createPDF(os);

我的代码的最终结果是:

在HTML文件中我得到: 一些希腊字符:Καλημέρα一些希腊字符 (预期)

在PDF文件中我得到: 一些希腊字符:一些希腊字符意外 - 希腊字符被忽略!!)

依赖关系:

  • java版“1.6.0_27”

  • iText的 - 2.0.8.jar

  • de.huxhorn.lilith.3rdparty.flyingsaucer.core - 渲染 - 8Pre2.jar

我也尝试过更多的字体,但我想我的问题与使用错误的字体无关。 任何帮助都非常受欢迎。

感谢名单

我来自捷克共和国,与我们的国家符号有同样的问题! 经过一番搜索,我设法用这个解决方案来解决它。

特别是(你已经拥有):

renderer
    .getFontResolver()
    .addFont(fonts.get(i).getFile().getPath(), 
             BaseFont.IDENTITY_H, 
             BaseFont.NOT_EMBEDDED);

然后在CSS中的重要部分:

* {
  font-family: Verdana;
/*  font-family: Times New Roman; - alternative. Without ""! */
}

在我看来,如果没有那个css,你的字体就不会被使用了。 当我从CSS中删除theese线时,编码再次被破坏。

希望这会有所帮助!

添加到您的HTML这样的东西:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'/>
        <style type='text/css'> 
            * { font-family: 'Arial Unicode MS'; }
        </style>
    </head>
    <body>
        <span>Some text with šđčćž characters</span>
    </body>
</html>

然后在Java代码中将FontResolver添加到ITextRenderer:

ITextRenderer renderer = new ITextRenderer();
renderer.getFontResolver().addFont("fonts/ARIALUNI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

非常适合克罗地亚人

用于生成PDF的jar是:

core-renderer.jar
iText-2.0.8.jar

iText从您的html内容中读取包含utf-8内容的标题信息。
使用utf-8 charset编码在html代码中添加content-type meta标记,然后运行iText生成PDF并检查结果。

<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
 </head>
 <body>
  Some greek characters: Καλημέρα Some greek characters
 </body>
</html>

更新
如果以上操作无效,请参阅http://www.manning.com/lowagie2/iText2E_MEAP_CH02.pdf上发布的文档中的ENCODING VERSUS THE DEFAULT CHARSET USED BY THE JVM

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM