- reportlab是一个很好用的Python第三方库,可惜文档是纯英文的。
- 下面是一个导出图片的简单示例
from reportlab.platypus import Image
from reportlab.pdfgen import canvas
def to_pdf(response, url_list=None):
can = canvas.Canvas(response)
url_list.sort(key=lambda x: x['page_num'])
for url in url_list:
image_url = url['image_url']
image = Image(image_url)
image_height = image.drawHeight
image_width = image.drawWidth
can.setPageSize((image_width, image_height))
can.drawImage(image_url, 0, 0, image_width, image_height, 'auto')
can.showPage()
can.save()
return response
if __name__ == "__main__":
urls = [
{"page_num":2 ,"image_url": "image_url"},
{"image_url": "image_url", 'page_num': 1}
]
port = to_pdf('test.pdf', urls)