brendanwd
brendanwd

Reputation: 109

FPDF __init__ combined with header and footer in Python

This will be my first post on Stackoverflow. I have a question concerning the FPDF package for python. Creating a class where the header and footer are defined, works fine. However when i try to initialize some variable via init, the header and footer will stop showing. I googled about everything and could not figure it out. Does anyone know how to solve this problem?

Code including the initialization

import fpdf as FPDF
import pandas as pd
import lipsum
from datetime import date
import locale

db=pd.DataFrame({'Fontsize':[5,6,7,8,9,10,11,12,14,17,20,25],'Baselineskip':[6,7,8,9.5,11,12,13.6,14.5,18,22,25,30]})


class PDF(FPDF.FPDF):

    def __init__(self,fontsize):
        FPDF.FPDF.__init__(self,orientation='P',unit='mm',format='A4')
        self.set_font('Arial','',fontsize)
        self.fontsize=fontsize
        self.baseskip=db[db['Fontsize']==self.fontsize].iloc[0,1]

    def header(self):
        # Logo
        # x=110.9, y=16.1
        self.image(name='logo.png', x=110.6, y=15.9, w=72.9)
        # Arial bold 15
        self.set_font('Arial', '', 15)
        #textcolor Arcadis orange
        self.set_text_color(r=228,g=97,b=15)
        # Title
        self.text(self.l_margin, 16.1+7.7, txt='bla')

    # 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(fontsize=9)
pdf.alias_nb_pages()
pdf.add_page()
pdf.c_margin=0
pdf.set_top_margin(37)
pdf.set_left_margin(25)
pdf.set_right_margin(25)
pdf.set_line_width(2/pdf.k)
pdf.set_draw_color(228,97,15)
pdf.line(pdf.l_margin,pdf.t_margin,pdf.fw-pdf.r_margin,pdf.t_margin)
pdf.set_font('Arial')
pdf.set_font_size(9)
pdf.ln(11/pdf.k)
for i in range(1, 20):
    pdf.cell(0,11/pdf.k, 'Printing line number ' + str(i), 0, 1)

pdf.multi_cell(0,11/pdf.k,lipsum.generate_words(100),align='J')
pdf.output('bla.pdf', 'F')

Code Without

class PDF(FPDF.FPDF):

    def header(self):
        # Logo
        # x=110.9, y=16.1
        self.image(name='logo.png', x=110.6, y=15.9, w=72.9)
        # Arial bold 15
        self.set_font('Arial', '', 15)
        #textcolor Arcadis orange
        self.set_text_color(r=228,g=97,b=15)
        # Title
        self.text(self.l_margin, 16.1+7.7, txt='BemalingsAdvies')

    # 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')

    def set_fontskip(self,fontsize):
        self.set_font_size(fontsize)
        self.fontsize=fontsize
        self.baseskip=db[db['Fontsize']==self.fontsize].iloc[0,1]

I thank whoever awnsers in advance.

Upvotes: 4

Views: 3258

Answers (1)

brendanwd
brendanwd

Reputation: 109

I already found my awnser: Here.

    def __init__(self):
        super(PDF,self).__init__()
        self.figcount=1

This snipped of code helped.

For those who are trying themselves, enjoy.

Upvotes: 5

Related Questions