Python 绘制到reportlab pdf的图像大于pdf纸张大小
我正在写一个程序,它把所有的图片都放在一个给定的文件夹中,并将它们聚合成pdf格式。我的问题是,当图像被绘制时,它们的尺寸更大,并且奇怪地向左旋转。我到处都找过了,甚至在reportlab文档中也没有找到任何东西 代码如下:Python 绘制到reportlab pdf的图像大于pdf纸张大小,python,pdf,reportlab,pypdf2,Python,Pdf,Reportlab,Pypdf2,我正在写一个程序,它把所有的图片都放在一个给定的文件夹中,并将它们聚合成pdf格式。我的问题是,当图像被绘制时,它们的尺寸更大,并且奇怪地向左旋转。我到处都找过了,甚至在reportlab文档中也没有找到任何东西 代码如下: import os from PIL import Image from PyPDF2 import PdfFileWriter, PdfFileReader from reportlab.pdfgen import canvas from reportlab.lib.un
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()
我只是试着运行你的代码,它似乎做得很好。输出是我的
sample.pdf
,文件夹中的图像绘制在左下角,宽度为1厘米。我用reportlab生成的pdf以及从互联网上随机提取的pdf尝试了这一点。。。因此,问题可能与您的reportlab或PyPDF2版本或image/pdf有关。它正常工作,但图像大小1962x2362没有正确合并到pdf中。它转向一边,只露出一部分@B8VREDE这很奇怪,我只是用一张精确尺寸的图片试了一下,结果还是很好。你试过把它合并到不同的PDF上吗?有一个很小的机会,你正在合并图像的PDF实际上是一个旋转的。我注意到你说你的输出是sample.PDF,我的输出是output.PDF。有没有可能这就是我们没有得到相同结果的原因@B8vredeOh不,这只是一个误会,我指的是output.pdf
看起来与sample.pdf
一模一样,唯一的区别是output.pdf
的左下角有一个小图像。但是可以检查一下你的水印0.pdf
(以及之后的每一个数字)是否以正确的方式包含图像?