def image_to_pdf(self, img, pdf_path=None, **kwargs):
"""
Convert image to pdf.
:param img: image file opened by PIL
:param pdf_path: path to save pdf
:param kwargs: any parameter accepted by Image.save i.e. quality
:return:
"""
processor = ResizeToFit(width=self.max_size_in_pixels[0], height=self.max_size_in_pixels[1])
img = processor.process(img)
# Create a white canvas and paste the image
final_img_width = min(img.size[0], self.max_size_in_pixels[0])
final_img_height = min(img.size[1], self.max_size_in_pixels[1])
tmp_image = Image.new("RGB", (final_img_width, final_img_height), "white")
margin_left = 0
margin_top = 0
tmp_image.paste(img, (margin_left, margin_top,
final_img_width, final_img_height))
# Save the image as .pdf file
if not pdf_path:
f = NamedTemporaryFile(delete=False)
pdf_path = f.name
tmp_image.save(pdf_path, "PDF", resolution=100.0, **kwargs)
return pdf_path
评论列表
文章目录