<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                {% raw %} # 使用Django輸出PDF # 這篇文檔闡述了如何通過使用Django視圖動態輸出PDF。這可以通過一個出色的、開源的Python PDF庫[ReportLab](http://www.reportlab.com/opensource/)來實現。 動態生成PDF文件的優點是,你可以為不同目的創建自定義的PDF -- 這就是說,為不同的用戶或者不同的內容。 例如,Django在[kusports.com](http://www.kusports.com/)上用來為那些參加March Madness比賽的人,生成自定義的,便于打印的 NCAA 錦標賽晉級表作為PDF文件。 ## 安裝ReportLab ## ReportLab庫在[PyPI](https://pypi.python.org/pypi/reportlab)上提供。也可以下載到[用戶指南](http://www.reportlab.com/docs/reportlab-userguide.pdf) (PDF文件,不是巧合)。 你可以使用`pip`來安裝ReportLab: ``` $ pip install reportlab ``` 通過在Python交互解釋器中導入它來測試你的安裝: ``` >>> import reportlab ``` 若沒有拋出任何錯誤,則已安裝成功。 ## 編寫你的視圖 ## 使用Django動態生成PDF的關鍵是,ReportLab API作用于類似于文件的對象,并且Django的 `HttpResponse`對象就是類似于文件的對象。 這里是一個 “Hello World”的例子: ``` 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 ``` 代碼和注釋是不用多說的,但是一些事情需要提醒一下: + 響應對象獲得了一個特殊的MIME類型, `application/pdf`。這會告訴瀏覽器,文檔是個PDF文件而不是HTML文件。 如果你把它去掉,瀏覽器可能會把輸出解釋為HTML,會在瀏覽器窗口中顯示一篇丑陋的、可怕的官樣文章。 + 響應對象獲取了附加的`Content-Disposition`協議頭,它含有PDF文件的名稱。 文件名可以是任意的;你想把它叫做什么都可以。瀏覽器會在”另存為“對話框中使用它,或者其它。 + 在這個例子中,`Content-Disposition` 協議頭以 `'attachment;'` 開頭。 這樣就強制讓瀏覽器彈出對話框來提示或者確認,如果機器上設置了默認值要如何處理文檔。如果你去掉了`'attachment;'`,無論什么程序或控件被設置為用于處理PDF,瀏覽器都會使用它。代碼就像這樣: ``` response['Content-Disposition'] = 'filename="somefilename.pdf"' ``` + 鉤住ReportLab API 非常簡單:只需要向`canvas.Canvas`傳遞`response`作為第一個參數。`Canvas`函數接受一個類似于文件的對象,而 `HttpResponse`對象正好合適。 + 注意所有隨后的PDF生成方法都在PDF對象(這個例子是p)上調用,而不是`response`對象上。 + 最后,在PDF文件上調用`showPage()` 和 `save()`非常重要。 > 注意 > > ReportLab并不是線程安全的。一些用戶報告了一些奇怪的問題,在構建生成PDF的Django視圖時出現,這些視圖在同一時間被很多人訪問。 ## 復雜的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 ``` ## 更多資源 ## + [PDFlib](http://www.pdflib.org/)與Python捆綁的另一個PDF生成庫。在Django中使用它的方法和這篇文章所闡述的相同。 + [Pisa XHTML2PDF](http://www.xhtml2pdf.com/)是另一個PDF生成庫。Pisa自帶了如何將 Pisa 集成到 Django的例子。 + [HTMLdoc](http://www.htmldoc.org/)是一個命令行腳本,它可以把HTML轉換為PDF。它并沒有Python接口,但是你可以使用`system` 或者 `popen`,在控制臺中使用它,然后再Python中取回輸出。 ## 其它格式 ## 要注意在這些例子中并沒有很多PDF特定的東西 -- 只是使用了`reportlab`。你可以使用相似的技巧來生成任何格式,只要你可以找到對應的Python庫。關于用于生成基于文本的格式的其它例子和技巧,另見[使用Django輸出CSV](http://python.usyiyi.cn/django/howto/outputting-csv.html)。 > 譯者:[Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html),原文:[Generating PDF](https://docs.djangoproject.com/en/1.8/howto/outputting-pdf/)。 > > 本文以 [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/cn/) 協議發布,轉載請保留作者署名和文章出處。 > > [Django 文檔協作翻譯小組](http://python.usyiyi.cn/django/index.html)人手緊缺,有興趣的朋友可以加入我們,完全公益性質。交流群:467338606。 {% endraw %}
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看