绘制在reportlab PDF上的图像比PDF纸张尺寸大

3
我正在编写一个程序,它可以将给定文件夹中的所有图片聚合到一个PDF文件中。我的问题是,当图片被绘制时,它们的大小会变大,并且奇怪地向左旋转。我已经到处搜索了,甚至在reportlab文档中也没有找到任何有用信息。
以下是代码:
import os
from PIL import Image
from PyPDF2 import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from reportlab.lib.units import cm
from StringIO import StringIO


def main():
    images = image_search()
    output = PdfFileWriter()
    for image in images:
        Image_file = Image.open(image)     # need to convert the image to the specific size first.
    width, height = Image_file.size
    im_width = 1 * cm
    # Using ReportLab to insert image into PDF
    watermark_str = "watermark" + str(images.index(image)) + '.pdf'
    imgDoc = canvas.Canvas(watermark_str)

    # Draw image on Canvas and save PDF in buffer
    # define the aspect ratio first
    aspect = height / float(width)

    ## Drawing the image
    imgDoc.drawImage(image, 0,0, width = im_width, height = (im_width * aspect))    ## at (399,760) with size 160x160
    imgDoc.showPage()
    imgDoc.save()
    # Get the watermark file just created
    watermark = PdfFileReader(open(watermark_str, "rb"))

    #Get our files ready

    pdf1File = open('sample.pdf', 'rb')
    page = PdfFileReader(pdf1File).getPage(0)
    page.mergePage(watermark.getPage(0))


    #Save the result

    output.addPage(page)
    output.write(file("output.pdf","wb"))

#The function which searches the current directory for image files.
def image_search():
    found_images = []
    for doc in os.listdir(os.curdir):
        image_ext = ['.jpg', '.png', '.PNG', '.jpeg', '.JPG']
        for ext in image_ext:
            if doc.endswith(ext):
                found_images.append(doc)
    return found_images

main()

我还尝试使用im_width变量进行缩放和指定纵横比,但输出结果相同。


我刚试了一下你的代码,看起来它完美地完成了工作。输出是我的 sample.pdf 文件,其中包含我文件夹中底部左侧绘制的图像,宽度为1厘米。我尝试过使用reportlab生成的pdf以及从互联网上随机下载的PDF...所以问题可能与你的reportlab或PyPDF2版本或图像/PDF有关。 - B8vrede
很奇怪,我刚刚试了一下一个完全相同尺寸的图像,它可以正常工作。你有没有尝试将其合并到另一个PDF上?有一种可能性是你要将图像合并到的PDF实际上是旋转的。 - B8vrede
我注意到你说你的输出是sample.pdf,而我的输出是output.pdf。这是否有可能是我们得不到相同结果的原因?@B8vrede - Onikute Opeyemi O
哦,不好意思,那只是一个混淆,我指的是 output.pdf 看起来和 sample.pdf 完全一样,唯一的区别是在 output.pdf 的左下角有一张小图片。但是请问一下,你能检查一下 watermark0.pdf(以及之后的每个数字)是否正确包含了图片吗? - B8vrede
不,他们不会。问题在于图像在画布上的绘制方式。我希望图像从左上角开始,并缩小以适应A4纸张大小。 - Onikute Opeyemi O
显示剩余2条评论
1个回答

4

经过一点关于你的目标的混乱之后,我弄清楚了你的目标是制作当前文件夹中图像的PDF概述。 为此,我们实际上不需要使用PyPDF2,因为Reportlab提供了我们所需的一切。

请看下面的代码,其中包含注释作为指南:

def main():
    output_file_loc = "overview.pdf"
    imgDoc = canvas.Canvas(output_file_loc)
    imgDoc.setPageSize(A4) # This is actually the default page size
    document_width, document_height = A4

    images = image_search()
    for image in images:
        # Open the image file to get image dimensions
        Image_file = Image.open(image)
        image_width, image_height = Image_file.size
        image_aspect = image_height / float(image_width)

        # Determine the dimensions of the image in the overview
        print_width = document_width
        print_height = document_width * image_aspect

        # Draw the image on the current page
        # Note: As reportlab uses bottom left as (0,0) we need to determine the start position by subtracting the
        #       dimensions of the image from those of the document
        imgDoc.drawImage(image, document_width - print_width, document_height - print_height, width=print_width,
                         height=print_height)

        # Inform Reportlab that we want a new page
        imgDoc.showPage()

    # Save the document
    imgDoc.save()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接