使用飞碟打印时 DOM 创建的文档中不允许出现序言内容
我正在尝试使用我的飞碟打印一些文本(https://xhtmlrenderer.dev.java.net)。该文档是使用 DOM-API 生成的,但当打印开始时,出现“序言中不允许的内容”异常。这个异常的原因是什么?
我的代码是这样的:
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));
print 方法采用 Printable 并将其放入 PrintJob 中。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
XHTMLPrintable 不适用于仅存在于 RAM 中的文档。 XHTMLPrintable 尝试使用给定文档生成 URL。然后将此“url”用作 Graphics2DRenderer 的文档 - 失败。
然后我编写了自己的 XHTMLPrintable,它采用 Document 而不是 XHTMLPanel。
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.