对鸭嘴兽使用QrCodeWidget(或PlotArea)

2024-05-15 03:13:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我的django应用程序正在使用一个多框架reportlab pdf报告,我想在其中添加一些条形码/二维码。在

我的问题是,我添加到布局中的每个对象都必须是可流动的。 所以问题是将PlotArea(QrCodeWidget的母类)转换为可流动的。在

如果我们在这里有一个答案,如果我们添加QrCodeWidget作为

AttributeError: QrCodeWidget instance has no attribute 'getKeepWithNext'

Tags: 对象django答案框架应用程序pdf报告布局
2条回答

您应该从您的QrCodeWidget生成一个图像,并将其包含在Image流中。在

好吧,我自己做了流动的,比我教的简单多了。在

简单到用这个API在canva上做。在

from reportlab.platypus import Flowable
from reportlab.graphics.barcode import qr
from reportlab.graphics import renderPDF
from reportlab.graphics.shapes import Drawing

class QRFlowable(Flowable):
    # usage : 
    # story.append(QRFlowable("http://google.fr"))
    def __init__(self, qr_value):
        # init and store rendering value
        Flowable.__init__(self)
        self.qr_value = qr_value

    def wrap(self, availWidth, availHeight):
        # optionnal, here I ask for the biggest square available
        self.width = self.height = min(availWidth, availHeight)
        return self.width, self.height

    def draw(self):
        # here standard and documented QrCodeWidget usage on
        # Flowable canva
        qr_code = qr.QrCodeWidget(self.qr_value)
        bounds = qr_code.getBounds()
        qr_width = bounds[2] - bounds[0]
        qr_height = bounds[3] - bounds[1]
        w = float(self.width)
        d = Drawing(w, w, transform=[w/qr_width, 0, 0, w/qr_height, 0, 0])
        d.add(qr_code)
        renderPDF.draw(d, self.canv, 0, 0)

相关问题 更多 >

    热门问题