简体   繁体   中英

java save pdf from string with accents

I have a simple string with french accents. I am trying to save it to the pdf using ITextRenderer. Problem is that all the accents are deleted from the resulting pdf.

Input string to be saved is coming from my velocity template. There, i am doinf StringEscapeUtils.escape(StringEscapeUtils.unescape(stringWithAccents)) and this process is giving me my input string, like Supplément : Visa&Pourboires".

My code:

         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);
        }

So u have idea why my encoding is not working? Why in the result pdf I cannot see characters like à&egrave

尝试将编码从UTF-8更改为ISO-8859-1。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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