eKoopmans / html2pdf.js

Client-side HTML-to-PDF rendering using pure JS.
MIT License
4.17k stars 1.39k forks source link

Cannot get to set Headers or Footers using pdfcallback function #68

Closed apoorv601 closed 5 years ago

apoorv601 commented 6 years ago

Hi, I am using the following code but can't seem to get the header or footer in the pdf, can anybody suggest plz what am i doing wrong? Thanks in advance,

function pdfCallback(pdfObject) {
   var number_of_pages = pdfObject.internal.getNumberOfPages()
    var pdf_pages = pdfObject.internal.pages
    var myFooter = "Footer info"
    for (var i = 1; i < pdf_pages.length; i++) {
        // We are telling our pdfObject that we are now working on this page
        pdfObject.setPage(i)
        // The 10,200 value is only for A4 landscape. You need to define your own for other page sizes
        pdfObject.text(myFooter, .1, .2)
        pdfObject.text("my header text", .1, .1);
    } }
function generatepdf(){ document.getElementById('preview').classList.add('hide');
var element = document.getElementById('element-to-print');
html2pdf(element, {
  margin:       .1,
  filename:     'Interview_Form.pdf',
  image:        { type: 'jpeg', quality: 0.98 },
  html2canvas:  { dpi: 192 },
  jsPDF:        { unit: 'in', format: 'A4', orientation: 'portrait', footer: 'a' },
  pdfCallback: pdfCallback
});
document.getElementById('preview').classList.add('show');
};
eKoopmans commented 6 years ago

Hey @apoorv601, sorry I've been away. A couple suggestions:

  1. As far as I know, the footer option you set in the options for jsPDF doesn't do anything.
  2. Have you checked this post from 9 days ago? It gives some guidance on getting the number of pages. Try that instead of pdf_pages.length.
eKoopmans commented 5 years ago

Hi, the v0.9.0 API added ways to add extra content to the PDF (including header/footer) - see here for an example with page numbers.

Closing as resolved!

Jigisha-sublime commented 4 years ago

@eKoopmans From all the examples it's clear that we can add normal text to the pdf (i.e. page number) but, is there any way we can provide the dynamic header content in the pdf. For example, I want to repeat some specifically designed HTML headers and logo as a header on each page of the pdf so, is there any workaround for this? Please help if we have any solutions for this. because most of the libraries are failing to do that on the client-side.

hariharanbn commented 3 years ago

@Jigisha-sublime Have you found any solution?

ghost commented 2 years ago

i have thead and tfoot in element so what to do to print that on every page of pdf

ghost commented 2 years ago

var element = document.getElementById('order-table');