1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
from fpdf import FPDF
from pystrich.datamatrix import DataMatrixEncoder, DataMatrixRenderer
class PDF(FPDF):
def datamatrix(self, text, w, h=None, x=None, y=None, cellsize=5):
if x is None:
x = self.x
if y is None:
y = self.y
if h is None:
h = w
encoder = DataMatrixEncoder(text)
encoder.width = w
encoder.height = h
renderer = DataMatrixRenderer(encoder.matrix, encoder.regions)
img = renderer.get_pilimage(cellsize)
self.image(img, x, y, w, h)
# Usage example:
pdf = PDF()
pdf.add_page()
pdf.set_font("Helvetica", size=24)
pdf.datamatrix("Hello world!", w=100)
pdf.output("datamatrix_from_method.pdf")
|