html2pdf using jsp and linux

Have you ever thought of downloading the current viewed page as PDF. Here’a  quick program which can read an HTML and convert it to PDF using JSP or Servlet. I am going to call an external linux command called ‘convert’ which does all the magic for me.

All I need is a linux box with ImageMagick setup along with html2ps and ghostscript. I can get this very very easily from my ubuntu’s apt-get install …

So here’s the small snippet which I wrote after setting up.

Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec(“convert /usr/local/share/pdfformat1.html /usr/local/share/pdfformat.pdf”  ) ;
p.waitFor();   //<– This waits until the operation is over
System.out.println(p.exitValue());  //<– if the output is 0 it means pdf was generated successfully
catch (Exception e){

Now this can be quite a good replacement for iText html to pdf conversion which is quite buggy (I am a huge fan of iText, but somehow this HTML needs to be rendered properly I feel)

After having got the file, read the file and reply to your browser by response.setContentType(“application/pdf”);

Please note, ONLY valid HTML tags are allowed. This method doesn’t support CSS. If someone knows of it, please let me know.This method works perfectly well with valid HTML syntax. So if your output pdf isn’t looking as expected, please check your HTML.

You can use this little utility to check your valid HTML #: sudo apt-get install wdg-html-validator and then run #: validate filename.html
This will list out all the invalid HTML tags.

So for all you report building guys out there, here’s a quick tip: Just output your report as HTML <offcourse with data poluated> and then use this utility to generate a PDF out of it!

Leave a comment

Your email address will not be published. Required fields are marked *