我正在尝试向我的 PDF 添加 QrCode,但出现 IOError

I am trying to add a QrCode to my PDF but I am getting IOError

我正在尝试使用 Reportlab 将 QrCode 添加到我的 PDF,但出现 IOerror。我以前也成功贴过条形码,但二维码好像有点棘手。

这是我的二维码:

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

这是我添加的二维码:

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 

我可以分享我是如何做到的,但我的实现是直接在 canvas 上绘制它,然后我将它添加到文档模板的脚注中。你的方法是创建一个 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