Search code examples
pythonsqlitefor-looppdffpdf

Python for loop for pdf output with FPDF not workin properly


I'm trying create a pdf output by fetching data from an sqlite database using the fpdf python library. I made a demo code which is suppose to output two pages but instead got four; with repitation of pages. Looks like my for loop did double the job. The codes:

import sqlite3
from datetime import datetime

conn = sqlite3.connect('delegates_cert.db')
cursor = conn.cursor()

cursor.execute('''SELECT marine_data.Cert_no, marine_data.Surname, marine_data.First_name, marine_data.Other_names, marine_data.Birthday, 
    marine_data.Course_end_date, marine_data.Expiry_date, setup.course, setup.course_description
    FROM marine_data
    INNER JOIN setup ON marine_data.Course=setup.course
    WHERE marine_data.Course_code = 'CM01'
             ''')
fetch = cursor.fetchall()
imagefiles = ["0.png","1.png"]



from fpdf import FPDF
pdf = FPDF('P', 'mm', 'A5')
pdf.add_page()
pdf.set_font('helvetica', '', 10)
pdf.set_auto_page_break(auto = True, margin = 10)
for image in imagefiles:
    for r in range(0, 2):
        pdf.set_font('helvetica', '', 10)
        pdf.cell(0, 63, " "*90+fetch[r][0].center(28, " "), ln = True )
        pdf.set_font('helvetica', 'B', 14)
        pdf.cell(0, 12, (fetch[r][1]+" "+fetch[r][2]+" "+fetch[r][3]).center(50, " "), ln = True )
        pdf.set_font('helvetica', 'B', 13)
        pdf.cell(0, 13, " "*17+datetime.strptime(fetch[r][4],"%Y-%m-%d").strftime("%d-%m-%Y").center(50, " "), ln = True )
        pdf.cell(0, 23, fetch[r][7], align = "C",  ln = True )
        pdf.set_font('helvetica', 'I', 9)
        pdf.multi_cell(0, 5, fetch[r][8], ln = True, align = "C" )
        pdf.set_font('helvetica', 'B', 9)
        pdf.cell(0, 4, fetch[r][5].center(20, " ")+" "*90+fetch[r][6].center(10, " "), align = "L", ln = True )
        pdf.image(image,117,74,24,24)

Can any one please help? this for loop is giving me headache. Thanks


Solution

  • the following if condition inside the for loop did the trick

    for r in range(0, 2):

    for image in imagefiles:
    
        if r == imagefiles.index(image):
            pdf.set_font('helvetica', '', 10)
            pdf.cell(0, 63, " "*90+fetch[r][0].center(28, " "), ln = True )
            pdf.set_font('helvetica', 'B', 14)
            pdf.cell(0, 12, (fetch[r][1]+" "+fetch[r][2]+" "+fetch[r][3]).center(50, " "), ln = True )
            pdf.set_font('helvetica', 'B', 13)
            pdf.cell(0, 13, " "*17+datetime.strptime(fetch[r][4],"%Y-%m-%d").strftime("%d-%m-%Y").center(50, " "), ln = True )
            pdf.cell(0, 23, fetch[r][7], align = "C",  ln = True )
            pdf.set_font('helvetica', 'I', 9)
            pdf.multi_cell(0, 5, fetch[r][8], ln = True, align = "C" )
            pdf.set_font('helvetica', 'B', 9)
            pdf.cell(0, 12, fetch[r][5].center(20, " ")+" "*90+fetch[r][6].center(10, " "), align = "L", ln = True )
            pdf.image(image,117,74,24,24)
        else:
            pass