{"id":17754,"date":"2015-03-03T16:22:27","date_gmt":"2015-03-03T10:52:27","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=17754"},"modified":"2016-12-16T15:32:52","modified_gmt":"2016-12-16T10:02:52","slug":"generate-pdf-for-google-charts-through-javascript","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/generate-pdf-for-google-charts-through-javascript\/","title":{"rendered":"Generate PDF for Google Charts through Javascript"},"content":{"rendered":"<p>In a previous blog, we learned, <a title=\"Vertical Labels with Google Chart API\" href=\"http:\/\/www.tothenew.com\/blog\/vertical-labels-with-google-chart-api\/\">how to draw a google chart<\/a>.<\/p>\n<p>Let&#8217;s say, I have 5 graphs on my page and I want to generate a PDF which contains these graphs <a title=\"Javascript development services\" href=\"http:\/\/www.tothenew.com\/front-end-angularjs-development\">using a JavaScript<\/a>.<\/p>\n<p>For that, we will use <a href=\"https:\/\/github.com\/MrRio\/jsPDF\">jsPDF plugin<\/a>.<\/p>\n<p>Step 1. Create a hidden empty DIV, to store graph-images:<\/p>\n<p>[java]<br \/>\n&lt;div id=&#8217;graph-images&#8217; style=&#8217;display:none&#8217;&gt;&lt;\/div&gt;<br \/>\n[\/java]<\/p>\n<p>Step 2. Update your &#8216;drawMyChart()&#8217; function to get image of chart<\/p>\n<p>[java]<br \/>\nvar data = &#8230;<br \/>\nvar options =<br \/>\n   &#8230;<br \/>\n}<\/p>\n<p>var chart = new google.visualization.ComboChart(document.getElementById(&#8216;chart_div&#8217;)); \/* Define your ComboChart object *\/<br \/>\ngoogle.visualization.events.addListener(chart, &#8216;ready&#8217;, function () {<br \/>\n  var content = &#8216;&lt;img src=&quot;&#8217; + chart.getImageURI() + &#8216;&quot;&gt;&#8217;;<br \/>\n  $(&#8216;#graph-images&#8217;).append(content);<br \/>\n}); \/* Adds a listener to make a image of chart inside a #graph-images*\/<\/p>\n<p>chart.draw(data, options); \/* draw the chart using above define &#8216;data&#8217; and &#8216;options&#8217; *\/<br \/>\n[\/java]<\/p>\n<p>Google Chart API provides access to a PNG image of a chart, using the getImageURI() method. When the chart object is ready an &#8216;img&#8217; tag is appended in the &#8216;#graph-images&#8217; DIV.<\/p>\n<p>Step 3 : Include plugin javascript files : <a href=\"https:\/\/github.com\/MrRio\/jsPDF\/blob\/master\/jspdf.js\">jspdf.js<\/a>, <a href=\"https:\/\/github.com\/eligrey\/FileSaver.js\/blob\/master\/FileSaver.js\">FileSaver.js<\/a>, <a href=\"https:\/\/github.com\/MrRio\/jsPDF\/blob\/master\/libs\/png_support\/zlib.js\">zlib.js<\/a>, <a href=\"https:\/\/github.com\/MrRio\/jsPDF\/blob\/master\/libs\/png_support\/png.js\">png.js<\/a>,<a href=\"https:\/\/github.com\/MrRio\/jsPDF\/blob\/master\/plugins\/addimage.js\">jspdf.plugin.addimage.js<\/a>, and <a href=\"https:\/\/github.com\/MrRio\/jsPDF\/blob\/master\/plugins\/png_support.js\">jspdf.plugin.png_support.js<\/a><\/p>\n<p>Step 4 : Add functionality to generate a PDF of the above saved chart-images:<\/p>\n<p>[java]<br \/>\nfunction generatePDF() {<br \/>\nvar doc = new jsPDF(&#8216;p&#8217;, &#8216;pt&#8217;, &#8216;a4&#8217;, false) \/* Creates a new Document*\/<br \/>\ndoc.setFontSize(15); \/* Define font size for the document *\/<br \/>\nvar yAxis = 30;<br \/>\nvar imageTags = $(&#8216;#graph-images img&#8217;);<br \/>\nfor (var i = 0; i &lt; imageTags.length; i++) {<br \/>\n        if (i % 2 == 0 &amp;&amp; i != 0) { \/* I want only two images in a page *\/<br \/>\n            doc.addPage();  \/* Adds a new page*\/<br \/>\n            yAxis = 30; \/* Re-initializes the value of yAxis for newly added page*\/<br \/>\n        }<br \/>\n        var someText = &#8216;Chart &#8216;+(i+1);<br \/>\n        doc.text(60, yAxis, someText); \/* Add some text in the PDF *\/<br \/>\n        yAxis = yAxis + 20; \/* Update yAxis *\/<br \/>\n        doc.addImage(imageTags[i], &#8216;png&#8217;, 40, yAxis, 530, 350, undefined, &#8216;none&#8217;);<br \/>\n        yAxis = yAxis+ 360; \/* Update yAxis *\/<br \/>\n    }<br \/>\ndoc.save(&#8216;Chart_Report&#8217; + &#8216;.pdf&#8217;) \/* Prompts user to save file on his\/her machine *\/<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Here we are adding a chart heading and the image for all the charts drawn.<\/p>\n<p>Step 5: Whenever there&#8217;s an error adding any image in the PDF, jsPDF API throws an error &#8216;throw new Error()&#8217; and stops any further processing. For this we can provide exception handling:<\/p>\n<p>[java]<br \/>\n try {<br \/>\n        doc.addImage(imageTags[i], &#8216;png&#8217;, 40, yAxis, 530, 350, undefined, &#8216;none&#8217;);<br \/>\n        yAxis = yAxis+ 360; \/* Update yAxis *\/<br \/>\n    }<br \/>\n    catch (e) {<br \/>\n        doc.text(120, yAxis + 30, &#8216;SOME ERROR OCCURRED WHILE ADDING IMAGE&#8217;);<br \/>\n        yAxis = yAxis + 50 \/* Update yAxis *\/<br \/>\n    }<br \/>\n[\/java]<\/p>\n<p>Our code remains the same, we just need to move doc.addImage(*, *, *, *, *, *) into the try-catch block, so that whenever any error occurs while inserting an\u00a0image inside the PDF, we add following text: &#8216;SOME ERROR OCCURRED WHILE ADDING IMAGE&#8217;, and continue further.<\/p>\n<p>Hope this helps everyone!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a previous blog, we learned, how to draw a google chart. Let&#8217;s say, I have 5 graphs on my page and I want to generate a PDF which contains these graphs using a JavaScript. For that, we will use jsPDF plugin. Step 1. Create a hidden empty DIV, to store graph-images: [java] &lt;div id=&#8217;graph-images&#8217; [&hellip;]<\/p>\n","protected":false},"author":79,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":29},"categories":[7],"tags":[1669,1674,1671,1672,398,1648,1308,1670,1673],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17754"}],"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\/79"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=17754"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/17754\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=17754"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=17754"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=17754"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}