【问题标题】:Use QrCodeWidget (or PlotArea) with platypus将 QrCodeWidget(或 PlotArea)与鸭嘴兽一起使用
【发布时间】:2013-09-02 09:22:04
【问题描述】:

我的 django 应用正在使用多帧 reportlab pdf 报告,我想添加一些条形码/二维码。

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

如果我们在这里有答案,如果我们将 QrCodeWidget 添加为,我们可以得到错误消息

AttributeError: QrCodeWidget instance has no attribute 'getKeepWithNext'

【问题讨论】:

    标签: python qr-code reportlab platypus


    【解决方案1】:

    好的,我制作了自己的 Flowable,它比我教的要简单。

    使用这个 API 就像在画布上做一样简单。

    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)
    

    【讨论】:

    • 完美运行。谢谢
    【解决方案2】:

    您应该从您的QrCodeWidget 生成图像并将其包含在Image flowable 中。

    【讨论】:

    • 我想过,但我不知道该怎么做,我宁愿避免将其写入服务器硬盘。
    • 您不必将其写出:您可以将其存储在内存中的类似文件的对象中。不幸的是,除了文本、图像和其他 PDF 之外,没有太多其他方法可以将数据放入 PDF 中。
    • 确实如此,但是作为普通的 pdf 渲染器处理这些 PlotArea,我希望有一种“简单的方法”将它提供给鸭嘴兽。
    • 因为不是我很快需要的东西,所以直到今天我才做任何事情,希望能像我最终做出的那样得到一个答案。我看到你在关于堆栈溢出的报告实验室问题上做出了很多贡献。感谢其他答案^^。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-12
    • 1970-01-01
    • 2012-01-01
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多