2013-10-26 96 views
2

我有3個關於在django中使用報告實驗室生成的pdf的問題,我似乎無法找到一個好的答案。 django網站上的文檔僅涵蓋了如何生成pdf以供下載。但是,如何將生成的pdf附加到電子郵件中併發送,而不是下載它?如何將pdf保存到服務器上的目錄而不是下載它?如何在瀏覽器窗口中顯示PDF,而不是下載它?只需使用django文檔示例:django reportlab將pdf添加到電子郵件中,將pdf保存到服務器,在瀏覽器中顯示pdf

from reportlab.pdfgen import canvas 
from django.http import HttpResponse 

def some_view(request): 
    # Create the HttpResponse object with the appropriate PDF headers. 
    response = HttpResponse(content_type='application/pdf') 
    response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' 

    # Create the PDF object, using the response object as its "file." 
    p = canvas.Canvas(response) 

    # Draw things on the PDF. Here's where the PDF generation happens. 
    # See the ReportLab documentation for the full list of functionality. 
    p.drawString(100, 100, "Hello world.") 

    # Close the PDF object cleanly, and we're done. 
    p.showPage() 
    p.save() 
    return response 

回答

2

要在瀏覽器中顯示德PDF而不是下載它,你只需要刪除單詞附件從

response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' 

你會有這樣的事情

response['Content-Disposition'] = 'filename="somefilename.pdf"' 
相關問題