0

I am constructing a class to produce automated pdf reports. (Using FPDF) In my class I have so far defined two functions. One that renders the first page(front) and one that renders a second page(page_two). These two functions produces each what I want. But when I try to combine the two in the function join the second page gets all white.

I don't understand why. Any help would be much appreciated.

from fpdf import FPDF

class MY_PDF(FPDF):
    
    def _init_(self, **kwargs):
        super(MY_PDF, self)-__init__(**kwargs)
        
        pdf.add_font('Arial', '', r"c:\WINDOWS\Fonts\ARIAL.ttf", uni=True)
        pdf.add_font('Arial_Bold', '', r"c:\WINDOWS\Fonts\ARIALBD.ttf", uni=True)
        pdf.add_font('Georgia', '', r"c:\WINDOWS\Fonts\GEORGIA.ttf", uni=True)
        pdf.add_font('Calibri', '', r"c:\WINDOWS\Fonts\CALIBRI.ttf", uni=True)
        
        # A4.
        WIDTH = 210
        HEIGHT = 297
    
    
    def front(self, title, second_title):

        self.set_fill_color(0,0,90) 
        self.rect(0.5, 50, WIDTH-1, 75, 'F')
        self.set_fill_color(149,194,61) 
        pdf.rect(14, 12, 5, 280, 'F')

        self.set_font('Arial', 'B', 38)
        self.set_text_color(255, 255, 255)
        self.cell(15)
        self.cell(150, 135, txt=f'{title}')

        self.ln(10)
        self.set_font('Arial', '', 28)
        self.set_text_color(255, 255, 255)
        self.cell(15)
        self.cell(150, 150, txt=f'{second_title}')

        #self.image('logga.png', 25, 12, 50)
        self.ln()   
        
    def page_two(self, name, date, nr):
        
        self.set_font('Arial', '', 9)
        self.ln(250)
        self.cell(20)
        self.cell(0, 0, txt="©", align='L', ln=0)
        self.cell(-160)
        self.cell(0, 0, txt='Company name', align='L', ln=2)
        self.ln(4)
        self.cell(30)
        self.cell(0, 0, txt=f'Author: {name}', align='L',  ln=2)
        self.ln(4)
        self.cell(30)
        self.cell(0, 0, txt=f'Date: {date}', align='L', ln=2)
        self.ln(4)
        self.cell(30)
        self.cell(0, 0, txt=f'Number: {nr}', align='L',  ln=2)
        self.ln()   
        
    
    def join(self, title, second_title, name, date, nr):
        self.add_page()
        self.front(title, second_title)
        self.add_page()
        self.page_two(name, date, nr)
        
pdf= MY_PDF()

pdf.join('TITLE!', 'some other title', 'Name Name', '2021-11-30', '21262')
pdf.output('output.pdf')       
        

Henri
  • 1,077
  • 10
  • 24
  • You need to call `self.add_page()` again after `self.front(...)` if you expect the rest to be on a new page. – Tim Roberts Dec 01 '21 at 06:17
  • That makes sense, but I still get the same result. A blank second page. – Henri Dec 01 '21 at 07:33
  • 1
    You may need to do `set_xy` to set the Y coordinate back to the top of the page. – Tim Roberts Dec 01 '21 at 08:04
  • Interesting enough, if I change the order both works. Self.page_two() first then self.front() in join. But I don't understand why :) Set_xy() didn't change anything. – Henri Dec 01 '21 at 08:15
  • 1
    Solved it. The text was white. Just needed to add set_text_color(0,0,0) to page_two() – Henri Dec 01 '21 at 09:36

0 Answers0