django pisa - pagenumber just for two or more pages - show pagenumber of total pages

2.4k Views Asked by At

Is there a posibility to show the pagenumber with django pisa just for two or more pages? The template for showing up pagenumber looks like this:

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

I think it's necessary to get the number of pages, before they are processed by pisa. Any option to use an if/else statement like this pseudo:

if pages > 1:
 ...
else:
 ...

If you want to display e.g. Page 1/5 (page one of five) it's also necessary to get the count of pages ?! No solution in pisa for that?

3

There are 3 best solutions below

0
On

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
1
On

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>
0
On

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>