krackmoe
krackmoe

Reputation: 1763

I have a org.w3c.dom.Document and want to save it to my filesystem

I create my pdf document out of a html File. I want to save it to my Filesystem after creating it. But i dont know how to save it... can you please help me saving this document? So that it is saved as a pdf then?

final DocumentBuilderFactory documentBuilderFactory =  DocumentBuilderFactory.newInstance();
documentBuilderFactory.setValidating(false);
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
builder.setEntityResolver(FSEntityResolver.instance());

org.w3c.dom.document = builder.parse(new ByteArrayInputStream(result.getBytes("UTF-8")), "UTF-8");

baos = new ByteArrayOutputStream();

ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(document, null);
renderer.layout();
renderer.createPDF(baos);
out.println(baos.toString());
baos.close();

Upvotes: 0

Views: 1592

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500535

If the code you've got is correct as far as you're aware, but is simply writing to the wrong place (memory instead of a file) you just need to use a FileOutputStream:

FileOutputStream output = new FileOutputStream(filename);
try {
    renderer.createPDF(output);
} finally {
    output.close();
}

Upvotes: 4

Related Questions