Search code examples
python-3.xpdf-generationfpdf

fpdf.cell height parameter generates new page instead of line break


i ran into an issue with fpdf python library for pdf generation. Turns out I need my pdf with dimensions [2 inches width x 1 inches height] because i need it to print a small label in zebra printer.

The following operations were executed:

  • I inserted a cell in coordinates [x=0.2,y=0.1, width=0, height=0.1] (Success)
  • Executed a break line (Success)
  • Inserted another cell in coordinates [x=0.2,y=coordinate which break line places, width=0, height=0.1] (FAILURE). What i was expecting is the cell to be below the first cell already placed. But instead when I used a height of 0.1 for the cell, The pdf adds another page and then add the cell in second page. Double_Page_Instead_Same_Page_Image When i modify height=0 cell is placed below first cell without adding a new page like this Cell_In_Same_Page_Image

I need the cells to be the height i want (in this case 0.1), because the data i am placing comes from a database and also there are at least 3 rows which i need to insert one below other.

NOTES: When increasing pdf height [2 inches width x 2 inches height] instead of [2 inches width x 1 inches height], it behaves as I expected, but i need the [2 inches width x 1 inches height] dimensions.

  • Why could this behaviour be happening?
  • How could i resolve this issue?

I am using:

  • Windows 10 Professional.
  • Python 3.8.8.
  • fpdf2 2.4.2.
  • Visual Studio Code.

Solution

  • Thanks to Olivier's comment i could resolve the issue by disabling automatic page break. In my code it looks like this:

    page_size = (2, 1)
    #Set page size to 2inch-width x 1inch-height
    pdf = FPDF("P", "in", page_size)
    
    pdf.add_page()
    pdf.set_auto_page_break(auto=False)