[ale] building pdf from python with reportlab

Jim Kinney jim.kinney at gmail.com
Wed Aug 27 16:13:03 EDT 2014


Good question. The text was read in from a file and was several lines long.
I suspect that the process was failing to parse correctly and was seeing
each character as a single line but I never found what it was splitting on
or how to adjust it. I found a solution by using a multi-line version of
the textobject.textLines(text, trim=0) [the trim=0 keeps spacing around the
text block intact for asciiart] and placing the entire string at once
instead of iterating over each line. Used the same text variable as earlier
and it all did as expected.

To see it all in action, sign up for Phreaknic in a few weeks and the PDF
you get in email is the output :-)


On Wed, Aug 27, 2014 at 3:06 PM, Michael Solberg <msolberg at redhat.com>
wrote:

> 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.
> _______________________________________________
> Ale mailing list
> Ale at ale.org
> http://mail.ale.org/mailman/listinfo/ale
> See JOBS, ANNOUNCE and SCHOOLS lists at
> http://mail.ale.org/mailman/listinfo
>



-- 
-- 
James P. Kinney III

Every time you stop a school, you will have to build a jail. What you gain
at one end you lose at the other. It's like feeding a dog on his own tail.
It won't fatten the dog.
- Speech 11/23/1900 Mark Twain


*http://heretothereideas.blogspot.com/
<http://heretothereideas.blogspot.com/>*
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.ale.org/pipermail/ale/attachments/20140827/7834bda6/attachment.html>


More information about the Ale mailing list