使用reportlab从图像创建A4尺寸的PDF

问题描述 投票:0回答:1

我想使用python中的reportlab将A4扫描图像转换为PDF。我写了下面的代码,但出现错误。我该如何解决它?

from reportlab.lib.pagesizes import A4
from reportlab.lib.units import mm
height, width = A4
doc = SimpleDocTemplate("image.pdf", pagesize=A4)
parts = []
parts.append(Image("image.jpg",width=210*mm, height= 293*mm))
doc.build(parts)

reportlab.platypus.doctemplate.LayoutError:Flowable (595.275590551 x 830.551181102) 在模板“稍后”的框架“正常”(439.275590551 x 685.88976378*) 的第 2 页上太大

python image pdf reportlab
1个回答
0
投票
parts.append(Image("image.jpg",width=210*mm-doc.rightMargin-doc.leftMargin, height=293*mm-doc.topMargin-doc.bottomMargin))

应该可以。如果你需要保持 image.jpg 的宽高比,你将需要更多的额外工作。

© www.soinside.com 2019 - 2024. All rights reserved.