Generate report with header and footer and image in body
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?
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.
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?
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.
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.
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 :(
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?
Sorry that was a mistake from my end. I managed to fix it and now the pdf looks awesome. You are a saviour!! :)
No worries!
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');
}
});
}
@simonbengtsson the demo link you have provided above does not seem to be working. Do you have a different link i can follow?
Updated the link above
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
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.
The examples have moved again, they are at https://simonbengtsson.github.io/jsPDF-AutoTable/
You can use
doc.autoTableSetDefaultslike 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