django 動態生成PDF文件


可以通過開源的Python PDF庫ReportLab來實現PDF文件的動態生成。

一、安裝ReportLab

ReportLab庫在PyPI上提供,可以使用pip來安裝:

$ pip install reportlab

在Python交互解釋器中導入它來測試安裝:

>>> import reportlab 

如果沒有拋出任何錯誤,證明已安裝成功。

二、編寫視圖

ReportLab的API可以處理於類似於文件(file-like)的對象。下面是一個 “Hello World”的例子:

from reportlab.pdfgen import canvas from django.http import HttpResponse def some_view(request): # 創建帶有PDF頭部定義的HttpResponse對象 response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"' # 創建一個PDF對象,並使用響應對象作為它要處理的‘文件’ p = canvas.Canvas(response) # 通過PDF對象的drawString方法,寫入一條信息。具體參考模塊的官方文檔說明。 p.drawString(100, 100, "Hello world.") # 關閉PDF對象 p.showPage() p.save() return response 

相關說明:

  • 響應對象的MIME類型為application/pdf。 這會告訴瀏覽器,文檔是個PDF文件而不是HTML文件。
  • 響應對象設置了附加的Content-Disposition協議頭,含有PDF文件的名稱。
  • 文件名可以是任意的,瀏覽器會在“另存為...”對話框等中使用。
  • Content-Disposition以'attachment'開頭,強制讓瀏覽器彈出對話框來提示或者確認。
  • Canvas函數接受一個類似於文件的對象,而HttpResponse對象正好合適。
  • 最后,在PDF文件上調用showPage()和save()方法非常重要。
  • 注意:ReportLab並不是線程安全的。

三、復雜的PDF

使用ReportLab創建復雜的PDF文檔時,可以考慮使用io庫作為PDF文件的臨時保存地點。這個庫提供了一個類似於文件的對象接口,非常實用。 下面的例子是上面的“Hello World”示例采用io重寫后的樣子:

from io import BytesIO 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"' buffer = BytesIO() # Create the PDF object, using the BytesIO object as its "file." p = canvas.Canvas(buffer) # 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. p.showPage() p.save() # Get the value of the BytesIO buffer and write it to the response. pdf = buffer.getvalue() buffer.close() response.write(pdf) return response


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM