Reportlab : How to switch between portrait and landscape?

python reportlab simpledoctemplate landscape
reportlab drawimage
reportlab documentation
reportlab image
reportlab paragraph style
reportlab getsamplestylesheet
python reportlab example
reportlab spacer

I am using reportlab to generate a pdf report automatically from dynamic data. As the content sometimes is too large to be displayed in portrait, I am trying to switch to landscape for large content.

Here is how my report generation works :

Main function :

doc = DocTemplate(...)           //Doctemplate is a customed BaseDocTemplate class
array = []
some_data= "Here is some data displayed in portrait" 

array.append(Paragraph(some_data))

large_data = "this data is too large to be displayed in portrait"
array.append(Paragraph(large_data))

... // Some more data is added after this

doc.build(array, canvasmaker=NumberedCanvas)

What I am looking for is a way to be able to switch from portrait to landscape at each step, as I don't know the number of pages that will be needed to display it. I am still new to reportlab and even a bit with python, so I do not see how I can use the solutions provided by reportlab (PageTemplates, flowables) properly as I am building the whole document at the end.

Here are my other useful classes for this case :

class DocTemplate(BaseDocTemplate, ):
def __init__(self, filename, **kw):
    apply(BaseDocTemplate.__init__, (self, filename), kw)
    f = Frame(2.6*cm, 2.8*cm, 16*cm, 22.7*cm, id='f')
    pt = PageTemplate('RectPage', [f], onPage=beforeDrawPage, onPageEnd=afterDrawPage)
    //beforeDrawPage and afterDrawPage fill the headers of the page (that also need to be in landscape)
    self.addPageTemplates(pt)

I think I shall add another page template or frame, but I don't see how i can switch from one to the other during the data appending phase.

class NumberedCanvas(canvas.Canvas):
def __init__(self, *args, **kwargs):
    canvas.Canvas.__init__(self, *args, **kwargs)

    self._saved_page_states = []

def showPage(self):
    self._saved_page_states.append(dict(self.__dict__))
    self._startPage()

def save(self):
    """add page info to each page (page x of y)"""
    num_pages = len(self._saved_page_states)
    for state in self._saved_page_states:
        self.__dict__.update(state)
        self.draw_page_number(num_pages)
        canvas.Canvas.showPage(self)
    self.setTitle("Title")
    canvas.Canvas.save(self)
    self._doc.SaveToFile(self._filename, self)

def draw_page_number(self, page_count):
    self.setFont("Helvetica", 11)
    self.drawRightString(18.5*cm, 26.8*cm,
        "PAGE %d / %d" % (self._pageNumber, page_count))

I hope I did'nt forgot anything to be clear.

Many thanks in advance.


Use the landscape and portrait functions that are already in the pagesizes module.

from reportlab.lib.pagesizes import letter, landscape
c = canvas.Canvas(file, pagesize=landscape(letter))

How do you change from Landscape to Portrait , Here is a hack, based on the answer I found here. from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY from reportlab.lib import​  I think this is because the values returned by the e.g. doc.width methods are not changing as I change the pagesize in the pagetemplate. Can someone put me on the right tracks here? landscape reportlab portrait platypus


I finally figured out the best way to do it by myself :

I added a new PageTemplate in my DocTemplate with landscape settings, and then simply used NextPageTemplate from the reportlab.platypus package :

array.append(NextPageTemplate('landscape'))

To get back in portrait, i use :

array.append(NextPageTemplate('portrait'))

This allows a pretty nice flexibility.

Reportlab: How to Create Landscape Pages, Learn how to use Python and Reportlab to create PDFs in landscape mode as well as rotate pages back to portrait. from reportlab.lib.pagesizes import landscape, letter On the other hand, if we do turn on rotation, then we create a file object using Python's StringIO library so that we can manipulate the  For all of those who may use Reportlab, I have a newbie question (should be simple to answer) that has been bugging me for the past day or so and after pouring over all online posts related to reportlab still can't be figured out. All I am trying to do is switch from landscape to portrait orientation in the same document.


This is how I switch between portrait and landscape modes, but I determine which orientation beforehand:

from reportlab.lib.pagesizes import letter, A4

lWidth, lHeight = letter

if orientation == 'landscape':
    canvas.setPageSize((lHeight, lWidth))
else:
    canvas.setPageSize((lWidth, lHeight))

Changing from landscape to portrait orientation in ReportLab., posts related to reportlab still can't be figured out. All I am trying to do is switch from landscape to portrait orientation in the same document. The other way to set landscape is just set the page size explicitly: from reportlab.lib.pagesizes import landscape from reportlab.pdfgen import canvas from reportlab.lib.units import inch self.c = canvas self.c.setPageSize( (11*inch, 8.5*inch) ) You could make this more generic by doing something like this though:


doc=SimpleDocTemplate(..., pagesize=(A4[1],A4[0]))

reportlab.lib.pagesizes.landscape Python Example, Python reportlab.lib.pagesizes.landscape() Examples 15 else: raise Exception(​'Must choose from the following orientations: portrait, landscape') input_words  ReportLab and pdfrw: Importing Scanned PDF. it look like if I pass my watermarked page through your reportlab code?" to switch between portrait and landscape? 3.


Also if anyone was looking for another way to change between landscape and portrait, you can also pass in the rotation as a keyword argument to BaseDocTemplate.

self._document = BaseDocTemplate(self._filename,
                                 pageSize=self._page_size,
                                 topMargin=1.5*cm,
                                 bottomMargin=1.5*cm,
                                 leftMargin=1*cm,
                                 rightMargin=1*cm,
                                 rotation=90,
                                 showBoundary=False
                                 )

How do you change from Landscape to Portrait Layout in , generating PDFs, LaTeX, Groff, HTMLDOC and ReportLab, are evaluated under a set 5.14 Mixed page orientation (portrait/landscape) in same document 33 Biotage wants to move away from their current solution into a dynamic and. Select all the text, images, and pages that you want to switch to the new orientation. On the ribbon, select Layout. In the Page Setup group, select the Page Setup dialog box launcher (the small arrow in the lower-right corner of the group). In the Page Setup dialog box, select the Margins tab.


[PDF] Evaluating and comparing tools for generating PDF files, RML2PDF can also be called directly from your own Python program using the You can control the automatic switch of frames by using the <setNextFrame/> tag. (0 | portrait | 90 | landscape | 180 | 270 | auto) auto. In a Microsoft Word document, place your cursor at the start of the page that you want to change to landscape. Select Layout > Breaks > Next Page to place a section break where you had your cursor. Make sure your cursor is still at the start of that page, and go to the Orientation option.


[PDF] RML User Guide, posts related to reportlab still can't be figured out. All I am trying to do is switch from landscape to portrait orientation in the same document. The following are code examples for showing how to use reportlab.lib.pagesizes.landscape().They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.


[reportlab-users] Changing from Landscape to Portrait orientation in , Your cursor should now be at the top of the page in your new section—the page you’re changing from portrait to landscape. Go back to the “Layout” tab. This time, click the “Orientation” button, and then click the “Landscape” option. You now have successfully switched your new section to landscape mode.