[Django]-Django pisa – pagenumber just for two or more pages – show pagenumber of total pages

2πŸ‘

I had problem with adding a counter that would tell me what page number i was looking at out of the total amount of pages in the document. It got it to work when i switch the pisa import from:

import ho.pisa as pisa

to

import xhtml2pdf.pisa as pisa 

Install xhtml2pdf with pip

pip install xhtml2pdf

This is what my footer looks like now

<div id="footerContent">
    {%block page_foot%}
        Page <pdf:pagenumber> / <pdf:pagecount>
    {%endblock%}
</div>
πŸ‘€Erik Perttu

1πŸ‘

This is not possible in the current version of python-pisa. There is however some patches for it. If you use the version of pisa or xhtml2pdf from this github you will be able to get total number of pages.

https://github.com/chrisglass/xhtml2pdf

My template footer looks like this:

<div id="footerContent">
  "Page: <pdf:pagenumber> / <pdf:pagecount>
</div>

1πŸ‘

I think you can do it without using https://github.com/chrisglass/xhtml2pdf.
What i did is,
i use page break as below:

<h1 style="display:none">PAGECOUNT</h1>

and CSS for h1 is

h1 {
    page-break-before:always;
}

Now, you can get page count using

<your-html-string>.count('PAGECOUNT') + 1
πŸ‘€vicky

Leave a comment