0

I am reading a file content and sending it to browser as content type "PDF",Data in the PDf not identifying line breaks.

I am using, Itext jar and formatting HTML using HTMLWorker.

Could any one help or any source code?

My Code so Far:

Document document = new Document(PageSize.LETTER);
response.setContentType("application/pdf");
try {
    PdfWriter.getInstance(document, response.getOutputStream());
    document.open();
    String cnt = SAMPLEJAVA.getFileContent("D:/abc.docx");
    System.out.println(cnt);
    HTMLWorker htmlWorker = new HTMLWorker(document);
    String str = "<html><body><pre>" + cnt + "</pre></body></html>";
    htmlWorker.parse(new StringReader(str));
    document.close();
    System.out.println("Done");
} catch(Exception ex)  {
}
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125
Madhan
  • 41
  • 2
  • 8
  • please provide code snippet that you have wrote for this functionality. – Jayesh Jul 19 '13 at 04:49
  • Document document = new Document(PageSize.LETTER); response.setContentType("application/pdf"); try { PdfWriter.getInstance(document, response.getOutputStream()); document.open(); String cnt=SAMPLEJAVA.getFileContent("D:/abc.docx"); System.out.println(cnt); HTMLWorker htmlWorker = new HTMLWorker(document); String str = "
    "+cnt+"
    "; htmlWorker.parse(new StringReader(str)); document.close(); System.out.println("Done"); } catch{ }
    – Madhan Jul 19 '13 at 04:56
  • I think your issue might be with line break char in "D:/abc.docx" file which is "\n" is not getting interpreted in PDF. You can take the content and try replacing newline char to some PDF understandable newline char and then send as response. this link might help http://stackoverflow.com/questions/861809/inserting-line-breaks-into-pdf – Jayesh Jul 19 '13 at 05:05
  • Hi Jayesh, that is not an issue, System.out.println(cnt) is giving me content perfectly and also,when I copy "str" in another html file it is fine,problem would be HTMLWorker – Madhan Jul 19 '13 at 05:12
  • I am not sure but I think you have to check what is the newline char understood by PDF. It will work perfectly in other scenario because place where you are trying there it understands "\n" as newline char. – Jayesh Jul 19 '13 at 05:15
  • HTMLWorker is now deprecated (http://api.itextpdf.com/itext/com/itextpdf/text/html/simpleparser/HTMLWorker.html). I think you should go with the XML Worker (http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html). – MaVRoSCy Jul 19 '13 at 05:50

1 Answers1

0

try replacing this line of code:

String str = "<html><body><pre>" + cnt + "</pre></body></html>";

with this:

String str = "<html><body><pre>" + cnt.replace("\n", "&#xD;") + "</pre></body></html>";
MaVRoSCy
  • 17,747
  • 15
  • 82
  • 125