我在問一個與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