I created a simple JavaFX (2.2) FXML project. I am trying to convert a html string to different Elements and add them to a Paragraph in my PDF document generated by iText. As HTMLWorker is deprecated, I use XMLWorker. It does seem to work with a own written simple html string, e.g.:
StringReader in = new StringReader("<html><body><p>test</p></body></html>");
However, when I try to use the html string generated by the JavaFX HTMLEditor control, I receive following message:
Caused by: java.lang.NoSuchMethodError: com.itextpdf.tool.xml.html.pdfelement.NoNewLineParagraph.setMultipliedLeading(F)V
at com.itextpdf.tool.xml.html.AbstractTagProcessor.currentContentToParagraph(AbstractTagProcessor.java:268)
at com.itextpdf.tool.xml.html.Span.end(Span.java:77)
at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:192)
at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:207)
at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:142)
at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:396)
at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:71)
at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:236)
at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:214)
at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:149)
at hmtltopdf.FXMLDocumentController.handleButtonAction(FXMLDocumentController.java:64)
... 54 more
My guess is that there are a bunch of tags in the html string that aren't recognized by XMLWorker. The HTML editor creates this html string:
<html><head></head><body contenteditable="true"><p style="text-align: left;"><font face="'Segoe UI'">test</font></p></body></html>
If that is the case, is there a way to 'clean' the html string? Or do I have to use another HTML editor of some sort?
Code of my sample project:
//using iText 5.3.1 and XMLWorker 5.5.0
@FXML
private HTMLEditor htmlEditor;
@FXML
private void handleButtonAction(ActionEvent event) {
final Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/test/loremipsum.pdf"));
document.open();
//StringReader in = new StringReader("<html><body><p>test</p></body></html>"); //this works
StringReader in = new StringReader(htmlEditor.getHtmlText()); //this does not work
try {
final Paragraph test = new Paragraph();
XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
@Override
public void add(final Writable w) {
if (w instanceof WritableElement) {
List<Element> elements = ((WritableElement) w).elements();
for (Element e : elements) {
test.add(e);
}
}
}
}, in);
document.add(test);
} catch (IOException | DocumentException e) {
System.out.println(e.toString());
System.out.println(e.getMessage());
}
document.close();
}
Edit:
It seems the problems start when using <b>
, <i>
, ... or other tags.
Ah it seems that it has to do something with using an older iText version. I've updated to iText 5.5.0 and it seems to work now. I still had to remove any <br>
and <hr>
tags thou.
//using iText 5.5.0 and XMLWorker 5.5.0
@FXML
private HTMLEditor htmlEditor;
@FXML
private void handleButtonAction(ActionEvent event) {
final Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("D:/test/loremipsum.pdf"));
document.open();
String htmlString = htmlEditor.getHtmlText();
htmlString = htmlString.replace("<br>", "");
htmlString = htmlString.replace("<br/>", "");
htmlString = htmlString.replace("<br />", "");
htmlString = htmlString.replace("<hr>", "<p></p>");
htmlString = htmlString.replace("<hr/>", "<p></p>");
htmlString = htmlString.replace("<hr />", "<p></p>");
StringReader in = new StringReader(htmlString);
try {
final Paragraph test = new Paragraph();
XMLWorkerHelper.getInstance().parseXHtml(new ElementHandler() {
@Override
public void add(final Writable w) {
if (w instanceof WritableElement) {
List<Element> elements = ((WritableElement) w).elements();
for (Element e : elements) {
test.add(e);
}
}
}
}, in);
document.add(test);
} catch (IOException | DocumentException e) {
System.out.println(e.toString());
System.out.println(e.getMessage());
}
document.close();
}