3

I want to convert my XHTML text to a PDF. I converted it to FileOutputStream but I ca'nt find a way to pass it as an input to the ITextRenderer. Is that possible, and how?

the code :

String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());

 String HTML_TO_PDF = "ConvertedFile.pdf";
 OutputStream os = new FileOutputStream(HTML_TO_PDF);       

  ITextRenderer renderer = new ITextRenderer();
      //   renderer.loadDocument(finalXhtmlStream);  i can pass a file here can i pass an input or output stream ?     
         renderer.layout();
         renderer.createPDF(os) ;    
         os.close();
   System.out.println("done.");

note: I can pass a file to the ITextRenderer as following:

 String File_To_Convert = "report.xhtml";
 String url = new File(File_To_Convert).toURI().toURL().toString();
 String HTML_TO_PDF = "ConvertedFile.pdf";
 OutputStream os = new FileOutputStream(HTML_TO_PDF);       

 ITextRenderer renderer = new ITextRenderer();
     renderer.setDocument(url);      
     renderer.layout();
     renderer.createPDF(os);
 os.close();
   System.out.println("done.");

please let me know if I have to provide more details.

Ian R. O'Brien
  • 6,682
  • 9
  • 45
  • 73
mohammad
  • 2,142
  • 7
  • 35
  • 60
  • renderer.loadDocument() takes File but i want to pass a ByteArrayInputStream or ByteArrayOutputStream can i do that ? – mohammad Mar 11 '13 at 18:52

2 Answers2

6

I am using following code to export HTML data to PDF with following code:

renderer.setDocumentFromString(htmls.toString());
renderer.layout();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + ".pdf\"");
renderer.createPDF(outputStream);
renderer.createPDF(fos);

Now here I am using inline CSS to generate PDF using style but is there any option that I can use setDocumentFromString() function by loading external CSS.

informatik01
  • 16,038
  • 10
  • 74
  • 104
shrey
  • 833
  • 1
  • 7
  • 15
2

I am assuming you are using Flying Saucer. ITextRenderer has a method that does something similar:

public void setDocumentFromString(String content) {
    InputSource is = new InputSource(new BufferedReader(new StringReader(content)));
    Document dom = XMLResource.load(is).getDocument();

    setDocument(dom, null);
}

Adapting your code, what you'd want would look something like this:

String finalXhtml=xhtmlparser(xmlText);
ByteArrayInputStream finalXhtmlStream = new ByteArrayInputStream(finalXhtml.getBytes());

String HTML_TO_PDF = "ConvertedFile.pdf";
OutputStream os = new FileOutputStream(HTML_TO_PDF);      

Document document = XMLResource.load(finalXhtmlStream).getDocument();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(os) ;    
os.close();

of course you could also do this and skip the inputstream all together:

renderer.setDocumentFromString(finalXhtml);
Lodewijk Bogaards
  • 19,777
  • 3
  • 28
  • 52