python - 绘制到 reportlab pdf 的图像大于 pdf 纸张大小

标签 python pdf reportlab pypdf

我正在编写一个程序,它可以获取给定文件夹中的所有图片并将它们聚合成一个 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 变量缩放和指定纵横比,这给出了相同的输出。

最佳答案

在对您的目标有点困惑之后,我发现目标是对当前文件夹中的图像制作 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()

关于python - 绘制到 reportlab pdf 的图像大于 pdf 纸张大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35896045/

相关文章:

c# - 将图像渲染为 pdf 是模糊的

java - 在Android中生成PDF,无需任何第三方库

python - 使用 unicode 以任何语言保存文件

python - 在Python中多次创建turtle对象

python - 名称错误 : name 'flask' is not defined

python - 选择最长和最短曲线

python - 需要帮助使用 python 生成 PDF 或 Doc 格式的报告

python - 一个简单的python循环问题

python - 在 Python 中使用 ReportLabs 生成报告

python - xhtml2pdf ImportError - Django