Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I add page numbers to PDF document? #168

Open
mphemming opened this issue Nov 12, 2020 · 5 comments
Open

How can I add page numbers to PDF document? #168

mphemming opened this issue Nov 12, 2020 · 5 comments

Comments

@mphemming
Copy link

I would like to add page numbers at the bottom of every page (except the first one which is a cover).

I was wondering if it's possible to do this using pyfpdf?

@cheikhnadiouf
Copy link

cheikhnadiouf commented Nov 22, 2020

Override the add_page() method and the footer() method then add a condition parameter (isCover ?) for footer page as example below

from FPDF import *

class MyFPDFClass(FPDF):
	def __init__(this, orientation='P',unit='mm',format='A4'):
		self.isCover = False
        # Override add_page methode
	def add_page(this,  same= True, orientation='', isCover= False):
		FPDF.add_page(self, same= same, orientation=orientation)

      # Override footer method
	def footer(self):
        	# Page number with condition isCover
        	self.set_y(-15) # Position at 1.5 cm from bottom
        	if self.isCover == False:   
			self.cell(0, 10, 'Page  ' + str(self.page_no) + '  |  {nb}', 0, 0, 'C') 

# Then call instruction with this instance line of your class  : 
# myfpdfclass.add_page(isCover = False) 
# or
# myfpdfclass.add_page(isCover = True)

@mphemming
Copy link
Author

Thanks! I will try this

@Lucas-C
Copy link

Lucas-C commented Jan 9, 2021

FYI, this recipe is documented here in fpdf2 documentation: https://alexanderankin.github.io/pyfpdf/reference/footer.html

@joestar-hub
Copy link

is there no way to use function instead of class to handle it

@Lucas-C
Copy link

Lucas-C commented Nov 11, 2022

With fpdf2, you can use the footer() method and the {{nb}} placeholder:
https://pyfpdf.github.io/fpdf2/Tutorial.html#tuto-2-header-footer-page-break-and-image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants