Ultimately the solution came to be:
- Convert the html added by user into xhtml using JTidy
- Use flying saucer's ITextRenderer (http://code.google.com/p/flying-saucer/) to generate the pdf out of xhtml.
public class TextSectionConverter {
private String notesContent;
public TextSectionConverter(String notesContent) {
this.notesContent = notesContent;
}
public void writeAsPdf(FileOutputStream fos)
throws Exception {
convertToXHTML();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(notesContent);
renderer.layout();
renderer.createPDF(fos);
}
void convertToXHTML() throws Exception {
notesContent = "" + notesContent + "";
StringWriter writer = new StringWriter();
Tidy tidy = new Tidy();
tidy.setTidyMark(false);
tidy.setDocType("omit");
tidy.setXHTML(true);
tidy.setInputEncoding("utf-8");
tidy.setOutputEncoding("utf-8");
tidy.parse(new StringReader(notesContent), writer);
writer.close();
notesContent = writer.toString();
}
No comments:
Post a Comment