图片转PDF
把图片转成PDF的方法有很多,以下是一个把图片映射在PDF白板上的一种方法。
可以根据需要调整文件格式、大小等参数。
# 需要事前安装 PIL, reportlab
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
def convert_png_to_pdf(png_file_path, pdf_file_path):
image = Image.open(png_file_path)
width, height = image.size
c = canvas.Canvas(pdf_file_path, pagesize=letter)
c.setPageSize((width, height))
c.drawImage(png_file_path, 0, 0, width, height)
c.showPage()
c.save()
def main():
png_file_path = 'your_png_file_path/example.PNG'
pdf_file_path = 'your_pdf_file_path/example.pdf'
try:
convert_png_to_pdf(png_file_path, pdf_file_path)
except Exception as e:
print('Error: ', e)
if __name__ == '__main__':
main()