question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add doc.putTotalPages function when printing PDF

See original GitHub issue

Is your feature request related to a problem? Please describe. I need a way to add “Page 1 of n” footer when using the PDF download option. This should be possible via AutoTable and jsPDF but it needs the following lines added after doc.autoTable(…) has been called.

Describe the solution you’d like Add the following code to https://github.com/olifolkerd/tabulator/blob/9e32aa87a9169503dfb6069a8eb60ec4e9d602d8/src/js/modules/download.js#L624

if (options.totalPagesExp) {
	if (typeof doc.putTotalPages === 'function') {
		doc.putTotalPages(options.totalPagesExp);
	}
}

totalPagesExp can then be passed as an option to the download function:

const totalPagesExp = "{total_pages_count_string}";
table.download( "pdf", "data.pdf", {
	orientation: "portrait", //set page orientation to portrait
	title: "PDF Page Numbers Test",
	totalPagesExp: totalPagesExp,
	autoTable: ( doc ) => {

		doc.autoTableSetDefaults( {
			addPageContent: ( data ) => {
				let footerStr = "Page " + doc.internal.getNumberOfPages();
				if ( typeof doc.putTotalPages === 'function' ) {
					footerStr = footerStr + " of " + totalPagesExp;
				}
				doc.setFontSize( 10 );
				doc.text( footerStr, data.settings.margin.left, doc.internal.pageSize.height - 10 );
			}
		} );
		
		//return the autoTable config options object
		return {
			columnStyles: columnStyles,
		};
	},
} );

Describe alternatives you’ve considered Doesn’t seem to be any way to workaround this at present.

Additional context https://github.com/simonbengtsson/jsPDF-AutoTable/issues/144#issuecomment-363945044

Samples Sample PDF Sample index.html test file

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
darshanrampatelcommented, Aug 2, 2019

@olifolkerd Thanks for doing this change! Just got around to upgrading to 4.3.0 and it works fine:

          const totalPagesExp = '{total_pages_count_string}';          
          this.myTable.download( 'pdf', 'fileName.pdf', {
            documentProcessing: ( doc ) => {
              if ( typeof doc.putTotalPages === 'function' ) {
                doc.putTotalPages( totalPagesExp );
              }
            },
            autoTable: ( doc ) => {
              doc.autoTableSetDefaults( {
                didDrawPage: ( data ) => {
                  let footerStr = `Page ${ doc.internal.getNumberOfPages() }`;
                  if ( typeof doc.putTotalPages === 'function' ) {
                    footerStr = `${ footerStr } of ${ totalPagesExp }`;
                  }
                  doc.setFontSize( 10 );
                  doc.text( footerStr, data.settings.margin.left, doc.internal.pageSize.height - 10 );
                }
              } );

              return {                
                theme: 'grid',                
              };
            },
          } );
0reactions
olifolkerdcommented, Jul 21, 2019

Hey @darshanrampatel

i have added a new documentProcessing option to the download options object, you can pass a callback to this that gives you access to the document object to call any functions you like on:

table.download("pdf", "test.pdf", {
	documentProcessing:function(doc){
		doc.text("SOME TEXT", 200, 30);
	}
});

It is currently on the 4.3 branch that will be released later today

Cheers

Oli 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

putTotalPages not working in jspdf-autotable header
I am trying to add 'Page number x of y' to a PDF generated using jspdf and jspdf-autotable. I have used the code...
Read more >
Creating customisable & beautiful PDFs using jsPDF API ...
Hi All,. I recently came across a requirement wherein I had to generate statements of account in PDF format. In this article, you...
Read more >
Advanced PDF print settings - Acrobat Pro
Advanced PDF print settings in Adobe Acrobat Pro for PostScript and ... Specifies how fonts and resources in the document are sent to...
Read more >
simonbengtsson/jsPDF-AutoTable - Gitter
Hello all, hopefully, someone can help. I'm using autotable to generate a pdf with a table that has styling applied to it through...
Read more >
Convert HTML /CSS Content to a Sleek Multiple Page PDF ...
jspdf puttotalpages: Convert HTML /CSS Content to a Sleek Multiple Page PDF File . ... jspdf page size a4: How do I add...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found