我在问一个与this one非常类似的问题。我在Django的Ubuntu服务器上使用wkhtmltopdf创建pdf。在Django中返回一个PDF响应
from tempfile import *
from subprocess import Popen, PIPE
tempfile = gettempdir()+"/results.pdf"
papersize = 'Tabloid'
orientation = 'Landscape'
command_args = "wkhtmltopdf -O %s -s %s -T 0 -R 0 -B 0 -L 0 http://pdfurl %s" %(orientation, papersize, tempfile)
popen = Popen(command_args, stdout=PIPE, stderr=PIPE)
pdf_contents = popen.stdout().read()
popen.terminate()
popen.wait()
response = HttpResponse(pdf_contents, mimetype='application/pdf')
return response
这给了我一个“没有这样的文件或目录”在popen = Popen ...线上的错误。所以我改变该行
popen = Popen(["sh", "-c", command_args], stdout=PIPE, stderr=PIPE)
,现在我得到一个“‘文件’对象不是可调用的”错误的pdf_contents = ...行。
我也试着在popen = ...行添加.communicate(),但我似乎无法找到这种方式的PDF输出。我应该补充一点,在命令行中输入command_args行创建pdf就好了。任何人都可以将我指向正确的方向吗?
当你手动运行此命令,是将其输出到控制台?还是仅限于gettempdir()+“/ results.pdf”文件? – 2011-03-24 19:24:25
它输出到results.pdf文件,正确格式化和一切。 – buken 2011-03-24 19:32:44