至于你的问题建议你需要更大的灵活性,我认为最明智的做法是使用Flowable
的。条码通常不是一个,但我们可以很容易地make it one。通过这样做,我们可以让platypus决定您的布局中每个条形码有多少空间。
所以一步一个Barcode
Flowable
看起来像这样:
from reportlab.graphics import renderPDF
from reportlab.graphics.barcode.eanbc import Ean13BarcodeWidget
from reportlab.graphics.shapes import Drawing
from reportlab.platypus import Flowable
class BarCode(Flowable):
# Based on https://stackoverflow.com/questions/18569682/use-qrcodewidget-or-plotarea-with-platypus
def __init__(self, value="1234567890", ratio=0.5):
# init and store rendering value
Flowable.__init__(self)
self.value = value
self.ratio = ratio
def wrap(self, availWidth, availHeight):
# Make the barcode fill the width while maintaining the ratio
self.width = availWidth
self.height = self.ratio * availWidth
return self.width, self.height
def draw(self):
# Flowable canvas
bar_code = Ean13BarcodeWidget(value=self.value)
bounds = bar_code.getBounds()
bar_width = bounds[2] - bounds[0]
bar_height = bounds[3] - bounds[1]
w = float(self.width)
h = float(self.height)
d = Drawing(w, h, transform=[w/bar_width, 0, 0, h/bar_height, 0, 0])
d.add(bar_code)
renderPDF.draw(d, self.canv, 0, 0)
然后回答你的问题,到现在把一个页面上的多个条形码是用最简单的方式Table
像这样:
from reportlab.platypus import SimpleDocTemplate, Table
from reportlab.lib.pagesizes import A4
doc = SimpleDocTemplate("test.pdf", pagesize=A4)
table_data = [[BarCode(value='123'), BarCode(value='456')],
[BarCode(value='789'), BarCode(value='012')]]
barcode_table = Table(table_data)
parts = []
parts.append(barcode_table)
doc.build(parts)
,输出:
答案低于你的意思? – B8vrede
是的。谢谢@ B8vrede –