HTML2PDF.js Add Page Numbers at Top in PDF Using JavaScript

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

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    div {
  outline: red solid 1px;
  page-break-inside: avoid;
}
#nextpage1 {
  page-break-before: always;
}
</style>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js" integrity="sha512-GsLlZN/3F2ErC5ifS5QtgpiJtWd43JWSuIgh7mbzZ8zBps+dvLusV+eNQATqgA/HdeKFVgA5v3S/cIrLF7QnIg==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<body>
    

<!-- Button to generate PDF. -->
<button id="btnTest">Generate PDF</button>
<div id ="root">

<div>
Lorem 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.
</div>
<div id="nextpage1">
Lorem 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.
</div>
<br>

</div>
</body>
<script>
    function test() {
  var element = document.getElementById('root');
 
  // Generate the PDF.
  html2pdf().from(element).set({
  margin:       [1, 0, 0, 0], 
    filename: 'samplepdf.pdf',
  	pageBreak: { mode: 'css', before:'#nextpage1'},
    jsPDF: {orientation: 'landscape', unit: 'in', format: 'letter'}
  }).toPdf().get('pdf').then(function (pdf) {
    var totalPages = pdf.internal.getNumberOfPages();

    for (i = 1; i <= totalPages; i++) {
    	pdf.setPage(i);
      pdf.setFontSize(10);
      pdf.setTextColor(150);
    pdf.text('Page ' + i + ' of ' + totalPages, (pdf.internal.pageSize.getWidth()/2.25), (pdf.internal.pageSize.getHeight()-8));
      
     
      }
  }).save();
}

document.getElementById('btnTest').onclick = test;
</script>
</html>

Output Screenshot

HTML2PDF Page Numbering

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.