'fpdf2 error Invalid value for parameter "new_x"
I'm trying to follow this tutorial, specifically the Tuto 2, but I keep getting the following error and I don't know why. I copied and pasted the code from the tutorial. I'm on version 2.5.2.
ValueError: Invalid value for parameter "new_x" (0),must be instance of Enum XPos
from fpdf import FPDF
class PDF(FPDF):
def header(self):
# Rendering logo:
self.image("logo.png", 10, 8, 33)
# Setting font: helvetica bold 15
self.set_font("helvetica", "B", 15)
# Moving cursor to the right:
self.cell(80)
# Printing title:
self.cell(30, 10, "Title", 1, 0, "C")
# Performing a line break:
self.ln(20)
def footer(self):
# Position cursor at 1.5 cm from bottom:
self.set_y(-15)
# Setting font: helvetica italic 8
self.set_font("helvetica", "I", 8)
# Printing page number:
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C")
# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font("Times", size=12)
for i in range(1, 41):
pdf.cell(0, 10, f"Printing line number {i}", 0, 1)
pdf.output("tuto2.pdf")
Solution 1:[1]
Looks like the github project indicates that you need to be passing in an XPos and YPos object for new_x and new_y.
First, add this right below your import statement:
from fpdf.enums import XPos, YPos.
This will bring in the classes you need to correctly set the position of the page number.
Then update the footer function - instead of
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", 0, 0, "C"), use
self.cell(0, 10, f"Page {self.page_no()}/{{nb}}", XPos.RIGHT, new_y=YPos.NEXT, "C").
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
