[ale] building pdf from python with reportlab

jkinney jkinney at jimkinney.us
Tue Aug 26 10:37:22 EDT 2014


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I'm trying to generate a 1-page pdf that has some text and a single
image (a qrcode) and I'm hitting issues. The docs for reportlab are a
better than many but still missing key details. The following will
generate the page, the text starts at the correct location, BUT the
text runs vertical down the page. I have found nothing that indicates
how to do that or fix it. By vertical down the page I mean:
l
i
k
e

t
h
i
s

grr! That _really_ messes up ascii art!

# generate the pdf file from the template, ascii art and the qrcode.png
files
# requires python-reportlab
def gen_pdf(text, asciiblock):

        from reportlab.pdfgen import canvas
        from reportlab.lib.units import inch
        def makeqrpage(canvas):

                textobject = canvas.beginText()
                textobject.setTextOrigin(inch, 9.5*inch)
                textobject.setFont("Helvetica-Bold", 14)
                for line in text:
                        textobject.textLine(line)
                canvas.drawText(textobject)
                textobject.setTextOrigin(1.5*inch, 6.5*inch)
                textobject.setFillGray(0.4)
                for line in asciiblock:
                        textobject.textLine(line)
                canvas.drawText(textobject)
                canvas.drawImage('qrcode.png', 2.5*inch, 1.5*inch,
4.5*inch, 3.5*inch)
        canvas = canvas.Canvas('qrcode.pdf')
        makeqrpage(canvas)
        canvas.showPage()
        canvas.save()

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1

iQIcBAEBAgAGBQJT/JuiAAoJEIm+DWlR6PEq+QgQAJbVHnWuU6SMIZqa4GW57ChB
+sJGxAkf0dX7AcB14+G/ipoZCfwkQrriMEAHqO9deHyZQvPnwXeN1rYGRMC0uKMn
JiTALxhdCZaj7mX+lOEy8iSsz/kpJ143AMUhY7LkEHbu4q1+kZCzdPuMgrTVUOdP
3VaT64OYVmAZ9jxSyelaMy+f8tx0QHroEnKowe4KhMhl7JbIMrgR9/6jBSPnF+0L
UoX7pWkqvI9lCFKdkoYAKXpDhoJFnj3AlcT14UTUTw2Kyp7ejrPG+t3b5pXvd6Wt
N4c/6NitssrYogqPHp393Xw0FJF7UiooFFaQLacy+zZzqoV4i9blHoyhqwQDsrHj
DwlskUcyaj5fP161GbDRwHp+ekLDHyyk2qH8jz1naj1qWEpLuC2l3fN56t1KCRAT
87CiA1M4XvE6dFl1MpaZKxlUWMrrImd83UKgjXPrd57wUkS6DSPj522PWc91DrBX
iBqx0iGGV+gV9jg1rWNgbfRmEJKjjheGDC0v7/J8Xv2YhfLyrTLWBvQAexT2VC39
hqyT4Cpw5tjDhrCEmIuVK29S/MHTkvEGOLniJaur48Gag1I2i6TyFooM+sjtxq4x
5/s24l+ienSxvwIxVgHOjIWcsLf9WCRQblpQHIs913xNY+Eqf0jIpb9rvGON2jNn
HPcjnzlwRPq9yWFdEeRY
=4oOE
-----END PGP SIGNATURE-----


More information about the Ale mailing list