Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/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使用重音符号从字符串保存pdf_Java_Encoding_Pdf Generation_Itext - Fatal编程技术网

java使用重音符号从字符串保存pdf

java使用重音符号从字符串保存pdf,java,encoding,pdf-generation,itext,Java,Encoding,Pdf Generation,Itext,我有一个带法语口音的简单字符串。我正在尝试使用ITextRenderer将其保存到pdf。问题是所有的重音符号都会从生成的pdf中删除 要保存的输入字符串来自我的velocity模板。在那里,我正在执行StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),这个过程将给我输入字符串,比如supplment:Visa&Pourboires” 我的代码: String documentHtml

我有一个带法语口音的简单字符串。我正在尝试使用ITextRenderer将其保存到pdf。问题是所有的重音符号都会从生成的pdf中删除

要保存的输入字符串来自我的velocity模板。在那里,我正在执行StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)),这个过程将给我输入字符串,比如supplment:Visa&Pourboires”

我的代码:

         String documentHtml = "Supplément : à&egrave"
         DocumentBuilder builder;
        try {
            DocumentBuilderFactory fac = DocumentBuilderFactory.newInstance();
            fac.setFeature("http://xml.org/sax/features/namespaces", false);
            fac.setFeature("http://xml.org/sax/features/validation", false);
            fac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
            fac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
            builder = fac.newDocumentBuilder();
            byte[] docByte = documentHtml.getBytes("UTF-8");
            ByteArrayInputStream is = new ByteArrayInputStream(docByte);
            Document doc = builder.parse(is);
            is.close();
            File file = new File(this.getFolder(), this.getFileName());
            if (file.exists()) {
                file.delete();
            }

            // save pdf
            OutputStream os = new FileOutputStream(file);
            ITextRenderer renderer = new ITextRenderer();
            renderer.setDocument(doc, file.getParentFile().getAbsolutePath());
            renderer.layout();
            renderer.createPDF(os, true);
            os.close();


            return this.getFolder().getAbsolutePath() + "/" + this.getFileName();
        } catch (ParserConfigurationException e) {
            LOGGER.error("Error while parsing the configuration " + e.getMessage(), e);
            throw new BOServiceException("Error while parsing the configuration : " + e.getMessage(), e);
        } catch (UnsupportedEncodingException e) {
            LOGGER.error("Encoding error :  " + e.getMessage(), e);
            throw new BOServiceException("Encoding error : " + e.getMessage(), e);
        } catch (SAXException e) {
            LOGGER.error("Error in the document because of SAX :  " + e.getMessage(), e);
            throw new BOServiceException("Error in the document because of SAX :  " + e.getMessage(), e);
        } catch (IOException e) {
            LOGGER.error("Error due to io problem : " + e.getMessage(), e);
            throw new BOServiceException("Error due to io problem :" + e.getMessage(), e);
        }

所以你知道为什么我的编码不起作用了吗?为什么在结果pdf中我看不到像aè这样的字符

试着把编码从UTF-8改成ISO-8859-1。

试着把编码从UTF-8改成ISO-8859-1。

读一下我不使用任何特殊字体(不需要样式字体)。这一定是编码问题。我的字符串是从velocity模板生成的,并没有额外的字体样式。每种字体都是“样式化”的。非样式化的字体是毫无意义的。如果您不明确指定字体,itext将设置一些默认字体。如果您不嵌入它,则取决于pdf阅读器如何显示您的pdf文档。通常情况下,它将帮助我们e类似的系统字体,并且该字体可能没有“
”符号的gliphs。是的,我正在阅读你的链接和文章,但所有的例子都是添加带有特定字体的段落或文本。在我的情况下,字体应该应用于我的最后一个字符串。你知道如何实现吗?如何将一种字体设置为整个html文档:只要读一下,我不使用任何特殊字体(不需要样式化字体)。这一定是编码问题。我的字符串是从velocity模板生成的,并没有额外的字体样式。每种字体都是“样式化”的。非样式化的字体是毫无意义的。如果您不明确指定字体,itext将设置一些默认字体。如果您不嵌入它,则取决于pdf阅读器如何显示您的pdf文档。通常情况下,它将帮助我们e类似的系统字体,并且该字体可能没有“
”符号的gliphs。是的,我正在阅读你的链接和文章,但所有的例子都是添加带有特定字体的段落或文本。在我的情况下,字体应该应用于我的最后一个字符串。你知道如何实现吗?如何将一种字体设置为整个html文档:如果我更改getBytes(“ISO-8859-1”),我将获得
code
ERROR[STDERR][致命错误]:66:7:3字节UTF-8序列的无效字节2。11:57:12109错误[PdfDocument]由于SAX,文档中出现错误:3字节UTF-8序列的无效字节2.org.xml.SAX.SAXParseException;行号:66;列号:7;3字节UTF-8序列的无效字节2.at org.apache.xerces.parsers.DOMParser.parse(未知源)字符串documentHtml也必须在您读取它的字符集中。对于这个字符串,它使用的是.java文件的编码。您的源代码是如何编码的?也许可以更改它们的编码。下面是iText中的编码示例:我在看这个示例,但实现与我的完全不同。他们使用的是段落在构造函数中使用字体路径的s和Font类,例如“c:/windows/fonts/arialbd.ttf”(我们将在具有不同路径的不同系统上运行应用程序)。是否可以避免这种情况?如果我更改getBytes(“ISO-8859-1”),我将获得
代码
错误[STDERR][致命错误]:66:7:3字节UTF-8序列的无效字节2。11:57:12109由于SAX导致文档中出现错误[PdfDocument]错误:3字节UTF-8序列的无效字节2。org.xml.SAX.SAXParseException;行号:66;列号:7;3字节UTF-8序列的无效字节2。位于org.apache.xerces.parsers.DOMParser.parse(未知源)字符串documentHtml也必须在您读取它的字符集中。对于这个字符串,它使用的是.java文件的编码。您的源代码是如何编码的?也许可以更改它们的编码。下面是iText中的编码示例:我在看这个示例,但实现与我的完全不同。他们使用的是段落在构造函数中使用字体路径的s和Font类,例如“c:/windows/fonts/arialbd.ttf”(我们将在具有不同路径的不同系统上运行应用程序)。是否可以避免这种情况?