“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 文件。 这是由优秀的开源 [https://www.reportlab.com/opensource/ ReportLab] Python PDF 库实现的。 | |
− | 动态生成 PDF | + | 动态生成 PDF 文件的优势在于您可以为不同的目的创建自定义的 PDF——例如,为不同的用户或不同的内容。 |
− | 例如,[http://www.kusports.com/ kusports.com] | + | 例如,[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: |
− | + | 通过在 Python 交互式解释器中导入它来测试您的安装: | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第30行: | 第22行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <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|FileResponse]] 对象接受类文件对象。 | |
− | + | 这是一个“Hello World”示例: | |
<div class="highlight-default notranslate"> | <div class="highlight-default notranslate"> | ||
第51行: | 第43行: | ||
<div class="highlight"> | <div class="highlight"> | ||
− | < | + | <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 | + | # 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, | + | 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')</ | + | return FileResponse(buffer, as_attachment=True, filename='hello.pdf')</syntaxhighlight> |
</div> | </div> | ||
</div> | </div> | ||
− | + | 代码和注释应该是不言自明的,但有几件事值得一提: | |
− | * | + | * 响应将根据文件扩展名自动设置 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>filename</code> 参数。 浏览器将在“另存为...”对话框中使用它。 |
− | * | + | * 您可以连接到 ReportLab API:作为第一个参数传递给 <code>canvas.Canvas</code> 的相同缓冲区可以提供给 [[../../ref/request-response#django.http|FileResponse]] 类。 |
− | * | + | * 请注意,所有后续的 PDF 生成方法都是在 PDF 对象上调用的(在本例中为 <code>p</code>)——而不是在 <code>buffer</code> 上。 |
− | * | + | * 最后,在 PDF 文件上调用 <code>showPage()</code> 和 <code>save()</code> 很重要。 |
<div class="admonition note"> | <div class="admonition note"> | ||
− | + | 笔记 | |
− | ReportLab | + | ReportLab 不是线程安全的。 我们的一些用户报告了构建 PDF 生成的 Django 视图的奇怪问题,这些视图可供多人同时访问。 |
第99行: | 第91行: | ||
<div id="other-formats" class="section"> | <div id="other-formats" class="section"> | ||
− | == | + | == 其他格式 == |
− | + | 请注意,这些示例中没有很多特定于 PDF 的内容——只有使用 <code>reportlab</code> 的位。 您可以使用类似的技术生成任何可以找到 Python 库的任意格式。 另请参阅 [[../outputting-csv|使用 Django]] 输出 CSV 以获取另一个示例以及在生成基于文本的格式时可以使用的一些技术。 | |
<div class="admonition seealso"> | <div class="admonition seealso"> | ||
− | + | 也可以看看 | |
− | Django | + | 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 交互式解释器中导入它来测试您的安装:
如果该命令没有引发任何错误,则安装成功。
写下你的看法
使用 Django 动态生成 PDF 的关键是 ReportLab API 作用于类文件对象,而 Django 的 FileResponse 对象接受类文件对象。
这是一个“Hello World”示例:
代码和注释应该是不言自明的,但有几件事值得一提:
- 响应将根据文件扩展名自动设置 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 视图的奇怪问题,这些视图可供多人同时访问。