Double page numbers in XHTML2PDF/Pisa

I’ve been working on a django project recently that generates PDFs from html using the XHTML2PDF library (formerly Pisa). I have html successfully in a pdf in just a couple lines, so overall it’s fantastic, but there are some quirks to get around. I ran into one of these quirks today while trying to put page numbers in my footer template – when it rendered, every page was showing its page number twice (11, 22, 33). My code looked fine; no extra tags, everything was properly formatted, etc, but I could not stop it from happening. Here is my page/footer style and my extremely simple footer markup.

@page {
	size: {% if pagesize %}{{pagesize}}{% else %}letter{% endif %};
	margin: 1cm;
	@frame footer {
		-pdf-frame-content: footerContent;
		bottom: 1cm;
		margin-left: 1cm;
		margin-right: 1cm;
		margin-top: 0cm;
		height: 1.4cm;
	}
}

...

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

Finally I got smart and found this answer on the mailing list. Essentially its a bug in the xhtml2pdf parser – without a new line after the tag it freaks out and decides you meant it twice. Interesting bug, but not interesting enough for me to go look at their parser code to figure it out. :)

Anyway, this simple change fixed the problem and hopefully you won’t waste any time banging your head on the desk with page 11, 22, etc.

<div id='footerContent'>
 Page <pdf:pagenumber />
</div>

Leave a Comment