{"id":3072,"date":"2022-09-11T23:27:04","date_gmt":"2022-09-11T18:27:04","guid":{"rendered":"https:\/\/www.edopedia.com\/blog\/?p=3072"},"modified":"2022-09-11T23:29:10","modified_gmt":"2022-09-11T18:29:10","slug":"html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript","status":"publish","type":"post","link":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/","title":{"rendered":"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript"},"content":{"rendered":"\n<p>In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js Page Numbering is given below.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">HTML2PDF.js Page Numbering Source 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;!DOCTYPE html&gt;\n&lt;html lang=&quot;en&quot;&gt;\n&lt;head&gt;\n    &lt;meta charset=&quot;UTF-8&quot;&gt;\n    &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge&quot;&gt;\n    &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1.0&quot;&gt;\n    &lt;title&gt;Document&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;style&gt;\n    div {\n  outline: red solid 1px;\n  page-break-inside: avoid;\n}\n#nextpage1 {\n  page-break-before: always;\n}\n&lt;\/style&gt;\n&lt;script src=&quot;https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/html2pdf.js\/0.10.1\/html2pdf.bundle.min.js&quot; integrity=&quot;sha512-GsLlZN\/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA\/HdeKFVgA5v3S\/cIrLF7QnIg==&quot; crossorigin=&quot;anonymous&quot; referrerpolicy=&quot;no-referrer&quot;&gt;&lt;\/script&gt;\n\n&lt;body&gt;\n    \n\n&lt;!-- Button to generate PDF. --&gt;\n&lt;button id=&quot;btnTest&quot;&gt;Generate PDF&lt;\/button&gt;\n&lt;div id =&quot;root&quot;&gt;\n\n&lt;div&gt;\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n&lt;\/div&gt;\n&lt;div id=&quot;nextpage1&quot;&gt;\nLorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.\n&lt;\/div&gt;\n&lt;br&gt;\n\n&lt;\/div&gt;\n&lt;\/body&gt;\n&lt;script&gt;\n    function test() {\n  var element = document.getElementById('root');\n \n  \/\/ Generate the PDF.\n  html2pdf().from(element).set({\n  margin:       [1, 0, 0, 0], \n    filename: 'samplepdf.pdf',\n  \tpageBreak: { mode: 'css', before:'#nextpage1'},\n    jsPDF: {orientation: 'landscape', unit: 'in', format: 'letter'}\n  }).toPdf().get('pdf').then(function (pdf) {\n    var totalPages = pdf.internal.getNumberOfPages();\n\n    for (i = 1; i &lt;= totalPages; i++) {\n    \tpdf.setPage(i);\n      pdf.setFontSize(10);\n      pdf.setTextColor(150);\n    pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth()\/2.25), (pdf.internal.pageSize.getHeight()-8));\n      \n     \n      }\n  }).save();\n}\n\ndocument.getElementById('btnTest').onclick = test;\n&lt;\/script&gt;\n&lt;\/html&gt;<\/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=\"260\" src=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-1024x260.png\" alt=\"HTML2PDF Page Numbering\" class=\"wp-image-3074\" title=\"HTML2PDF Page Numbering\" srcset=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-1024x260.png 1024w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-300x76.png 300w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-768x195.png 768w, https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering.png 1357w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js Page Numbering is given below. HTML2PDF.js Page Numbering Source Code index.html Output Screenshot<\/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-3072","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>HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript<\/title>\n<meta name=\"description\" content=\"In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js\" \/>\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\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"Edopedia\" \/>\n<meta property=\"article:author\" content=\"trulyfurqan\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-11T18:27:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-09-11T18:29:10+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-1024x260.png\" \/>\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":"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript","description":"In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js","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\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/","og_locale":"en_US","og_type":"article","og_title":"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript","og_description":"In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js","og_url":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/","og_site_name":"Edopedia","article_author":"trulyfurqan","article_published_time":"2022-09-11T18:27:04+00:00","article_modified_time":"2022-09-11T18:29:10+00:00","og_image":[{"url":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/09\/html2pdf_page_numbering-1024x260.png","type":"","width":"","height":""}],"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\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#article","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/"},"author":{"name":"Furqan","@id":"https:\/\/www.edopedia.com\/blog\/#\/schema\/person\/3951cb19e3aa56df09e408c98aa02339"},"headline":"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript","datePublished":"2022-09-11T18:27:04+00:00","dateModified":"2022-09-11T18:29:10+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/"},"wordCount":57,"commentCount":0,"publisher":{"@id":"https:\/\/www.edopedia.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#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\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/","url":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/","name":"HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript","isPartOf":{"@id":"https:\/\/www.edopedia.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#primaryimage"},"image":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#primaryimage"},"thumbnailUrl":"https:\/\/www.edopedia.com\/blog\/wp-content\/uploads\/2022\/02\/default_featured_image.jpg","datePublished":"2022-09-11T18:27:04+00:00","dateModified":"2022-09-11T18:29:10+00:00","description":"In this tutorial, you will learn how to add page numbers in PDF documents using JavaScript and HTML2PDF.js library. The full source code of HTML2PDF.js","breadcrumb":{"@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.edopedia.com\/blog\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#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\/html2pdf-js-add-page-numbers-at-top-in-pdf-using-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edopedia.com\/blog\/"},{"@type":"ListItem","position":2,"name":"HTML2PDF.js Add Page Numbers at Top in PDF Using 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:\/\/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\/3072","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=3072"}],"version-history":[{"count":2,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3072\/revisions"}],"predecessor-version":[{"id":3076,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/posts\/3072\/revisions\/3076"}],"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=3072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/categories?post=3072"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edopedia.com\/blog\/wp-json\/wp\/v2\/tags?post=3072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}