It seems that your example does work when it is run from a simple java test class like what is below, but it fails when it is executed in a servlet under a Tomcat application server, still trying to track down why!
From in a servlet it fails in both the 3.3 version of the JAR and the 4.0 version as well.
public class Test4 {
public static void main(String[] args) {
try {
String html = "<html><body><p>abc <i>def</i>.</p></body></html>";
InputStream stream = new ByteArrayInputStream(html.getBytes("UTF-8"));
Document doc = new Document(stream);
} catch (Exception e) {
e.printStackTrace();
}
}
}
The idea about using the DocumentBuilder.insertHtml sounds good however I am not quite sure how to use this when my current code is using the basic Document class to build the document. Is threre a way to ‘switch’ to the DocumentBuilder to use the insertHtml method, then switch back to the normal Document class?
Currently the method called by the servlet looks like this:
public Document export(dao.Document policy) throws Exception {
List<dao.Section> sections = DAOFactory.getSectionFactory().getByDocumentId(policy.getId(),0,500);
SectionsHelper.sort(sections);
Document doc = new Document();
doc.removeAllChildren();
formatStyles(doc);
Section section = new Section(doc);
doc.appendChild(section);
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.A4);
Body body = new Body(doc);
section.appendChild(body);
addHeading(doc,body,"Heading 1", policy.getName()+" ("+policy.getNumber()+")");
for(dao.Section documentSection : sections) {
addHeading(doc,body,"Heading");
String html = "<html><body><p>abc <i>def</i>.</p></body></html>";
InputStream stream = new ByteArrayInputStream(html.getBytes("UTF-8"));
Document hdoc = new Document(stream);
// Now try to insert hdoc into the main doc
}
return doc;
}
Thank you for additional information. Actually, you can use DocumentBuilder class to build whole document. I think, this would be easier than building document using DOM.
For instance, you can try using the following code:
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
// Specify page setup
builder.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
builder.getPageSetup().setPaperSize(PaperSize.A4);
// insert headings
builder.getParagraphFormat().setStyleIdentifier(StyleIdentifier.HEADING_1);
builder.writeln("This is my heading");
// Insert some HTML
String html = "<html><body><p>abc <i>def</i>.</p></body></html>";
builder.insertHtml(html);
Hi Alexey, thanks for the pointers, however I have already written a few thousand lines of code that generate my document without using DocumentBuilder, I am not quite sure how I would use document builder at the same time as using the basic Document class.
If necessary I will re-write the code to use the document builder, but it would save quite a few hours if I could work out how to setup a html document and just insert that.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.