我正在尝试将QrCode添加到我的PDF中,但出现IOError

时间:2019-10-16 16:39:20

标签: python reportlab

我正在尝试使用Reportlab将QrCode添加到我的PDF中,但是出现了IOerror。我之前已经成功添加了条形码,但是QrCode似乎有点棘手。

这是我的QrCode代码:

def get_QRcode(self,inventory):
    receipt = str(inventory.identifier)
    qr_code = qr.QrCodeWidget(receipt)
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[1]
    height = bounds[3] - bounds[1]
    d = Drawing(45,45, transform = [45./width,0,0,45./height,0,0])
    d.add(qr_code)
    filename = tempfile.mkstemp()[1] + '.png'
    d.save(filename)


    return filename

这是我的QrCode附录:

parts.append(Image(self.qrCode, width=.15* 0.35 *inch, height=.2*.25*inch))

尝试运行此命令后,我得到:

IOError at /inventories/download_tag/2525/
Cannot open resource "/tmp/tmpUf7ASf.png"
fileName='/tmp/tmpUf7ASf.png' identity=[ImageReader@0x7f6665819490 filename='/tmp/tmpUf7ASf.png']

感谢您的帮助!谢谢

编辑:这是一个附加我的条形码并且当前正在尝试附加QRCode的代码段

def get_pdf_flows(self):
        sku = self.sku_name 
        sku =  sku + " ({0})".format(self.num_cases) if self.num_cases else sku
        sku = Paragraph(sku, self.sku_style)
        grower_detail = self.get_grower_detail()

        col1, col2 = self.get_table_rows()

        parts = []
        parts.append(Spacer(1, 30))
        parts.append(sku)
        parts.append(Spacer(1, 5))
        parts.append(Image(self.barcode_file, width=.15* 0.35 *inch, height=.2*.25*inch))
        parts.append(self.qrCode)
        parts.append(grower_detail)
        parts.append(Spacer(1, 5))
        t1 = Table(col1,colWidths='*')
        t2 = Table(col2,colWidths='*')
        data = [[t1, t2]]
        if self.foodhub.company_name== "Harvest Santa Barbara":
            parts.append(Table(data, colWidths=(1.75*inch, 2.25*inch)))
        return parts

    def get_doc(self):
        buff = StringIO.StringIO()
        doc = SimpleDocTemplate(buff, pagesize=(4*inch, 3*inch), rightMargin=0, leftMargin=0, topMargin=1*mm, bottomMargin=0)
        return doc, buff 

1 个答案:

答案 0 :(得分:0)

我可以分享完成的方式,但是我的实现是直接将其绘制在画布上,然后将其添加到文档模板的脚注中。您的方法是先创建一个png,然后将其添加(我对一个图形对象进行了类似的操作,但是后来我使用了pdfrw,这是一个很好的插件,刚开始时我对文件夹的写访问有些麻烦)。看看下面是否适合您。

from reportlab.lib.units import mm, cm
from reportlab.lib.pagesizes import A4
from reportlab.graphics.barcode import qr, code128
from reportlab.graphics.shapes import Drawing
from reportlab.platypus import Table, TableStyle
from reportlab.platypus import SimpleDocTemplate
from reportlab.lib import colors

def func(canvas, doc):
    canvas.saveState()

    # draw a QR code
    receipt = str(f'hello')
    qr_code = qr.QrCodeWidget(f'{receipt}')
    bounds = qr_code.getBounds()
    width = bounds[2] - bounds[0]
    height = bounds[3] - bounds[1]
    d = Drawing(60, 60, transform=[60. / width, 0, 0, 60. / height, 0, 0])
    d.add(qr_code)
    d.drawOn(canvas, 1.77 * cm, 259.55 * mm)

    #Draw Bar code
    canvas.setFillColor(colors.black)
    barcode = code128.Code128(receipt, barWidth=0.17 * mm, barHeight=5 * mm, ratio=2.75, humanReadable=1)
    barcode.drawOn(canvas, 5.95 * cm, 259.55 * mm)
    canvas.restoreState()

    return func
#
def create_pdf():
    story = []
    data = [['Data1', 'Data2', 'Data3', 'Data4', 'Data5', 'Data6'],
            ['0.2', '-0.1', '0', '0', '-0.5', '0.6']]

    colwidths = (50)
    rowheights = (10)

    t = Table(data, colwidths, rowheights)

    GRID_STYLE = TableStyle(
        [('FONTSIZE', (0, 0), (-1, -1), 5),
         ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
         ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
         ('LEFTPADDING', (0, 0), (-1, -1), 0),
         ('RIGHTPADDING', (0, 0), (-1, -1), 0),
         ('TOPPADDING', (0, 0), (-1, -1), 0),
         ('BOTTOMPADDING', (0, 0), (-1, -1), 0),
         ('FONTNAME', (0, 0), (-1, -1), 'Helvetica'),
         ('SIZE', (0, 0), (-1, -1), 7),
         ('LEADING', (0, 0), (-1, -1), 8.2),
         ]
    )

    t.setStyle(GRID_STYLE)
    story.append(t)

    doc = SimpleDocTemplate('mydoc.pdf', pagesize=A4, topMargin=50)
    doc.build(story, onFirstPage=func)

    # ----------------------------------------------------------------------
if __name__ == "__main__":
    create_pdf()  # Printing the pdf