{"id":2092,"date":"2022-07-06T14:01:34","date_gmt":"2022-07-06T09:01:34","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=2092"},"modified":"2022-09-18T14:34:12","modified_gmt":"2022-09-18T09:34:12","slug":"jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/","title":{"rendered":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to use<strong> jsPDF Html2Canvas to print hidden div<\/strong>.<\/p>\n\n\n\n<p>To hide an HTML tag; add this attribute tag <code>data-html2canvas-ignore=\"true\"<\/code> instead of the <code>hidden<\/code>.<\/p>\n\n\n\n<p>So with <strong>Mario Alexandro Santini\u2019s<\/strong> suggestion in the comments, I was able to solve the problem. I used <strong>jquery<\/strong> to unhide the <code>div<\/code> in my <code>makePdf()<\/code> function then hide it again after jsPDF and html2canvas had done their \u201cmagic\u201d:<\/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;}\">function makePdf() {\n    $(&quot;#divToPdf&quot;).attr(&quot;hidden&quot;, false);\n    ...\n    $(&quot;#divToPdf&quot;).attr(&quot;hidden&quot;, true);\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;css&quot;,&quot;mime&quot;:&quot;text\/css&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;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">@media print {\n   div[hidden] {\n      display: block;\n   }\n   ...\n}<\/pre><\/div>\n\n\n\n<p>You could change the layout of your page on different media through CSS.<\/p>\n\n\n\n<p>This is true for printing too.<\/p>\n\n\n\n<p>So you could write a dedicated stylesheet that is valid only when you print the page in pdf.<\/p>\n\n\n\n<p>Please have a look at:<\/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;css&quot;,&quot;mime&quot;:&quot;text\/css&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;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">@media print {\n   ...\n}\n<\/pre><\/div>\n\n\n\n<p>For your example you could use a code like:<\/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;css&quot;,&quot;mime&quot;:&quot;text\/css&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;CSS&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;css&quot;}\">@media print {\n   div[hidden] {\n      &lt;span class=&quot;hljs-attr&quot;&gt;display&lt;\/span&gt;: block;\n   }\n   ...\n}\n<\/pre><\/div>\n\n\n\n<p>This should select the div with <code>hidden<\/code> attribute and made those visible.<\/p>\n\n\n\n<p>If you prefer the programmatic approach then you could get all div in the page with attribute hidden and remove the attribute, print your document, then put the attribute back.<\/p>\n\n\n\n<p>You could use something like:<\/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 hideDivs = document.querySelectorAll(&quot;div[hidden]&quot;);<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag data-html2canvas-ignore=&#8221;true&#8221; instead of the hidden. So with Mario Alexandro Santini\u2019s suggestion in the comments, I was able to solve the problem. I used jquery to unhide the div in my makePdf() &#8230; <a title=\"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript\" class=\"read-more\" href=\"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/\" aria-label=\"Read more about jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":3352,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-2092","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>jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag\" \/>\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\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-07-06T09:01:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-18T09:34:12+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"880\" \/>\n\t<meta property=\"og:image:height\" content=\"495\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\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=\"1 minute\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript","description":"In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag","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\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/","og_locale":"en_US","og_type":"article","og_title":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript","og_description":"In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag","og_url":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-07-06T09:01:34+00:00","article_modified_time":"2022-09-18T09:34:12+00:00","og_image":[{"width":880,"height":495,"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg","type":"image\/jpeg"}],"author":"Furqan","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Furqan","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript","datePublished":"2022-07-06T09:01:34+00:00","dateModified":"2022-09-18T09:34:12+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/"},"wordCount":184,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/","url":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/","name":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg","datePublished":"2022-07-06T09:01:34+00:00","dateModified":"2022-09-18T09:34:12+00:00","description":"In this tutorial, you will learn how to use jsPDF Html2Canvas to print hidden div. To hide an HTML tag; add this attribute tag","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/07\/jsPDF_Html2Canvas_Hidden_Div_PDF_Print.jpg","width":880,"height":495,"caption":"jsPDF Html2Canvas Convert Div With Attribute Hidden to PDF and Print it"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/jspdf-html2canvas-tutorial-convert-div-with-attribute-hidden-to-pdf-and-print-it-in-browser-using-html5-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"jsPDF Html2Canvas Tutorial: Convert Div With Attribute Hidden to PDF and Print it in Browser Using HTML5 and JavaScript"}]},{"@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\/2092","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=2092"}],"version-history":[{"count":1,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2092\/revisions"}],"predecessor-version":[{"id":3351,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/2092\/revisions\/3351"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/3352"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=2092"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=2092"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=2092"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}