【发布时间】:2019-07-01 02:49:31
【问题描述】:
我正在尝试将我的 HTML 转换为 PDF。我观看了本教程,https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/,并且我能够成功地让本教程使用硬编码值。问题是......我不知道如何将我的模型及其属性动态地合并到这个例子中。
这是我遵循的步骤...
首先我使用以下版本将reportlab 安装到我的环境中...
pip install --pre xhtml2pdf
这行得通...
然后我按照说明在我的项目中添加了一个 utils.py 文件。
然后我将此代码复制到我的 utils.py 文件中...
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src, context_dict={}):
template = get_template(template_src)
html = template.render(context_dict)
result = BytesIO()
pdf = pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")), result)
if not pdf.err:
return HttpResponse(result.getvalue(), content_type='application/pdf')
return None
然后我创建了一个如下所示的 HTML 文件:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Title</title>
<style type="text/css">
body {
font-weight: 200;
font-size: 14px;
}
.header {
font-size: 20px;
font-weight: 100;
text-align: center;
color: #007cae;
}
.title {
font-size: 22px;
font-weight: 100;
/* text-align: right;*/
padding: 10px 20px 0px 20px;
}
.title span {
color: #007cae;
}
.details {
padding: 10px 20px 0px 20px;
text-align: left !important;
/*margin-left: 40%;*/
}
.hrItem {
border: none;
height: 1px;
/* Set the hr color */
color: #333; /* old IE */
background-color: #fff; /* Modern Browsers */
}
</style>
</head>
<body>
<div class='wrapper'>
<div class='header'>
<p class='title'>Invoice # </p>
</div>
<div>
<div class='details'>
Bill to: {{ customer_name }} <br/>
Amount: {{ amount }} <br/>
Date:
<hr class='hrItem' />
</div>
</div>
</body>
</html>
我还创建了必要的 URL....
然后我创建了一个类似于下面定义的视图:
from django.http import HttpResponse
from django.views.generic import View
from yourproject.utils import render_to_pdf #created in step 4
class GeneratePdf(View):
def get(self, request, *args, **kwargs):
data = {
'today': datetime.date.today(),
'amount': 39.99,
'customer_name': 'Cooper Mann',
'order_id': 1233434,
}
pdf = render_to_pdf('pdf/invoice.html', data)
return HttpResponse(pdf, content_type='application/pdf')
当我单击指向此视图的链接时...输出显示就像教程中所说的一样...
但是,如果我想让我的模型以这种格式显示...我不知道该怎么做。我尝试了一个 DetailView....然后我得到了数据但没有 PDF....我还搜索了许多其他地方,我似乎找不到一个可以让我动态获取模型并拉取的示例根据需要在属性中...提前感谢您的任何帮助或指示。
【问题讨论】:
标签: django django-models django-templates django-views reportlab