如何在reportlab,python中创建具有不同页面大小的PDF文档

问题描述:

是否可以在reportlab中创建具有不同页面大小的PDF文档?

Is it possible to create a PDF document with differing page sizes in reportlab?

我想创建一个文档,其中第一页的大小与其他页面的大小不同.有人可以帮忙吗?

I would like to create a document where the first page has a different size then the other pages. Can anyone help?

是的,这应该可行,因为PDF支持此功能,这只是如何在ReportLab中实现它的问题.我从来没有做过,但是以下应该可以工作:

Yes, this should be possible, since PDF supports this, it's just a question of how to make it happen in ReportLab. I've never done this, but the following should work:

c = reportlab.pdfgen.canvas.Canvas("test.pdf")
# draw some stuff on c
c.showPage()
c.setPageSize((700, 500)) #some page size, given as a tuple in points
# draw some more stuff on c
c.showPage()
c.save()

您的文档现在应该有两页,一个具有默认大小的页面,另一个具有700 pt x 500 pt的页面.

And your document should now have two pages, one with a default size page and one with a page of size 700 pt by 500 pt.

如果您正在使用PLATYPUS,您应该能够实现相同的目的,但是可能很需要在BaseDocTemplate子类中花哨才能处理更改的页面大小,因为我很确定PageTemplate机器不会因为每个PageTemplate主要是一种更改框架在每个页面上的布局方式的方式,所以尚不支持此功能.但这在技术上是可能的,只是没有记录在案,您可能不得不花一些时间阅读和理解PLATYPUS在内部的工作方式.

If you are using PLATYPUS you should be able to achieve the same sort of thing, but will probably require getting fancy in a BaseDocTemplate subclass to handle changing page sizes, since I'm pretty sure the PageTemplate machinery doesn't already support this since each PageTemplate is mainly a way of changing the way frames are laid out on each page. But it is technically possible, it just isn't documented and you'll probably have to spend some time reading and understanding how PLATYPUS works internally.