【问题标题】:FPDF different page size (documentation is out of date?)FPDF 不同的页面大小(文档已过期?)
【发布时间】:2017-06-25 19:52:51
【问题描述】:

所以下面的代码将图像添加到 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)

但我必须将高度和宽度设置为最大尺寸,以便图像适合,从而导致较小的图像周围有巨大的空间。

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

它还说我可以更改页面的尺寸Link,但是在查看源代码之后,add_page() 方法除了orientation 之外不采用任何其他参数这不是这个答案所暗示的@987654323 @

【问题讨论】:

    标签: python pdf


    【解决方案1】:
    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()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-31
      • 1970-01-01
      • 2013-11-26
      • 2012-09-16
      • 2015-02-28
      相关资源
      最近更新 更多