[Python] FPDF creates a pdf file with 1 extra page with every iteration

Discussão em 'Python' iniciado por Stack, Maio 15, 2025.

  1. Stack

    Stack Membro Participativo

    I've got a function from FPDF library to create pdf. Let's say, i have 4 orders and i need to create pdf for each of them, so i loop through them,

    orders = [10, 11, 12, 13]
    for o in orders:
    generate_pdf(o)


    Function generate_pdf:

    from fpdf import FPDF
    pdf = FPDF()
    def generate_pdf(DokId):
    pdf.add_page()
    header = "Order #{}".format(DokId)

    pdf.set_font('Arial', 'B', 20)
    w = pdf.get_string_width(header) + 6
    pdf.set_x((210 - w) / 2)
    pdf.cell(w, 9, header, 0, 0, 'C')
    pdf.line(20, 18, 210-20, 18)
    pdf.output('{}.pdf'.format(DokId), 'F')
    return None


    For first el in my list i've got file "10.pdf" with 1 page, for the next one - 2 pages, etc. How to avoid this case and create only one page for each order number in a loop?

    Thanks in advance

    Continue reading...

Compartilhe esta Página