Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python FPDF页面大小不同(文档是否过期?)_Python_Pdf - Fatal编程技术网

Python FPDF页面大小不同(文档是否过期?)

Python FPDF页面大小不同(文档是否过期?),python,pdf,Python,Pdf,下面的代码将图像添加到PDF中 def __create(self): if len(self.__DLImages) == 0: raise Exception("No images to add to PDF") else: maxWidth = max([Image.open(imgPath).size[0] for imgPath in self.__DLImages]) maxHeight = max([Image.op

下面的代码将图像添加到PDF中

def __create(self):
    if len(self.__DLImages) == 0:
        raise Exception("No images to add to PDF")
    else:
        maxWidth  = max([Image.open(imgPath).size[0] for imgPath in self.__DLImages])
        maxHeight = max([Image.open(imgPath).size[1] for imgPath in self.__DLImages])
        PDF = FPDF(unit = "pt", format = (maxWidth, maxHeight))

        for imgPath in self.__DLImages:
            PDF.add_page()
            PDF.image(imgPath, x = self.__margin, y = self.__margin)

        PDF.output(self.__outputFile)
但我必须将高度和宽度设置为最大尺寸,这样图像才能适合,从而使较小的图像周围有巨大的空间

文档中说我可以在图像上设置宽度和高度,但当我不做任何更改时,因为我已经设置了页面的尺寸(我必须这样做才能使其适合),否则它默认为A4

它还说我可以更改页面的尺寸,但是在查看源代码之后,
add_page()
方法除了
方向
之外,没有任何其他参数,这不是这个答案所建议的

我找到了一个新模块并完成了这个解决方案

我找到了一个新模块并完成了这个解决方案

from reportlab.pdfgen import canvas
def __create(self):
    c = canvas.Canvas(self.__outputFile)
    for imgPath in self.__DLImages:
        with PIL.Image.open(imgPath) as loadedImage:
            w, h = loadedImage.size
        c.setPageSize((w, h))
        c.drawImage(imgPath, x = 0, y = 0)
        c.showPage()
    c.save()