简体   繁体   English

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

[英]How to create a PDF document with differing page sizes in reportlab, python

Is it possible to create a PDF document with differing page sizes in reportlab?是否可以在 reportlab 中创建具有不同页面大小的 PDF 文档?

I would like to create a document where the first page has a different size then the other pages.我想创建一个文档,其中第一页的大小与其他页面不同。 Can anyone help?任何人都可以帮忙吗?

Yes, this should be possible, since PDF supports this, it's just a question of how to make it happen in ReportLab.是的,这应该是可能的,因为 PDF 支持这一点,这只是如何在 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()

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.您的文档现在应该有两页,一页具有默认大小的页面,另一页的页面大小为 700 pt x 500 pt。

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.如果您使用的是 PLATYPUS,您应该能够实现相同的目标,但可能需要花哨的BaseDocTemplate子类来处理页面大小的变化,因为我很确定PageTemplate机器不支持这个,因为每个PageTemplate主要是一种改变框架在每个页面上的布局方式的方法。 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.但这在技术上是可行的,只是没有记录,您可能需要花一些时间阅读和了解 PLATYPUS 内部的工作原理。

my use case was to create a big table inside pdf.我的用例是在 pdf 中创建一个大表。 as table was big it was getting cropped on both sides.由于桌子很大,两边都被剪掉了。 this is how to create a pdf with custom size.i am using platypus from reportlab.这是如何创建具有自定义尺寸的 pdf。我正在使用来自 reportlab 的鸭嘴兽。

from reportlab.platypus import  SimpleDocTemplate
from reportlab.lib.units import mm, inch

pagesize = (20 * inch, 10 * inch)  # 20 inch width and 10 inch height.
doc = SimpleDocTemplate('sample.pdf', pagesize=pagesize)
data =    [['sarath', 'indiana', 'usa'],
                            ['jose', 'indiana', 'shhs']]
table = Table(data)
elems = []
elems.append(table)
doc.build(elems)

one downside of this technique is the size is same for all pages.But will help people looking to create pdf with custom size (same for all pages)这种技术的一个缺点是所有页面的大小相同。但会帮助希望创建具有自定义大小的 pdf 的人(所有页面都相同)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM