In a project GitHub I'm trying to convert any arbitrary HTML string into a PDF version. By convert I mean parse the HTML, and render it into a PDF file.
To achieve that I'm using Flying Saucer PDF Rendering like this:
public class Main {
public static void main(String [] args) {
final String ok = "<valid html here>: see github rep for real html markup here";
final String html = "<invalid html here>: see github rep for real html markup here";
try {
// final byte[] bytes = generatePDFFrom(ok); // works!
final byte[] bytes = generatePDFFrom(html); // does NOT work :(
try(FileOutputStream fos = new FileOutputStream("sample-file.pdf")) {
fos.write(bytes);
}
} catch (IOException | DocumentException e) {
e.printStackTrace();
}
}
private static byte[] generatePDFFrom(String html) throws IOException, DocumentException {
final ITextRenderer renderer = new ITextRenderer();
renderer.setDocumentFromString(html);
renderer.layout();
try (ByteArrayOutputStream fos = new ByteArrayOutputStream(html.length())) {
renderer.createPDF(fos);
return fos.toByteArray();
}
}
}
In the above code if I use the html string stored in ok
variable (this is a "valid" html), it creates the PDF correctly (if you run the GitHub project by using the ok
variable it will create a file sample-file.pdf
inside the project folder with some rendered html).
Now, if I use the value in html
variable (html with invalid tags, tags maybe not closed properly, etc) it throws the following error (the error can vary depending on the incorrect value):
ERROR: 'The markup in the document following the root element must be well-formed.'
Exception in thread "main" org.xhtmlrenderer.util.XRRuntimeException: Can't load the XML resource (using TrAX transformer). org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 9; The markup in the document following the root element must be well-formed.
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:222)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.createXMLResource(XMLResource.java:181)
at org.xhtmlrenderer.resource.XMLResource.load(XMLResource.java:84)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:171)
at org.xhtmlrenderer.pdf.ITextRenderer.setDocumentFromString(ITextRenderer.java:166)
at Main.generatePDFFrom(Main.java:84)
at Main.main(Main.java:72)
Caused by: javax.xml.transform.TransformerException: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 9; The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:740)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:343)
at org.xhtmlrenderer.resource.XMLResource$XMLResourceBuilder.transform(XMLResource.java:220)
... 6 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 22; columnNumber: 9; The markup in the document following the root element must be well-formed.
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1239)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transformIdentity(TransformerImpl.java:659)
at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl.transform(TransformerImpl.java:728)
... 8 more
Now, as far as I understood this is because of the "invalid" parts of the html string.
Important notes:
- The values assigned to variables
ok
andhtml
here are just a placeholder for the question. Real ones are here. - In the real project, the html string is an input that comes from the user. Yes, he/she must know what to put there, but, of course, he/she can do some mistakes in the html conformation, so I have to handle this.
Question(s)
- Is there any way I can "tell" to Flying Saucer PDF Rendering to ignore/autocomplete/clean itself/or any other, those "invalid" parts and move on with the creation of the PDF file (preferred).
- Is there a better approach I can use in order to overcome this.