“Django/docs/3.0.x/howto/outputting-pdf”的版本间差异

来自菜鸟教程
Django/docs/3.0.x/howto/outputting-pdf
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:使用 Django 输出 PDF — Django 文档}}
 
<div id="outputting-pdfs-with-django" class="section">
 
<div id="outputting-pdfs-with-django" class="section">
  
= 利用 Django 输出 PDF =
+
= 使用 Django 输出 PDF =
  
本文介绍如何用 Django 的视图动态输出 PDF 文件。该功能由绝佳的开源 [https://www.reportlab.com/opensource/ ReportLab] Python PDF 库提供。
+
本文档解释了如何使用 Django 视图动态输出 PDF 文件。 这是由优秀的开源 [https://www.reportlab.com/opensource/ ReportLab] Python PDF 库实现的。
  
动态生成 PDF 文件的优点是你可以为不同的目的创建不同的自定义 PDF——例如,为不同的用户或内容的不同片段生成 PDF。
+
动态生成 PDF 文件的优势在于您可以为不同的目的创建自定义的 PDF——例如,为不同的用户或不同的内容。
  
例如,[http://www.kusports.com/ kusports.com] Django 将自定义的,打印友好的 NCAA 锦标赛树状图生成 PDF 文件,发放给参加三月疯狂竞赛的人。
+
例如,[http://www.kusports.com/ kusports.com] 使用 Django 为参加 March Madness 比赛的人们生成定制的、打印友好的 NCAA 锦标赛支架,作为 PDF 文件。
  
 
<div id="install-reportlab" class="section">
 
<div id="install-reportlab" class="section">
  
== 安装 ReportLab ==
+
== 安装报告实验室 ==
  
ReportLab 库可从 [https://pypi.org/project/reportlab/ PyPI] 获取。也可以下载 [https://www.reportlab.com/docs/reportlab-userguide.pdf 用户指南] (一份 PDF 文件,这不是巧合)。你可以用 <code>pip</code> 安装 ReportLab:
+
ReportLab [https://pypi.org/project/reportlab/ PyPI] 上可用。 还可以下载 [https://www.reportlab.com/docs/reportlab-userguide.pdf 用户指南] (并非巧合,PDF 文件)。 您可以使用 <code>pip</code> 安装 ReportLab:
  
<div class="highlight-console notranslate">
+
通过在 Python 交互式解释器中导入它来测试您的安装:
 
 
<div class="highlight">
 
 
 
<pre>$ python -m pip install reportlab</pre>
 
 
 
</div>
 
 
 
</div>
 
Python 交互解释器中导入它,测试你的安装是否成功:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第30行: 第22行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&gt;&gt;&gt; import reportlab</pre>
+
<syntaxhighlight lang="python">>>> import reportlab</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
若该命令未抛出任何错误,安装成功。
+
如果该命令没有引发任何错误,则安装成功。
  
  
第41行: 第33行:
 
<div id="write-your-view" class="section">
 
<div id="write-your-view" class="section">
  
== 编写视图 ==
+
== 写下你的看法 ==
  
利用 Django 动态生成 PDF 的关键是 ReportLab API 作用于类文件对象,而 Django 的 [[../../ref/request-response#django.http|<code>FileResponse</code>]] 对象接收类文件对象。
+
使用 Django 动态生成 PDF 的关键是 ReportLab API 作用于类文件对象,而 Django 的 [[../../ref/request-response#django.http|FileResponse]] 对象接受类文件对象。
  
这有个 &quot;Hello World&quot; 示例:
+
这是一个“Hello World”示例:
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第51行: 第43行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>import io
+
<syntaxhighlight lang="python">import io
 
from django.http import FileResponse
 
from django.http import FileResponse
 
from reportlab.pdfgen import canvas
 
from reportlab.pdfgen import canvas
第59行: 第51行:
 
     buffer = io.BytesIO()
 
     buffer = io.BytesIO()
  
     # Create the PDF object, using the buffer as its &quot;file.&quot;
+
     # Create the PDF object, using the buffer as its "file."
 
     p = canvas.Canvas(buffer)
 
     p = canvas.Canvas(buffer)
  
 
     # Draw things on the PDF. Here's where the PDF generation happens.
 
     # Draw things on the PDF. Here's where the PDF generation happens.
 
     # See the ReportLab documentation for the full list of functionality.
 
     # See the ReportLab documentation for the full list of functionality.
     p.drawString(100, 100, &quot;Hello world.&quot;)
+
     p.drawString(100, 100, "Hello world.")
  
 
     # Close the PDF object cleanly, and we're done.
 
     # Close the PDF object cleanly, and we're done.
第73行: 第65行:
 
     # present the option to save the file.
 
     # present the option to save the file.
 
     buffer.seek(0)
 
     buffer.seek(0)
     return FileResponse(buffer, as_attachment=True, filename='hello.pdf')</pre>
+
     return FileResponse(buffer, as_attachment=True, filename='hello.pdf')</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
代码和注释应该是不言自明的,但是有几件事值得提一下:
+
代码和注释应该是不言自明的,但有几件事值得一提:
  
* The response will automatically set the MIME type ''application/pdf'' based on the filename extension. This tells browsers that the document is a PDF file, rather than an HTML file or a generic ''application/octet-stream'' binary content.
+
* 响应将根据文件扩展名自动设置 MIME 类型 ''application/pdf''。 这告诉浏览器该文档是 PDF 文件,而不是 HTML 文件或通用的 ''application/octet-stream'' 二进制内容。
* <code>as_attachment=True</code> 传递给 <code>FileResponse</code> 时,它会设置合适的 <code>Content-Disposition</code> 头,这将告诉 Web 浏览器弹出一个对话框,提示或确认如何处理该文档,即便设备已配置默认行为。若省略了 <code>as_attachment</code> 参数,浏览器会用已配置的用于处理 PDF 的程序或插件来处理该 PDF。
+
* <code>as_attachment=True</code> 传递给 <code>FileResponse</code> 时,它会设置适当的 <code>Content-Disposition</code> 标头,并告诉 Web 浏览器弹出一个对话框,提示/确认如何处理文档,即使机器上设置了默认值。 如果省略 <code>as_attachment</code> 参数,浏览器将使用它们已配置为用于 PDF 的任何程序/插件处理 PDF。
* 你也可以提供可选参数 <code>filename</code>。浏览器的“另存为…”对话框会用到它。
+
* 您可以提供任意 <code>filename</code> 参数。 浏览器将在“另存为...”对话框中使用它。
* You can hook into the ReportLab API: The same buffer passed as the first argument to <code>canvas.Canvas</code> can be fed to the [[../../ref/request-response#django.http|<code>FileResponse</code>]] class.
+
* 您可以连接到 ReportLab API:作为第一个参数传递给 <code>canvas.Canvas</code> 的相同缓冲区可以提供给 [[../../ref/request-response#django.http|FileResponse]] 类。
* 注意,所有后续生成 PDF 的方法都是在 PDF 对象上调用的(本例中是 <code>p</code>)——而不是在 <code>buffer</code> 上调用。
+
* 请注意,所有后续的 PDF 生成方法都是在 PDF 对象上调用的(在本例中为 <code>p</code>)——而不是在 <code>buffer</code> 上。
* 最后,牢记在 PDF 文件上调用 <code>showPage()</code> 和 <code>save()</code>
+
* 最后,在 PDF 文件上调用 <code>showPage()</code> 和 <code>save()</code> 很重要。
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
ReportLab 不是线程安全的。某些用户已经报告了一些奇怪的 issue,在创建用于生成 PDF Django 视图时,这些视图被多个用户同时访问会出现问题。
+
ReportLab 不是线程安全的。 我们的一些用户报告了构建 PDF 生成的 Django 视图的奇怪问题,这些视图可供多人同时访问。
  
  
第99行: 第91行:
 
<div id="other-formats" class="section">
 
<div id="other-formats" class="section">
  
== 其它格式 ==
+
== 其他格式 ==
  
注意,这些例子中没有任何 PDF 特有的数据——只有使用 <code>reportlab</code> 的部分。你可以用类似的技巧生成任意格式,只要你能找到对应的 Python 库。也请看看 [[../outputting-csv|<span class="doc">利用 Django 输出 CSV</span>]],看看另一个例子中,如何用一些技巧输出文本内容。
+
请注意,这些示例中没有很多特定于 PDF 的内容——只有使用 <code>reportlab</code> 的位。 您可以使用类似的技术生成任何可以找到 Python 库的任意格式。 另请参阅 [[../outputting-csv|使用 Django]] 输出 CSV 以获取另一个示例以及在生成基于文本的格式时可以使用的一些技术。
  
 
<div class="admonition seealso">
 
<div class="admonition seealso">
  
参见
+
也可以看看
  
Django 包提供了一个 [https://djangopackages.org/grids/g/pdf/ 包的比较] 有助于用 Django 生成 PDF 文件。
+
Django Packages 提供了包 [https://djangopackages.org/grids/g/pdf/ ] 比较,有助于从 Django 生成 PDF 文件。
  
  
第113行: 第105行:
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Django 3.0.x 中文文档]]
+
[[Category:Django 3.0.x 文档]]

2021年10月31日 (日) 04:08的最新版本

使用 Django 输出 PDF

本文档解释了如何使用 Django 视图动态输出 PDF 文件。 这是由优秀的开源 ReportLab Python PDF 库实现的。

动态生成 PDF 文件的优势在于您可以为不同的目的创建自定义的 PDF——例如,为不同的用户或不同的内容。

例如,kusports.com 使用 Django 为参加 March Madness 比赛的人们生成定制的、打印友好的 NCAA 锦标赛支架,作为 PDF 文件。

安装报告实验室

ReportLab 库 在 PyPI 上可用。 还可以下载 用户指南 (并非巧合,PDF 文件)。 您可以使用 pip 安装 ReportLab:

通过在 Python 交互式解释器中导入它来测试您的安装:

>>> import reportlab

如果该命令没有引发任何错误,则安装成功。


写下你的看法

使用 Django 动态生成 PDF 的关键是 ReportLab API 作用于类文件对象,而 Django 的 FileResponse 对象接受类文件对象。

这是一个“Hello World”示例:

import io
from django.http import FileResponse
from reportlab.pdfgen import canvas

def some_view(request):
    # Create a file-like buffer to receive PDF data.
    buffer = io.BytesIO()

    # Create the PDF object, using the buffer 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, and we're done.
    p.showPage()
    p.save()

    # FileResponse sets the Content-Disposition header so that browsers
    # present the option to save the file.
    buffer.seek(0)
    return FileResponse(buffer, as_attachment=True, filename='hello.pdf')

代码和注释应该是不言自明的,但有几件事值得一提:

  • 响应将根据文件扩展名自动设置 MIME 类型 application/pdf。 这告诉浏览器该文档是 PDF 文件,而不是 HTML 文件或通用的 application/octet-stream 二进制内容。
  • as_attachment=True 传递给 FileResponse 时,它会设置适当的 Content-Disposition 标头,并告诉 Web 浏览器弹出一个对话框,提示/确认如何处理文档,即使机器上设置了默认值。 如果省略 as_attachment 参数,浏览器将使用它们已配置为用于 PDF 的任何程序/插件处理 PDF。
  • 您可以提供任意 filename 参数。 浏览器将在“另存为...”对话框中使用它。
  • 您可以连接到 ReportLab API:作为第一个参数传递给 canvas.Canvas 的相同缓冲区可以提供给 FileResponse 类。
  • 请注意,所有后续的 PDF 生成方法都是在 PDF 对象上调用的(在本例中为 p)——而不是在 buffer 上。
  • 最后,在 PDF 文件上调用 showPage()save() 很重要。

笔记

ReportLab 不是线程安全的。 我们的一些用户报告了构建 PDF 生成的 Django 视图的奇怪问题,这些视图可供多人同时访问。


其他格式

请注意,这些示例中没有很多特定于 PDF 的内容——只有使用 reportlab 的位。 您可以使用类似的技术生成任何可以找到 Python 库的任意格式。 另请参阅 使用 Django 输出 CSV 以获取另一个示例以及在生成基于文本的格式时可以使用的一些技术。

也可以看看

Django Packages 提供了包 比较,有助于从 Django 生成 PDF 文件。