无法在PDF文件中写入图像

时间:2016-03-21 12:18:40

标签: python pdf reportlab

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)

我什么时候打电话

docket.logo_para()

我在cannot concatenate 'str' and 'int' objects

收到错误self.doc.build(self.elements)

当评论行docket.logo_para()时,代码效果非常好。

我正在尝试使用SimpleDocTemplate

在PDF文件上添加图片

编辑1

创建新的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 = []

写给pdf

 def write_pdf(self):

        self.doc.build(self.elements)

1 个答案:

答案 0 :(得分:0)

self.elements中的某些值是否可能是整数?我建议在这种情况下尝试这个:

def write_pdf(self):

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