要在使用 reportlab 构建的报告中插入 image.pdf,一种方法是首先使用 Wand 将 image.pdf 转换为 image.jpg:
from wand.image import Image as wandImage
from reportlab.platypus import Image
....
with wandImage(filename='input.pdf') as img:
img.crop(42, 64, 552, 677) # (left, top, width, height)
img.save(filename = "output.jpg")
img = Image("output.jpg", 175.0*mm, 30.0*mm)
img.hAlign = 'CENTER'
rapport.append(img)
...
保存一个 file.jpg 并在之后打开这个文件可能看起来不太优雅。是否可以在没有保存步骤的情况下直接使用结果?例如这个白痴代码(不起作用!):
from wand.image import Image as wandImage
from reportlab.platypus import Image
....
with wandImage(filename='input.pdf') as img:
img.crop(42, 64, 552, 677) # (left, top, width, height)
img2 = img.convert('jpeg')
img3 = Image(img2, 175.0*mm, 30.0*mm) # 2508px × 430px
img3.hAlign = 'CENTER'
rapport.append(img3)
...