django - 在 django 项目中使用 xhtml2pdf 从 html 生成 pdf
问题描述
我正在尝试使用 xhtml2pdf 从 django 项目中的 html 模板生成 pdf。一切正常,但引导程序不起作用。我正在关注本教程https://www.codingforentrepreneurs.com/blog/html-template-to-pdf-in-django/
视图.py
def generate_pdf(request, inflow_id):
template = 'cashflow/invoice2.html'
inflow = get_object_or_404(Inflow, pk=inflow_id)
context = {
"invoice_id": inflow.id,
"customer_name": inflow.student,
"amount": inflow.amount,
"today": inflow.time,
"fee_type": inflow.fee_type,
}
pdf = render_to_pdf(template, context)
if pdf:
response = HttpResponse(pdf, content_type='application/pdf')
filename = "Invoice_%s.pdf" % inflow.student
content = "inline; filename='%s'" % filename
download = request.GET.get("download")
if download:
content = "attachment; filename='%s'" % filename
response['Content-Disposition'] = content
return response
return HttpResponse("Not found")
utils.py 中的 render_to_pdf 函数
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
invoice2.html 文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>duck</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
<script src="//netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-4 col-md-3">
<div class="card" style="width: 24rem;">
<img class="card-img-top" src='https://cdn.bulbagarden.net/upload/thumb/0/0d/025Pikachu.png/250px-025Pikachu.png' alt="Card image cap">
<div class="card-body">
<p class="card-text">Please enter all the fee payments here.</p>
</div>
</div>
</div>
<div>
<h1>Invoice id: {{ invoice_id }}</h1>
<h2>Customer name: {{ customer_name }}</h2>
<h3>Amount: {{ amount }}</h3>
<h4>Time: {{ today }}</h4>
<h5>Fee Type: {{ fee_type }}</h5>
</div>
</div>
</div>
<div class="container">
<div class="row">
<div class="well col-xs-10 col-sm-10 col-md-6 col-xs-offset-1 col-sm-offset-1 col-md-offset-3">
<div class="row">
<div class="col-xs-6 col-sm-6 col-md-6">
<address>
<strong>Elf Cafe</strong>
<br>
2135 Sunset Blvd
<br>
Los Angeles, CA 90026
<br>
<abbr title="Phone">P:</abbr> (213) 484-6829
</address>
</div>
<div class="col-xs-6 col-sm-6 col-md-6 text-right">
<p>
<em>Date: 1st November, 2013</em>
</p>
<p>
<em>Receipt #: 34522677W</em>
</p>
</div>
</div>
<div class="row">
<div class="text-center">
<h1>Receipt</h1>
</div>
<table class="table table-hover">
<thead>
<tr>
<th>Product</th>
<th>#</th>
<th class="text-center">Price</th>
<th class="text-center">Total</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-9"><em>Baked Rodopa Sheep Feta</em></h4></td>
<td class="col-md-1" style="text-align: center"> 2 </td>
<td class="col-md-1 text-center">$13</td>
<td class="col-md-1 text-center">$26</td>
</tr>
<tr>
<td class="col-md-9"><em>Lebanese Cabbage Salad</em></h4></td>
<td class="col-md-1" style="text-align: center"> 1 </td>
<td class="col-md-1 text-center">$8</td>
<td class="col-md-1 text-center">$8</td>
</tr>
<tr>
<td class="col-md-9"><em>Baked Tart with Thyme and Garlic</em></h4></td>
<td class="col-md-1" style="text-align: center"> 3 </td>
<td class="col-md-1 text-center">$16</td>
<td class="col-md-1 text-center">$48</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td class="text-right">
<p>
<strong>Subtotal: </strong>
</p>
<p>
<strong>Tax: </strong>
</p></td>
<td class="text-center">
<p>
<strong>$6.94</strong>
</p>
<p>
<strong>$6.94</strong>
</p></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td class="text-right"><h4><strong>Total: </strong></h4></td>
<td class="text-center text-danger"><h4><strong>$31.53</strong></h4></td>
</tr>
</tbody>
</table>
<button type="button" class="btn btn-success btn-lg btn-block">
Pay Now <span class="glyphicon glyphicon-chevron-right"></span>
</button>
</div>
</div>
</div>
</div>
</body>
</html>
请让我知道我可以在此处更改什么或在此处使用以在生成 pdf 时加载引导程序
解决方案
因为 xhtml2pdf 支持有限数量的标准 CSS 属性。
推荐阅读
- ios - PKPushRegistry.init() 后未调用 pushRegistry PKPushRegistry pushCredentials 委托方法
- java - 从 int[] 或 List 创建一列
- javascript - 角度和平行点的线几何预测
- angular - ngrx state-什么时候展平状态?
- python - 为什么有时会打印“正确!” 两次不接受输入?
- kotlin - Kotlin 初始化:如何失败
- html - 调整浏览器大小或在不同设备上时,引导程序无响应
- c++ - 如何在不增加对库的依赖的情况下添加与库结构的兼容性?
- sql-server - 将 Excel 导入 SQL Server 数据库
- java-8 - 从对象的方法创建 Stream