[ale] building pdf from python with reportlab

Michael Solberg msolberg at redhat.com
Wed Aug 27 15:06:11 EDT 2014


On 08/26/2014 10:37 AM, jkinney wrote:
> -----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)

I suspect your issue isn't actually with reportlab.  What is the value
of "text"?  Consider:

>>> text = "likethis"
>>> for t in text:
...     t
...
'l'
'i'
'k'
'e'
't'
'h'
'i'
's'

Versus

>>> text = ['like', 'this']
>>> for l in text:
...     l
...
'like'
'this'

Michael.


More information about the Ale mailing list