Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用ITextRenderer从HTML生成pdf文件时的编码问题_Java_Pdf_Encoding_Itext - Fatal编程技术网

Java 使用ITextRenderer从HTML生成pdf文件时的编码问题

Java 使用ITextRenderer从HTML生成pdf文件时的编码问题,java,pdf,encoding,itext,Java,Pdf,Encoding,Itext,我正在尝试使用包含非拉丁字符的ITextRenderer生成pdf文档。就我而言,这里是保加利亚语 在调用ITextRenderer之前,我有一个字符串内容,在一些过程(比如使用tidy进行解析)之后看起来是这样的(我可以通过调试看到这个值) 内容: 我尝试过几种方法(主要与编码有关),但不幸的是,我还没有找到解决方案。可能我在这里遗漏了一些明显的东西:) 我不确定这是否会增加任何值,但我使用的是spring,并且该代码在控制器中运行 任何帮助都将不胜感激 Thanx您的HTML是否指定UTF-

我正在尝试使用包含非拉丁字符的ITextRenderer生成pdf文档。就我而言,这里是保加利亚语

在调用ITextRenderer之前,我有一个字符串内容,在一些过程(比如使用tidy进行解析)之后看起来是这样的(我可以通过调试看到这个值)

内容:

我尝试过几种方法(主要与编码有关),但不幸的是,我还没有找到解决方案。可能我在这里遗漏了一些明显的东西:)

我不确定这是否会增加任何值,但我使用的是spring,并且该代码在控制器中运行

任何帮助都将不胜感激


Thanx

您的HTML是否指定UTF-8编码?在该路径中是否找到您的字体文件?看一看,它通过提供一个指向系统中字体默认位置的路径,在Linux上对汉字有效。Thanx作为回复。你认为这是字体问题吗?我是否需要特定字体来显示非拉丁字符?我很确定我的字体在正确的位置,但我会尝试一下,我会让你知道你好。我仔细检查了一下。字体加载正确。我还运行了您建议的FontTest。我在那里也面临同样的问题。我加载支持西里尔字母的字体。但是pdf会忽略它们并打印空行。有什么建议吗?我刚刚添加了一个新帖子,更详细地解释了我的问题
td class="description">Вид на потока</td>
td class="description">Статус на потока</td>
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

        dbf.setValidating(false);
        dbf.setNamespaceAware(false);
        dbf.setFeature("http://xml.org/sax/features/namespaces", false);
        dbf.setFeature("http://xml.org/sax/features/validation", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
        dbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

        DocumentBuilder builder = dbf.newDocumentBuilder();

        Document doc = builder.parse(new ByteArrayInputStream(content.getBytes("UTF-8")));

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        InputStream is = null;

        ITextRenderer renderer = new ITextRenderer();

        renderer.getFontResolver().addFont("fonts/TIMES.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.getFontResolver().addFont("fonts/TIMESBD.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.getFontResolver().addFont("fonts/TIMESBI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.getFontResolver().addFont("fonts/TIMESI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);


        renderer.setDocument(doc, null);
        renderer.layout();
        renderer.createPDF(outputStream);
        outputStream.close();


        byte[] outputBytes = outputStream.toByteArray();
        is = new ByteArrayInputStream(outputBytes);
        response.setContentType("application");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + "exported.pdf" + "\"");
        response.setContentLength(outputBytes.length);
        response.getOutputStream().write(inputStreamToBytes(is));