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