Create pdf file from html using Flying Saucer, render problem

I am trying to create a pdf using itext, but I am getting an error on the line:

ItextRenderer renderer = new ITextRenderer(); 

The error looks something like this:

 05-20 12:00:48.468: E/AndroidRuntime(6121): java.lang.VerifyError: org/xhtmlrenderer/pdf/ITextOutputDevice 

Creates a file, but the file is an error (size 0kb and not open)

 private void HTMLtoPDF(){ try{ String outputFile = "sdcard/firstdoc.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); renderer.setDocumentFromString(getHtmlBuffer().toString()); renderer.layout(); renderer.createPDF(os); renderer.finishPDF(); os.close(); }catch (Exception e) { e.printStackTrace(); } } private StringBuffer getHtmlBuffer() { StringBuffer buff = new StringBuffer(); buff.append("<html xmlns='http://www.w3.org/1999/xhtml'>"); buff.append("<body>"); buff.append("<h2 style='color: #353535;font-size: 16px;margin: 0px;padding: 20px 0px;font-weight: bold;'>Test</h2>"); buff.append("<div id='news-details' class='news-details'>"); buff.append("<div class='news-details-data'>"); buff.append("some japanes charactors"); buff.append("</div></div>"); buff.append("</body></html>"); return buff; } 
+4
source share
2 answers

I also looked for HTML to HTML converter for Android and studied Flying Saucer. I ran into the same problem and noticed that Flying Saucer is not an Android library, but a JAVA library. It uses Java classes that are not available for Android (for example: java.awt. *), So the banner will not work on an Android device.

+1
source

Format your HTML correctly, I also had a problem. itext does not accept HTML unformatted string, your pdf file is damaged.

+1
source

Source: https://habr.com/ru/post/1481731/


All Articles