0

Problem statement: preetify string in to json format and store in pdf file.
Earlier I am able to solve this problem by preetifying string using below code.

public Long convertJsonToPdf(Long customerId,
                                 Object json,
                                 String documentType) {
    log.info("In convert Json To Pdf");
    String fileName = documentType + ".pdf";
    ObjectMapper objectMapper = new ObjectMapper();
    String json1 = null;
    try {
        json1 = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(json);
    } catch (JsonProcessingException e) {
        log.warn("Error occurred in parsing json {}", e.getMessage(), e);
    }
    try {
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        document.open();
        document.add(new Paragraph(json1));
        Paragraph signature = new Paragraph("Created by me.");
        signature.setAlignment(Paragraph.ALIGN_RIGHT);
        document.add(Chunk.NEWLINE);
        document.add(Chunk.NEWLINE);
        document.add(signature);
        document.close();
    } catch (Exception e) {
        log.info("exception occurred in creating pdf {}", e.getMessage(), e);
    }  
    // code to store in cloud.
}

Above code was able to solve my problem. Now it is not able to solve my problem i.e not able preetify the json.

I tried other solutions from this preety-print-json-in-java.
I tried using JSONObject.

JSONObject json = new JSONObject(jsonString); // Convert text to object
System.out.println(json.toString(4)); // Print it with specified indentation

or this:

json1 = objectMapper.readTree(json.toString()).toPrettyString();

When I print json1 in my console it is able to preetify it properly. but when this is stored in pdf then at a position there is a gap to 3-4 line between two field. This error occur for a specific file. In other file it is able to print correctly in pdf.

json in pdf file:

"city": "city name",  





"state": "state name"
Swarnim Kumar
  • 335
  • 5
  • 21
  • *"Above code was able to solve my problem. Now it is not able to solve my problem i.e not able preetify the json."* - What? It worked before but it doesn't work now? You must have changed something. – Stephen C Jun 09 '21 at 07:03
  • No I didn't change anything. – Swarnim Kumar Jun 09 '21 at 07:03
  • So how come it stopped working? Software doesn't stop working unless something has been changed. – Stephen C Jun 09 '21 at 07:04
  • Currently, I don't know the reason that why it stopped. Need to dig deeper in to it. – Swarnim Kumar Jun 09 '21 at 07:18
  • I suspect that the problem maybe due to inconsistent handling of newline characters in a Paragraph. Try breaking the string into lines, and adding them as chunks with a Chunk.NEWLINE between each. There are apparently bugs in some versions of iText. See https://stackoverflow.com/questions/10409766 and http://itext.2136553.n4.nabble.com/newline-handling-inside-chunk-phrase-td4655550.html – Stephen C Jun 09 '21 at 07:27
  • And if this is the root cause then my guess is that the change (see above) is that you used a different version of iText. – Stephen C Jun 09 '21 at 07:29
  • (But I really don't see the value in turning JSON into a PDF. JSON is more useful as plain text ... unless you are in the deforestation business!) – Stephen C Jun 09 '21 at 07:32

0 Answers0