【发布时间】:2018-03-09 00:52:18
【问题描述】:
我正在尝试编写一些代码,将驻留在网络上的 PDF 转换为一系列 jpg。
我得到了工作代码:
1) 获取pdf
2) 将其保存到磁盘
3) 将其转换为 JPG,然后保存到磁盘。
有没有办法编写相同的代码(尝试下面的代码,这会引发错误),可以从 Internet 获取 PDF,但将其保存在内存中(以防止程序写入磁盘/从磁盘读取),然后将其转换为 JPG(要上传到 AWS s3)?
我认为这会起作用:
f = urlopen("https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf") #file to process
但我收到以下错误:
"Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in >被忽略"
完整代码,以及我想要转换的正确 PDF 文件。注意:如果我将 f= 替换为保存在磁盘上的 PDF 的位置,则代码有效:
from urllib2 import urlopen
from wand.image import Image
#location on disk
save_location = "/home/bob/Desktop/pdfs to convert/example1"
#file prefix
test_id = 'example'
print 1
f = urlopen("https://s3.us-east-2.amazonaws.com/converted1jpgs/example.pdf")
print 2
print type(f)
with Image(filename=f) as img:
print('pages = ', len(img.sequence))
with img.convert('jpg') as converted:
converted.save(filename=save_location+"/"+test_id+".jpg")
【问题讨论】: