将 QrCodeWidget(或 PlotArea)与鸭嘴兽一起使用 [英] Use QrCodeWidget (or PlotArea) with platypus
问题描述
我的 django 应用程序在女巫中使用多帧 reportlab pdf 报告,我想添加一些条形码/二维码.
My django app is using a multi-frames reportlab pdf report in witch I would like to add some barcodes/qr-codes.
我遇到的问题是,我添加到布局中的每个对象都必须是 Flowable.所以问题是将 PlotArea(QrCodeWidget 的母类)转换为 Flowable.
The problem I have is that every object I add to my layout have to be a Flowable. So the question would be as to cast a PlotArea (mother class of QrCodeWidget) as Flowable.
如果我们在这里有答案,如果我们将 QrCodeWidget 添加为
If we have an answer here the error message we can get if we add the QrCodeWidget as
AttributeError: QrCodeWidget instance has no attribute 'getKeepWithNext'
推荐答案
好吧,我自己做了 Flowable,它比我教的更简单.
Ok, I made my own Flowable, it was simpler than I taught.
就像使用此 API 在画布上进行操作一样简单.
It's as simple as doing it on a canva with this 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)
这篇关于将 QrCodeWidget(或 PlotArea)与鸭嘴兽一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!