使用pdf url(pdf2image)将pdf转换为图像


images_from_path = convert_from_path(settings.MEDIA_ROOT+'file/some.pdf',100)
images_from_path[0].save(settings.MEDIA_ROOT+'image/'+'a.jpg'))

我可以像这样获取或保存图像。如何使用pdf文件url(https://example.com/xyz.pdf)获取图像?

使用您最喜欢的HTTP库获取数据:

import requests, pdf2image
pdf = requests.get('https://example.com/xyz.pdf')
pdf2image.convert_from_bytes(pdf.raw.read())

增加@fqrt的答案,我们需要在请求中添加stream=True。获取

pdf = requests.get('https://example.com/xyz.pdf', stream=True)

总结一下,为下一个人节省一些时间:

import requests, pdf2image
pdf = requests.get('https://example.com/xyz.pdf', stream=True)
images = pdf2image.convert_from_bytes(pdf.content)

最新更新