Search code examples
pythonpdfreportlab

Unable to write image in PDF File


def logo_para(self):
    exp = Paragraph(
        '<b>Express</b>', self.styles['CenterHeading'])
    csheet = Paragraph(
        '<b>PDF SHEET</b>', self.styles['CenterHeading'])

    img_location = "https://www.google.co.in/logos/doodles/2016/icc-australia-v-bangladesh-5759441086447616-res.png"

    img_data = '''
       <para><img src="%s" width="300" height="90"/><br/>
       </para>''' % img_location
    img = Paragraph(img_data, self.styles['CenterHeading'])
    data = [[exp], [csheet], [''], [img] ]
    main_header_table = Table([['', img, '']], colWidths=(100, 300, 100))
    main_header_table.setStyle(TableStyle([
        ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
        ('BOX', (0, 0), (-1, -1), 0.25, colors.black)
    ]))
    self.elements.append(main_header_table)

When ever I am calling

docket.logo_para()

I am getting error cannot concatenate 'str' and 'int' objects at self.doc.build(self.elements)

When the line is commenteddocket.logo_para(), the code works superbly.

I am trying to add an image on the PDF File using SimpleDocTemplate

EDIT 1

creating new pdf

class PDFDocketGenerator(object):
def __init__(self, file_name):
    self.filename = file_name
    self.filepath = STATIC_URL + 'uploads/billing/' + file_name
    self.path_to_save = FILE_UPLOAD_TEMP_DIR + '/billing/' + file_name
    # define the pdf object
    self.doc =  SimpleDocTemplate(
        self.path_to_save, pagesize=landscape(A4), topMargin=50, bottomMargin=30,
        leftMargin=60, rightMargin=60)
    self.elements = []

writing to pdf

 def write_pdf(self):

        self.doc.build(self.elements)

Solution

  • Is it possible that some values in self.elements are integers? I would suggest to try this in this case :

    def write_pdf(self):
    
        self.doc.build([str(e) for e in self.elements])