content not allowed in prolog from document created by DOM when printing using flying saucer

538 Views Asked by At

I'm trying to print some text using my flying saucer (https://xhtmlrenderer.dev.java.net). The document is generated using DOM-API but when the print starts there is a 'content not allowed in prolog' exception. What is the reason for this exception?

My code is this:

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder;
documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element html = document.createElement("html");
document.appendChild(html);
Element body = document.createElement("body");
html.appendChild(body);
for (String paragraph : paragraphs) {
    Element paragraphTag = document.createElement("p");
    paragraphTag.setTextContent(paragraph);
    body.appendChild(paragraphTag);
}
XHTMLPanel panel = new XHTMLPanel();
panel.setDocument(document);

print(new XHTMLPrintable(panel));

The print method takes a Printable and puts it into a PrintJob.

1

There are 1 best solutions below

0
On BEST ANSWER

The XHTMLPrintable does not work with Documents that just exist in RAM. The XHTMLPrintable trys to generate a URL using the given document. This 'url' is then used a document for the Graphics2DRenderer - fail. I then wrote my own XHTMLPrintable that takes a Document instead of a XHTMLPanel.