[reportlab-users] Rotated paragraph/document with ReportLab (vertical text)

Jesus Sanchez-Palencia jesus at eventick.com.br
Thu Oct 25 13:21:22 EDT 2012


Hi!

On Thu, Oct 25, 2012 at 1:54 PM, Robin Becker <robin at reportlab.com> wrote:

> I'm not sure exactly why you want to do this, but the code below is a start

> you will need to tweak the translate here to get exactly what you want as we

> will obviously be rotating about a place that's not exactly clear ie the

> center of the top line.


Imagine that I'm generating a PDF to be printed as a label.
The page has to be that size (80x250, for example), and the text has
to be vertical, from bottom to top.

I have tried to this horizontally, saving the PDF, and then rotating
it somehow but usually what the pdf libraries do is just add a \Rotate
-90 into the PDF. The printers can't understand that, this is why I
need to actually rotate the canvas.

Is there any documentation about what happens to the coordinates after
we canvas.rotate(90) everything? That would help to tweak the
translation, but in summary I want the same behavior that my code
would have if the document had a size of 250x80 and then the result
would be rotated 90 degrees counter clockwise.

I will test your snippet later, thanks.


>

> #########################

> from reportlab.platypus.doctemplate import SimpleDocTemplate

> from reportlab.lib.styles import getSampleStyleSheet

> from reportlab.lib.enums import TA_CENTER

> from reportlab.platypus.paragraph import Paragraph

> class RotatedPara(Paragraph):

> def draw(self):

> self.canv.saveState()

> self.canv.translate(0,0)

> self.canv.rotate(90)

> Paragraph.draw(self)

> self.canv.restoreState()

>

>

> style = getSampleStyleSheet()

> normal = style["Normal"]

> normal.alignment = TA_CENTER

> normal.fontName = "Helvetica"

> normal.fontSize = 15

>

> pdf = SimpleDocTemplate("rotatedpara.pdf", pagesize = (80, 250),

>

> rightMargin=10, leftMargin=10, topMargin=20,bottomMargin=20)

> story = []

> text = "I really need this to be wrapped and vertical!"

>

> para = RotatedPara(text, normal)

> story.append(para)

> pdf.build(story)

> #########################

>

>

> On 25/10/2012 14:16, Jesus Sanchez-Palencia wrote:

>>

>> Hi!

>>

>> I'm trying to get something like the following snippet to actually

>> draw this document rotated by 90 degrees. The pagesize is already the

>> way I want it in the end, but the text is still horizontal. How can I

>> rotate the text making it vertical?

>> What I want in the end is something similar to calling

>> canvas.rotate(90) and then canvas.drawString(), but I want to take

>> the advantage of word wrapping from Paragraph...

>

> .........

> --

> Robin Becker



More information about the reportlab-users mailing list