0

I am new to classes in Python so please help me out. I am generating reports in pdf using fpdf package.

I am using sample code from the following to generate PDF page with footer.

https://pyfpdf.readthedocs.io/en/latest/Tutorial/index.html#header-footer-page-break-and-image

from fpdf import FPDF

class PDF(FPDF):
    def header(self):
        # Logo
        self.image('logo_pb.png', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, 'Title', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')

# Instantiation of inherited class
pdf = PDF()
pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font('Times', '', 12)
for i in range(1, 5):
    pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1)
pdf.output('tuto2.pdf', 'F')

This generates 5 pages. I want to add a list element in the footer.

list=['A','B','C','D','E']
for i in range(1, 5):
    pdf.cell(0, 10, 'Printing line number ' + str(i)+list[i], 0, 1)
pdf.output('tuto2.pdf', 'F')

How do I pass this list to the Class? This list is generated in the derived class from the Class above.

Abhishek Kulkarni
  • 654
  • 1
  • 8
  • 20

2 Answers2

0

I did not find the package to test but I added an init to generate an alphabet list that way you can add it while generating the footer based on the

"""
https://stackoverflow.com/questions/61473844/access-a-local-variable-via-class-method-in-python
https://stackoverflow.com/questions/453576/is-there-a-fast-way-to-generate-a-dict-of-the-alphabet-in-python
"""
from fpdf import FPDF
import string

class PDF(FPDF):
    def ___init___(self):
        letters = string.ascii_lowercase
        alphabet = d = dict(zip(range(1,27), letters))

    def header(self):
        # Logo
        self.image('logo_pb.png', 10, 8, 33)
        # Arial bold 15
        self.set_font('Arial', 'B', 15)
        # Move to the right
        self.cell(80)
        # Title
        self.cell(30, 10, 'Title', 1, 0, 'C')
        # Line break
        self.ln(20)

    # Page footer
    def footer(self):
        # Position at 1.5 cm from bottom
        self.set_y(-15)
        # Arial italic 8
        self.set_font('Arial', 'I', 8)
        # Page number
        self.cell(0, 10, 'Page ' + str(self.page_no()) + str(self.alphabet.get(self.page_no()) + '/{nb}', 0, 0, 'C')

if __name__ == '__main__':
    # Instantiation of inherited class
    pdf = PDF()
    pdf.alias_nb_pages()
    pdf.add_page()
    pdf.set_font('Times', '', 12)
    for i in range(1, 5):
        pdf.cell(0, 10, 'Printing line number ' + str(i), 0, 1)
    pdf.output('tuto2.pdf', 'F')
Grom
  • 50
  • 4
0

Luckily, figured out!

class PDF1(FPDF,HTMLMixin):
    def __init__(self, list):
        super(PDF1, self).__init__()
        self.proper_name = list
Abhishek Kulkarni
  • 654
  • 1
  • 8
  • 20