我正在尝试将pdf文件的第一页转换为缩略图JPEG文件。我有类似于以下python代码的代码。
from tempfile import TemporaryFile, NamedTemporaryFile
import os
from subprocess import call
…
def createThumbnail(reporttempfile, thumbnailtempfile):
# build the command for executing for extracting thumbnail
command = "convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75"
csplit = command.split(' ')
csplit.append("pdf:"+reporttempfile.name + "[0]")
csplit.append(thumbnailtempfile.name)
print "csplit = ", csplit
print "cmd = %s" % " ".join(csplit)
# run the command convert to create thumbnail
retval = call(csplit)
print "retval = %d" % retval
return retval
当我调用上面的函数时,我得到以下输出:
csplit = ['convert', '-resize', '241x344', '-background', 'white', '-gravity', 'center', '-extent', '241x344', '-quality', '75', 'pdf:/tmp/reportfile0001_zeEJ8B.pdf[0]', '/tmp/thumbnail0001_DTPHGb.jpg']
cmd = convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75 pdf:/tmp/reportfile0001_zeEJ8B.pdf[0] /tmp/thumbnail0001_DTPHGb.jpg
**** Error: Cannot find a 'startxref' anywhere in the file.
**** Warning: An error occurred while reading an XREF table.
**** The file has been damaged. This may have been caused
**** by a problem while converting or transfering the file.
**** Ghostscript will attempt to recover the data.
**** Error: Trailer is not found.
Requested FirstPage is greater than the number of pages in the file: 0
No pages will be processed (FirstPage > LastPage).
**** This file had errors that were repaired or ignored.
**** Please notify the author of the software that produced this
**** file that it does not conform to Adobe's published PDF
**** specification.
convert.im6: Postscript delegate failed `/tmp/reportfile0001_zeEJ8B.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/677.
convert.im6: no images defined `/tmp/thumbnail0001_DTPHGb.jpg' @ error/convert.c/ConvertImageCommand/3044.
retval = 1
1
但是,当我通过在shell上键入来直接调用命令时,转换成功没有任何问题,我可以看到.jpg文件被正确转换。使用的命令是(也在上面的输出中打印):
convert -resize 241x344 -background white -gravity center -extent 241x344 -quality 75 pdf:/tmp/reportfile0001_zeEJ8B.pdf[0] /tmp/thumbnail0001_DTPHGb.jpg
我在Mac上看到过这种行为(10.9.5),ImageMagick 6.9.0-0 Q16 x86_64 2015-04-09以及Ubuntu 14和ImageMagick 6.7.7-10 2014-03-06 Q16。
我知道为什么会发生这种情况。有人可以帮忙吗?
此致
SN