Search code examples
javaspring-bootcharacter-encodingpdfboxflying-saucer

Character encoding for French locale while creating PDF - Java


I have a spring boot application which renders a XML document into PDF. The document contains French characters likeé à. While running the application through STS I have no issues the PDF is generated as expected. But while running the application through command line using java -jar target\application.jar the generated PDF has French characters as é Ã. I am converting the XML into byte[] and creating the PDF. I couldn't figure out a way out. Any help is much appreciated.


Solution

  • Two options:

    1. Force the encoding with the file.encoding argument, such as -Dfile.encoding=utf-8.

      java -Dfile.encoding=utf-8 -jar target\application.jar
      
    2. (better) When you convert the xml file into a byte array, specify the encoding:

      Reader reader = new InputStreamReader(new FileInputStream("/path/to/xml/file"), StandardCharsets.UTF_8);
      // do your file reading ...