1

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.

Wienczny
  • 3,958
  • 4
  • 30
  • 35
  • 1
    I don't really have an answer, because it doesn't look like anything that you are doing would cause this, but usually "content not allowed in prolog" is caused by whitespace that gets inserted before the markup. Can't see why this would happen here, since you are working with a DOM. – erickson Sep 07 '10 at 16:50
  • This can also be caused by a Byte Order Mark appearing, and something like the print method not handling it. Try setting encoding options. – xcut Sep 07 '10 at 18:42
  • @xcut How should I set the BOM or encoding options? The document is never serialized in to a string or file by my code. Do encoding options really matter? – Wienczny Sep 08 '10 at 22:38

1 Answers1

0

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.

Wienczny
  • 3,958
  • 4
  • 30
  • 35