{"id":3090,"date":"2022-09-12T02:07:57","date_gmt":"2022-09-11T21:07:57","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=3090"},"modified":"2022-09-12T02:07:59","modified_gmt":"2022-09-11T21:07:59","slug":"convert-html-to-pdf-using-javascript-html2pdf-js-library","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/","title":{"rendered":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Install html2pdf Library<\/h2>\n\n\n\n<p>Add html2pdf library in your code using CDN.<\/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;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&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;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/html2pdf.js\/0.8.0\/html2pdf.bundle.min.js&quot;&gt;&lt;\/script&gt;<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">HTML to PDF Using JavaScript Code<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">index.html<\/h3>\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;htmlmixed&quot;,&quot;mime&quot;:&quot;text\/html&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;HTML&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;html&quot;}\">&lt;link rel=&quot;stylesheet&quot; href=&quot;https:\/\/maxcdn.bootstrapcdn.com\/bootstrap\/4.5.2\/css\/bootstrap.min.css&quot;&gt;\n&lt;div id=&quot;canvas_div_pdf&quot;&gt;\n\n&lt;!--Add HTML content you want to convert to PDF--&gt;\n&lt;p&gt;hello this is a paragraph&lt;\/p&gt;\n\n&lt;table class=&quot;table table-dark&quot;&gt;\n    &lt;thead&gt;\n      &lt;tr&gt;\n        &lt;th&gt;Firstname&lt;\/th&gt;\n        &lt;th&gt;Lastname&lt;\/th&gt;\n        &lt;th&gt;Email&lt;\/th&gt;\n      &lt;\/tr&gt;\n    &lt;\/thead&gt;\n    &lt;tbody&gt;\n      &lt;tr&gt;\n        &lt;td&gt;John&lt;\/td&gt;\n        &lt;td&gt;Doe&lt;\/td&gt;\n        &lt;td&gt;john@example.com&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;Mary&lt;\/td&gt;\n        &lt;td&gt;Moe&lt;\/td&gt;\n        &lt;td&gt;mary@example.com&lt;\/td&gt;\n      &lt;\/tr&gt;\n      &lt;tr&gt;\n        &lt;td&gt;July&lt;\/td&gt;\n        &lt;td&gt;Dooley&lt;\/td&gt;\n        &lt;td&gt;july@example.com&lt;\/td&gt;\n      &lt;\/tr&gt;\n    &lt;\/tbody&gt;\n  &lt;\/table&gt;\n  \n&lt;\/div&gt;\n&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/html2pdf.js\/0.8.0\/html2pdf.bundle.min.js&quot;&gt;&lt;\/script&gt;\n&lt;script&gt;\n\n var element = document.getElementById('canvas_div_pdf');\nhtml2pdf(element);\n&lt;\/script&gt;<\/pre><\/div>\n\n\n\n<p>You can also pass the custom\u00a0<code>options<\/code>\u00a0to the html2pdf library as shown below. It will add custom\u00a0<code>margin<\/code>\u00a0to the pdf document and also pass the\u00a0<code>filename<\/code>\u00a0and also pass the\u00a0<code>html2canvas<\/code>\u00a0and\u00a0<code>jspdf<\/code>\u00a0library.<\/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;}\">html2pdf(element, {\n  margin:       10,\n  filename:     'myfile.pdf',\n  image:        { type: 'jpeg', quality: 0.98 },\n  html2canvas:  { scale: 2, logging: true, dpi: 192, letterRendering: true },\n  jsPDF:        { unit: 'mm', format: 'a4', orientation: 'portrait' }\n});<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Output Screenshot<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"462\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot-1024x462.jpg\" alt=\"HTML to PDF Using JavaScript HTML2PDF.js Library Screenshot\" class=\"wp-image-3093\" title=\"HTML to PDF Using JavaScript HTML2PDF.js Library Screenshot\" srcset=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot-1024x462.jpg 1024w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot-300x135.jpg 300w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot-768x347.jpg 768w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot-1536x694.jpg 1536w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript_screenshot.jpg 1911w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Install html2pdf Library Add html2pdf library in your code using CDN. HTML to PDF Using JavaScript Code index.html You can also pass the custom\u00a0options\u00a0to the html2pdf library as shown below. It will add custom\u00a0margin\u00a0to the pdf document and also pass the\u00a0filename\u00a0and also pass the\u00a0html2canvas\u00a0and\u00a0jspdf\u00a0library. Output Screenshot<\/p>\n","protected":false},"author":1,"featured_media":3092,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112],"tags":[],"class_list":["post-3090","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Convert HTML to PDF Using JavaScript HTML2PDF.js Library<\/title>\n<meta name=\"description\" content=\"Install html2pdf Library Add html2pdf library in your code using CDN. &lt;script\" \/>\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\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Convert HTML to PDF Using JavaScript HTML2PDF.js Library\" \/>\n<meta property=\"og:description\" content=\"Install html2pdf Library Add html2pdf library in your code using CDN. &lt;script\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-11T21:07:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-11T21:07:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.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":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library","description":"Install html2pdf Library Add html2pdf library in your code using CDN. &lt;script","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\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/","og_locale":"en_US","og_type":"article","og_title":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library","og_description":"Install html2pdf Library Add html2pdf library in your code using CDN. &lt;script","og_url":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-09-11T21:07:57+00:00","article_modified_time":"2022-09-11T21:07:59+00:00","og_image":[{"width":880,"height":495,"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.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\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library","datePublished":"2022-09-11T21:07:57+00:00","dateModified":"2022-09-11T21:07:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/"},"wordCount":65,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.jpg","articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/","url":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/","name":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.jpg","datePublished":"2022-09-11T21:07:57+00:00","dateModified":"2022-09-11T21:07:59+00:00","description":"Install html2pdf Library Add html2pdf library in your code using CDN. &lt;script","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#primaryimage","url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.jpg","contentUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_using_javascript.jpg","width":880,"height":495,"caption":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library"},{"@type":"BreadcrumbList","@id":"https:\/\/www.edopedia.com\/blog\/convert-html-to-pdf-using-javascript-html2pdf-js-library\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Convert HTML to PDF Using JavaScript HTML2PDF.js Library"}]},{"@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:\/\/secure.gravatar.com\/avatar\/e5e68aef3ad8f0b83d56f4953c512c8e57bd2e6dc64daec33b5d0495d9058f51?s=96&d=mm&r=g","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\/3090","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=3090"}],"version-history":[{"count":2,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3090\/revisions"}],"predecessor-version":[{"id":3094,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3090\/revisions\/3094"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media\/3092"}],"wp:attachment":[{"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/media?parent=3090"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=3090"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=3090"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}