Reportlab,如何更改页面方向?

时间:2018-06-02 19:24:41

标签: python pdf pdf-generation reportlab

我正在创建包含许多页面的pdf文件。我想要第一页是肖像,其余的页面将是风景。我试过了,

story.append(NextPageTemplate('landscape')) refererence

但我得到了,

ValueError: can't find template('landscape') handle_nextPageTemplate args=('landscape',)

1 个答案:

答案 0 :(得分:1)

我刚想通了。

doc = BaseDocTemplate("mypdf.pdf", pagesize=A4, rightMargin=25, leftMargin=25, topMargin=25, bottomMargin=25)
portrait_frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='portrait_frame ')
landscape_frame = Frame(doc.leftMargin, doc.bottomMargin, doc.height, doc.width, id='landscape_frame ')

story= []
story.append(<first-page-content>)
story.append(NextPageTemplate('landscape'))
story.append(PageBreak())
story.append(<second-page-content>)

doc.addPageTemplates([PageTemplate(id='portrait',frames=portrait_frame),
                      PageTemplate(id='landscape',frames=landscape_frame, pagesize=landscape(A4)),
                      ])
doc.build(story)

参考文献:

http://code.activestate.com/recipes/123612-basedoctemplate-with-2-pagetemplate/

Reportlab : How to switch between portrait and landscape?