0

I am creating a program that generates a translation sheet for a language. It uses the python FPDF module. I utilise pdf.multicell() to create a page with two columns for the English word, and then the foreign equivalent

I am using pdf.set_auto_page_break() to create a new page whenever I reach the page limit. However, I am experiencing some problems with this.

The new page is always created after one line has been written on a new page. This means that I have mismatched translations, and lots of empty whitepsace.

This is my code:

data = [['le bus', 'the bus'], ['le train', 'the train'], ['la gare', 'the train station']] etc

def write_to_pdf(data):
    pdf = FPDF()
    pdf.set_auto_page_break(True, 2)
    pdf.add_page()
    pdf.add_font('DejaVu', '', 'filepath/font', uni=True)
    pdf.set_font('DejaVu', '', 14)
    
    pdf.cell(200, 10, txt = title,ln = 1, align = 'C')
    pdf.cell(200, 10, txt = subtitle,ln = 2, align = 'C')
    pdf.set_font('DejaVu', '', 10)

    for section in data:
        try:
            top = pdf.y
    
            offset = pdf.x + 100

            pdf.multi_cell(100,10,section[0],0,'L')

            pdf.y = top

            pdf.x = offset 
           
            pdf.multi_cell(100,10,section[1],0,'R')

        except:
            pass

    pdf.output("Output.pdf") 

And this is the result

Page 1

Page 2Page 3

Thank you in advance for any help

frankfalse
  • 1,553
  • 1
  • 4
  • 17
Max Davies
  • 148
  • 10

1 Answers1

0

I think that the explanation of the problem is that before you save the y with the instruction: top=pdf.y and after you execute the instruction:

pdf.multi_cell(100,10,section[0],0,'L')

so when the bottom of the page is reached y value is high (near 300 mm) so the adding of a new multi_cell of 10 mm causes the creation of a new page.
The multi_cell which contains the sentence òu est-ce que tu va ? is placed at the top of the second page.
After this, the program executes the instruction pdf.y=top so the cursor is moved to the bottom of the second page where is inserted the new multi_cell with the english translation where are you going ?, but this is placed in the third page as you can see in your PDF file.

The solution is to add a if condition to test the value of y before to set its value. All changes are showed by the following code (which is your write_to_pdf function modified):

def write_to_pdf(data):
    pdf = FPDF()
    pdf.set_auto_page_break(True, 2)
    pdf.add_page()
    pdf.add_font('DejaVu', '', 'filepath/font', uni=True)
    pdf.set_font('DejaVu', '', 14)
    
    pdf.cell(200, 10, txt = title,ln = 1, align = 'C')
    pdf.cell(200, 10, txt = subtitle,ln = 2, align = 'C')
    pdf.set_font('DejaVu', '', 10)

    for section in data:
        try:
            top = pdf.y
    
            offset = pdf.x + 100

            pdf.multi_cell(100,10,section[0],0,'L')

            # ---> add this if <--- 
            if pdf.y > top:
                # we are always in the same page
                pdf.y = top
            else:
                # creation of a new page => y must be go back of the
                # height of a multi_cell (that is 10)
                pdf.y -= 10 

            pdf.x = offset 
           
            pdf.multi_cell(100,10,section[1],0,'R')

        except:
            pass

    pdf.output("Output.pdf") 

I have tried the code on my system and seems working correctly.


This is an other post with an analog problem but where it is used the method cell() instead of multi_cell().

frankfalse
  • 1,553
  • 1
  • 4
  • 17