如何解决如何在Django中将boostrap模板转换为pdf
我有发票引导程序模板,其中在发票中填充响应数据后,数据带有ajax响应,然后我想将具有其CSS的确切模板转换为pdf,以便我可以使用django服务器通过电子邮件发送。是否有将模板及其数据和CSS转换为pdf的方法。我尝试了很多事情,但无法获得理想的结果。
我想要此发票的确切PDF版本(含CSS)。谢谢。
解决方法
我个人会使用reportlab
https://docs.djangoproject.com/en/3.1/howto/outputting-pdf/
请按照此处的说明进行操作。
使用另一个软件包在这里也有一个很好的答案: Generate PDF from HTML using Django and Reportlab
,就我而言,您可能想使用WeasyPrint (Vanilla)。我使用Vanilla /原始版本,因为它简单易用。
摘录自使用它的repository中的代码。但是我还是会进行一些示范。
- 从要呈现内容的位置创建HTML视图。
select tbl.tablename,l.moment,l.root_pid,l.origin + ' - ' + l.message AS ErrorMessage
from dbo.log l
outer apply ( select top 1 tbl.tablename
from dbo.transaction_tbl tbl
where tbl.rootpid = l.root_pid -- link process id ...
and tbl.startdatetime >= l.moment
and tbl.errormessage not in (' - ')
order by tbl.startdatetime ) tbl; -- ... and timing in one query
注意:# inside pdfRenderer.html
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>Simple Task Management | Summer Skilling #1</title>
</head>
<body>
{% include "databoard.html" with pdfRenderMode=True %}
</body>
<!-- ! Content Ends Here -->
</html>
包含我要在PDF内部呈现的布局。在这里instead中查看。 pdfRender.html是我要在其中加载databoard.html
的正文。
- 像普通Django视图一样使用它,但相反,我们使用WeasyPrint的功能将其呈现为文件。
databoard.html
结果
注意:渲染效果似乎是通过# Inside views.py
from weasyprint import CSS,HTML
from django.template.loader import render_to_string
from django.core.files.storage import FileSystemStorage
from django.http import HttpResponse,HttpResponseRedirect
template_pdfRenderer = "pdfRenderer.html" # Refer to the html file I want to render at my PDF at.
# Create a Filename of the PDF.
pdfFileName = "%s_%s.pdf" % (
self.request.user.username,datetime.datetime.now().strftime("%H%M%S%m%d%Y"),)
# Renders The HTML with Model's Content.
ml_string = render_to_string(
template_pdfRenderer,{"DataSets": UserTasks.objects.filter(Task_Owner=self.request.user.uuid)},)
# Let WeasyPrint Get HTML String of ml_string.
# Input the location from where it would be saved (in server's part / perspective)
# Add some stylesheets incorporated with the HTMLs. DO NOT INCLUDE CSS INSIDE THESE TEMPLATES AS THEY WON'T WORK.
html = HTML(string=html_string).write_pdf(
target="userdata/ExportedPDFs/%s" % (pdfFileName),stylesheets=[
CSS("CrudSimulation\static\css\material.min.css"),CSS("CrudSimulation\static\css\sc_require-min.css"),],)
# Instantiate FileStorage.
fs = FileSystemStorage("")
# Open The Saved PDF and Let User Saved It By Packaging it to the HttpResponse.
with fs.open("userdata/ExportedPDFs/" + pdfFileName) as pdf:
response = HttpResponse(pdf,content_type="application/pdf")
response["Content-Disposition"] = 'attachment; filename="%s.pdf"' % (pdfFileName)
return response
# Once we're done,returning to a particular URL.
return HttpResponseRedirect(reverse("data_user_view"))
保存的,但是添加内容的灵活性是我推荐这么做的一个很好的补充。
对于基本设置,您可能想看一下本指南> Using Python WeasyPrint generate HTML to PDF in django,但是,我主要使用此guide。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。