{"id":2088,"date":"2022-07-06T13:45:10","date_gmt":"2022-07-06T08:45:10","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=2088"},"modified":"2022-07-06T13:47:31","modified_gmt":"2022-07-06T08:47:31","slug":"angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/","title":{"rendered":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript"},"content":{"rendered":"\n<p>On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>On click execute <code>generateAllPdf()<\/code> function<\/li><li>gather all 6 <code>id<\/code>&#8216;s from my HTML collection<\/li><li>iterate through each id and call the html2canvas function,<\/li><li>as html2canvas runs in the background to process images, I&#8217;m using await on function,<\/li><li>after the html2canvas completes its process, I&#8217;ll save the document,<\/li><li>Suppose, if I don&#8217;t use await, I&#8217;ll end up downloading an empty page.<\/li><\/ul>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/ As All Functions in js are asynchronus, to use await i am using async here\n async generateAllPdf() {\n    const doc = new jsPDF('p', 'mm', 'a4');\n    const options = {\n      pagesplit: true\n    };\n    const ids = document.querySelectorAll('[id]');\n    const length = ids.length;\n    for (let i = 0; i &lt; length; i++) {\n      const chart = document.getElementById(ids[i].id);\n      \/\/ excute this function then exit loop\n      await html2canvas(chart, { scale: 1 }).then(function (canvas) { \n        doc.addImage(canvas.toDataURL('image\/png'), 'JPEG', 10, 50, 200, 150);\n        if (i &lt; (length - 1)) {\n          doc.addPage();\n        }\n      });\n    }\n    \/\/ download the pdf with all charts\n    doc.save('All_charts_' + Date.now() + '.pdf');\n  }<\/pre><\/div>\n\n\n\n<p>I think the issue here is that <code>elementTobePrinted<\/code> is not what you think it is.<\/p>\n\n\n\n<p>When you run the code:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">var elementTobePrinted = angular.element(attrs.selector)<\/pre><\/div>\n\n\n\n<p>This will return you a list of every element that matches the conditions, so you said you have 6 of these elements (\u201cIt has 6 divs\u201d).<\/p>\n\n\n\n<p>Have you tried replacing:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">html2canvas(elementTobePrinted, {\n  onrendered: function (canvas) {\n    var doc = new jsPDF();     \n    for(var i=1;i&lt;elementTobePrinted.length;i++) {\n      doc.addImage(canvas.toDataURL(&quot;image\/jpeg&quot;), 'jpeg', 15, 40, 180, 160);\n      doc.addImage(canvas.toDataURL(&quot;image\/jpeg&quot;),'JPEG', 0, 0, 215, 40)\n      doc.addPage();\n    } \n    doc.save(attrs.fileName);\n}<\/pre><\/div>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:true,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;JavaScript&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;js&quot;}\">for(var i=0; i&lt;elementTobePrinted.length; i++){\n  html2canvas(elementTobePrinted[i], {\n    onrendered: function (canvas) {\n      var doc = new jsPDF();     \n      doc.addImage(canvas.toDataURL(&quot;image\/jpeg&quot;), 'jpeg', 15, 40, 180, 160);\n      doc.addImage(canvas.toDataURL(&quot;image\/jpeg&quot;),'JPEG', 0, 0, 215, 40)\n      doc.addPage();\n      doc.save(attrs.fileName);\n    }\n}<\/pre><\/div>\n\n\n\n<p>The reason I suggest this is that html2Canvas wants a <strong>SINGLE<\/strong> element as its first parameter and your example above passes a list of elements (I think, assuming <code>angular.element(attrs.selector)<\/code> finds all 6 divs you are trying to print).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() function gather all 6 id&#8216;s from my HTML collection iterate through each id and call the html2canvas function, as html2canvas runs in the background to process images, I&#8217;m using await on function, after the html2canvas &#8230; <a title=\"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/\" aria-label=\"Read more about Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1762,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-2088","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript<\/title>\n<meta name=\"description\" content=\"On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() functiongather all 6\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript\" \/>\n<meta property=\"og:description\" content=\"On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() functiongather all 6\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-06T08:45:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-07-06T08:47:31+00:00\" \/>\n<meta name=\"author\" content=\"Furqan\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Furqan\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript","description":"On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() functiongather all 6","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/","og_locale":"en_US","og_type":"article","og_title":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript","og_description":"On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() functiongather all 6","og_url":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-07-06T08:45:10+00:00","article_modified_time":"2022-07-06T08:47:31+00:00","author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript","datePublished":"2022-07-06T08:45:10+00:00","dateModified":"2022-07-06T08:47:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/"},"wordCount":174,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/","url":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/","name":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-07-06T08:45:10+00:00","dateModified":"2022-07-06T08:47:31+00:00","description":"On the Angular 2 framework (now Angular 13), you can use the step-by-step logic mentioned below: On click execute generateAllPdf() functiongather all 6","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","width":880,"height":495,"caption":"Default Featured Image"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/angular-13-jspdf-html2canvas-project-export-multiple-html-div-content-in-different-pdf-pages-using-typescript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Angular 13 jsPDF Html2Canvas Project: Export Multiple HTML Div Content in Different PDF Pages Using TypeScript"}]},{"@type":"WebSite","@id":"https:\/\/www.edopedia.com\/blog\/#website","url":"https:\/\/www.edopedia.com\/blog\/","name":"Edopedia","description":"Coding\/Programming Blog","publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edopedia.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.edopedia.com\/blog\/#organization","name":"Edopedia","url":"https:\/\/www.edopedia.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2017\/10\/edopedia_icon_text_10.jpg","width":400,"height":100,"caption":"Edopedia"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339","name":"Furqan","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","caption":"Furqan"},"description":"Well. I've been working for the past three years as a web designer and developer. I have successfully created websites for small to medium sized companies as part of my freelance career. During that time I've also completed my bachelor's in Information Technology.","sameAs":["http:\/\/www.edopedia.com\/blog\/","trulyfurqan"],"url":"https:\/\/www.edopedia.com\/blog\/author\/furqan\/"}]}},"_links":{"self":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2088","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/comments?post=2088"}],"version-history":[{"count":0,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2088\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/1762"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=2088"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=2088"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=2088"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}