{"id":14279,"date":"2014-06-27T12:00:32","date_gmt":"2014-06-27T06:30:32","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=14279"},"modified":"2016-12-15T23:57:05","modified_gmt":"2016-12-15T18:27:05","slug":"generate-pdf-in-nodejs-using-node-phantom-module","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/generate-pdf-in-nodejs-using-node-phantom-module\/","title":{"rendered":"Generate PDF in NodeJS using &#8220;node-phantom&#8221; module"},"content":{"rendered":"<p><strong>PhantomJS<\/strong> is used to emulate browsers through command line, for generating PDFs, web page manipulation, headless testing and much more interesting stuff.<\/p>\n<p>Npm provides us a module called <strong>&#8220;node-phantom&#8221;<\/strong> which helps us to use PhantomJS in <a title=\"Nodejs development\" href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">our NodeJS application<\/a> in a non-fussy way. A prerequisite for using this module is, you must have PhantomJS binary installed on your system.<\/p>\n<p>Then you have to run <code>npm install node-phantom<\/code> and require the module <code>var phantom = require('node-phantom')<\/code> in your application.<\/p>\n<p>Let us consider a code snippet which make use of this module for generating PDF:<\/p>\n<p>[js]<br \/>\nvar html = ejs.render(htmlFileContent,<br \/>\n            {seller: &quot;Sakshi Tyagi&quot;, buyer: &quot;Test Buyer&quot;});<br \/>\n phantom.create(function (error, ph) {<br \/>\n    ph.createPage(function (error, page) {<br \/>\n      page.settings = {<br \/>\n        loadImages: true,<br \/>\n        localToRemoteUrlAccessEnabled: true,<br \/>\n        javascriptEnabled: true,<br \/>\n        loadPlugins: false<br \/>\n       };<br \/>\n      page.set(&#8216;viewportSize&#8217;, { width: 800, height: 600 });<br \/>\n      page.set(&#8216;paperSize&#8217;, { format: &#8216;A4&#8217;, orientation: &#8216;portrait&#8217;, border: &#8216;1cm&#8217; });<br \/>\n      page.set(&#8216;content&#8217;, html, function (error) {<br \/>\n        if (error) {<br \/>\n          console.log(&#8216;Error setting content: &#8216;, error);<br \/>\n        }<br \/>\n      });<\/p>\n<p>      page.onResourceRequested = function (rd, req) {<br \/>\n        console.log(&quot;REQUESTING: &quot;, rd[0][&quot;url&quot;]);<br \/>\n      }<br \/>\n      page.onResourceReceived = function (rd) {<br \/>\n        rd.stage == &quot;end&quot; &amp;&amp; console.log(&quot;LOADED: &quot;, rd[&quot;url&quot;]);<br \/>\n      }<br \/>\n      page.onLoadFinished = function (status) {<br \/>\n        page.render(url, function (error) {<br \/>\n          if (error) console.log(&#8216;Error rendering PDF: %s&#8217;, error);<br \/>\n          console.log(&quot;PDF GENERATED : &quot;, status);<br \/>\n          ph.exit();<br \/>\n          cb &amp;&amp; cb();<br \/>\n        });<br \/>\n      }<br \/>\n    });<br \/>\n  });<br \/>\n[\/js]<\/p>\n<p>Here, we first get the html content we want to display on pdf. <strong>&#8220;ejs&#8221;<\/strong> templating engine is used to render this html we have in <strong>&#8220;htmlFileContent&#8221;<\/strong> variable. Next, we start with<\/p>\n<p>Next, we start with <strong>create(callback)<\/strong> method in which we get two parameters error and an object of phantomJS. Method <strong>&#8220;createPage&#8221;<\/strong> is called to create a webpage. It returns us the page object on which we can call several functions and set properties like:<\/p>\n<p><strong>1. page.settings :<\/strong> For setting values for enabling and disabling various<br \/>\nproperties for example: javascriptEnabled, localToRemoteUrlAccessEnabled etc.<br \/>\n<strong>2. page.onResourceRequested() :<\/strong> For tracking the request, when a page requests a resource from a remote server.<br \/>\n<strong>3. page.onResourceReceived() :<\/strong> For tracking the response similarly.<br \/>\n<strong>4. page.onLoadFinished() :<\/strong> For debugging, to see if the requested resource is generated or not. Called when resource is loaded and return status.<br \/>\n<strong>5. page.set() :<\/strong> This method is a useful one, as it allow us to set values for various configuration variables for page for example &#8220;viewportSize&#8221;, &#8220;paperSize&#8221; and most important &#8220;content&#8221;.<\/p>\n<p>Once the page or pdf generation is done, we call <strong>ph.exit()<\/strong> to terminate the pdf generation.<\/p>\n<p>So, this was a very simple example of using the power, PhantomJS provides us with our NodeJS application.<\/p>\n<p>Thanks \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PhantomJS is used to emulate browsers through command line, for generating PDFs, web page manipulation, headless testing and much more interesting stuff. Npm provides us a module called &#8220;node-phantom&#8221; which helps us to use PhantomJS in our NodeJS application in a non-fussy way. A prerequisite for using this module is, you must have PhantomJS binary [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":8},"categories":[1185],"tags":[55,1449,1124,1303,292,1227],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14279"}],"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\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=14279"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/14279\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=14279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=14279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=14279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}