{"id":22973,"date":"2015-07-14T14:58:46","date_gmt":"2015-07-14T09:28:46","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=22973"},"modified":"2015-07-30T11:33:52","modified_gmt":"2015-07-30T06:03:52","slug":"html-to-pdf-using-phantomjs-and-grails","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/html-to-pdf-using-phantomjs-and-grails\/","title":{"rendered":"HTML to PDF using PhantomJs and Grails"},"content":{"rendered":"<p>Converting an html or web page into a pdf is never easy and\u00a0you might face\u00a0similar\u00a0problem in\u00a0your project as well. There is \u00a0a solution that works out of the box for this problem and all you need is installing PhantomJs on your\u00a0system and calling it from your Grails or Java code.<\/p>\n<p>Just follow below steps one by one and you are done with converting your html page into a good looking pdf file.<\/p>\n<p>1. First\u00a0you need to install PhantomJS on your system (assuming you are using linux system). use below command for this purpose.<\/p>\n<p><code>sudo apt-get install phantomjs<\/code><\/p>\n<p>2. You will also need phantomJS script &#8220;rasterize.js&#8221; \u00a0for converting web page to pdf. \u00a0Place this file into your system on any location of your choice and you can easily get this script \u00a0from below github url:<\/p>\n<p>https:\/\/github.com\/ariya\/phantomjs\/blob\/master\/examples\/rasterize.js<\/p>\n<p>3. Now, since\u00a0you are done with the prerequisites\u00a0of installing phantomJs and downloading rasterize.js, lets <a title=\"Grails Migration Services\" href=\"http:\/\/www.tothenew.com\/grails-application-development\">move to grails<\/a> or java code. Create an endpoint that calls an external runtime process, this is the critical code that calls the operating system to run PhantomJS and execute the script.<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\nProcess process = Runtime.getRuntime().exec(pathToPhantomJS + &quot; &quot; + pathToRasterizeJS + &quot; &quot; + url + &quot; &quot; + pathToYourOutputFile + &quot; &quot; + paperSize);<br \/>\n[\/code]<\/p>\n<p>Below is the grails code snippet that you can use as referance:<\/p>\n<p>[code language=&#8221;java&#8221;]<br \/>\ndef downloadPdf() {<br \/>\nString pathToPhantomJS = &quot;\/usr\/bin\/phantomjs&quot; \/\/path to your phantom js<br \/>\nString pathToRasterizeJS = &quot;\/home\/tothenew\/Desktop\/rasterize.js&quot; \/\/path to your rasterize.js<br \/>\nString paperSize = &quot;A4&quot;<br \/>\nString url = &quot;https:\/\/www.google.co.in\/&quot; \/\/url of your web page to which you want to convert into pdf<br \/>\nFile outputFile = File.createTempFile(&quot;sample&quot;, &quot;.pdf&quot;) \/\/file in which you want to save your pdf<\/p>\n<p>\/\/TODO: also do exception handling stuff . i am not doing this for simplicity<\/p>\n<p>Process process = Runtime.getRuntime().exec(pathToPhantomJS + &quot; &quot; + pathToRasterizeJS + &quot; &quot; + url + &quot; &quot; + outputFile.absolutePath + &quot; &quot; + paperSize);<br \/>\nint exitStatus = process.waitFor(); \/\/do a wait here to prevent it running for ever<br \/>\nif (exitStatus != 0) {<br \/>\nlog.error(&quot;EXIT-STATUS &#8211; &quot; + process.toString());<br \/>\n}<\/p>\n<p>response.contentType = &quot;application\/pdf&quot;<br \/>\nresponse.setHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=sample.pdf&quot;);<br \/>\nresponse.outputStream &lt;&lt; outputFile.bytes<br \/>\nresponse.outputStream.flush()<br \/>\nresponse.outputStream.close()<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Now all you need is hitting your controller action and watching your browser downloading pdf.<\/p>\n<p><em>Furhter reading: If you want to explore phantomjs screen capture in more detail, you can refer to the example at below\u00a0url:<\/em><\/p>\n<p>http:\/\/phantomjs.org\/screen-capture.html<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Converting an html or web page into a pdf is never easy and\u00a0you might face\u00a0similar\u00a0problem in\u00a0your project as well. There is \u00a0a solution that works out of the box for this problem and all you need is installing PhantomJs on your\u00a0system and calling it from your Grails or Java code. Just follow below steps one [&hellip;]<\/p>\n","protected":false},"author":229,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[7,446],"tags":[1672,1986,1227],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22973"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/229"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=22973"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/22973\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=22973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=22973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=22973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}