jsPDF-AutoTable icon indicating copy to clipboard operation
jsPDF-AutoTable copied to clipboard

Generate report with header and footer and image in body

Open ruhijain opened this issue 11 years ago • 15 comments

Hi,

I am looking for generating a report.

The report will typically contain:

  • A header and a header logo image
  • A graph image in document body
  • Then a tabular data , using jsPDF autoTable plugin already
  • Then the page footer with page number

For now, I have the png file for body image and header logo and i have the pdf already generating with tabular data using auto Table plugin.

What am i supposed to do ti add graph image, header or details text data and footer as page number in the pdf report generated?

ruhijain avatar Jun 22 '15 09:06 ruhijain

Hi! Check out this demo for how to use headers and footers. It has no image, but it can be added with the relevant jspdf method. The code for the above demo and all other examples is in this file.

simonbengtsson avatar Jun 22 '15 12:06 simonbengtsson

Thanks i managed to put headers and footers. Quick questions:

  • Can i put header image as well?
  • for a pdf document, Can i have a document with image as well as table data in one pdf. Can it be achieved like: doc.addImage then doc.addPage and then finally doc.autotable?

ruhijain avatar Jun 22 '15 17:06 ruhijain

Sweet! Yes you can use the jspdf method doc.addImage in the drawHeader and drawFooter functions from this plugin. It shouldn't matter what order you call doc.autoTable and doc.addPage, however it probably makes sense to do it in the order the content flows in the document.

simonbengtsson avatar Jun 29 '15 23:06 simonbengtsson

I'll hope it worked out for you! If you have further questions or issues feel free to reopen this issue or create a new one.

simonbengtsson avatar Jul 10 '15 22:07 simonbengtsson

Does not work for me. It just added image to the doc on first page, It does not add the image as header for every page :(

ruhijain avatar Jul 15 '15 07:07 ruhijain

Strange! I added an image on each page in the the header and footer example now (demo) without any issues. Do you get any errors?

simonbengtsson avatar Jul 15 '15 19:07 simonbengtsson

Sorry that was a mistake from my end. I managed to fix it and now the pdf looks awesome. You are a saviour!! :)

ruhijain avatar Jul 16 '15 09:07 ruhijain

No worries!

simonbengtsson avatar Jul 16 '15 09:07 simonbengtsson

Hi..I am trying similar thing.I have created a pdf from HTML page using html2canvas. I am not able to provide bottom margin or footer for adImage function. The Image comes to the end of that pdf file. I saw this post but the demo links are not working. @simonbengtsson can you help me with this code below to add footer in all pages.

function generatePdf(divId) { var doc = new jsPDF('p', 'mm', [210,297]);
var elemen = document.getElementById(divId); html2canvas(elemen, { onrendered: function(canvas) {
var imgData = canvas.toDataURL( 'image/png');
var imgWidth = 160; var pageHeight = 297;
var imgHeight = canvas.height * imgWidth / canvas.width; var heightLeft = imgHeight; var position = 0; doc.addImage(imgData, 'PNG',25, 10, imgWidth, imgHeight,'alias','FAST'); heightLeft -= pageHeight; while (heightLeft >= 0) { position = heightLeft - imgHeight; position=position; doc.addPage(); doc.addImage(imgData, 'PNG', 25, position, imgWidth, imgHeight,'alias','FAST'); footer(); heightLeft -= pageHeight; }
doc.save('sample.pdf'); } }); }

bjraj avatar Dec 09 '15 05:12 bjraj

@simonbengtsson the demo link you have provided above does not seem to be working. Do you have a different link i can follow?

fireholster avatar May 21 '17 19:05 fireholster

Updated the link above

simonbengtsson avatar May 21 '17 19:05 simonbengtsson

Hi @simonbengtsson another question the example works ok if we have one autotable. In my case i have multiple autotables added to the pdf. how to add header footer in that scenario?r or am i missing something? for something like your multitable example

fireholster avatar May 21 '17 20:05 fireholster

You can use doc.autoTableSetDefaults like so:

doc.autoTableSetDefaults({
    addPageContent: function(data) {
	    doc.text("Header", 40, 30);
    }
});

This will add the header on each page no matter how many tables you add with doc.autoTable.

simonbengtsson avatar May 23 '17 06:05 simonbengtsson

The examples have moved again, they are at https://simonbengtsson.github.io/jsPDF-AutoTable/

PeterWooster avatar Apr 02 '20 15:04 PeterWooster

You can use doc.autoTableSetDefaults like so:

doc.autoTableSetDefaults({
    addPageContent: function(data) {
	    doc.text("Header", 40, 30);
    }
});

This will add the header on each page no matter how many tables you add with doc.autoTable.

for me it's not working, I want to add SVG into the header to left and right on both sides, but end up getting addSVG function is not defined, and also I have locally those files. can you please help me out? @simonbengtsson

MayurGVarma avatar Jul 13 '21 12:07 MayurGVarma