python-3.x - pdf格式输出分散在django中
问题描述
您好我想使用创建发票WeasyPrint
。我想得到格式良好的打印作为发票的输出。
我正在使用以下函数来创建 pdf:
from weasyprint import CSS, HTML
def render_to_pdf(context, file_name):
file_data = render_to_string('order_detail.html', context)
html = HTML(string=file_data).write_pdf(
target='collections/%s' % (file_name),
stylesheets = [CSS(string='body { font-size: 7px }')]
)
fs = FileSystemStorage("")
with fs.open("collections/" + file_name) as pdf:
response = HttpResponse(pdf, content_type="application/pdf")
response['Content-Disposition'] = '"attachment; filename="%s"'% (file_name)
print("pdf working")
return response
在我使用的最新版本中,stringformat
但之前我尝试使用普通的 jinja 模板并且输出是相同的。这是我的html:
{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Email</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC" crossorigin="anonymous">
<style>
.container {
border: 1px solid #585696;
padding: 0;
}
.header {
background-color: #585696;
padding: 1.5rem;
}
h1.text-center {
color: #fff;
text-align: center;
}
.text-center {
text-align: center;
color: #585696;
}
.text-content {
padding: 2rem;
}
.order-header{
color: #585696;
margin-top: 1rem;
}
.table-stripped{
width: 100%;
border: 1px solid #585696;
}
.title-bar {
background-color: #585696;
color: #fff;
}
th, td {
text-align: center;
border-bottom: 1px solid #585696;
}
tr {
border-bottom: 1px solid #585696;
}
.address-time {
margin-top: 0;
}
.footer{
margin: 1rem auto;
}
</style>
</head>
<body>
<div class="container">
<div class="header text-center">
<h1 class="text-white text-center"> Order Confirmation [ # {{order.id|stringformat:"i" }}]</h1>
</div>
<div class="text-content">
<h4 class="text-user">
<strong> Dear {{ order.user.email }}, </strong>
<br>
<p>
We received your order and are now being processed. Your order details are shown below for your reference.
</p>
</h4>
<h1 class="order-header">
<strong> Your Order: #{{ order.id|stringformat:"i" }} </strong>
</h1>
<table class="table-stripped">
<thead class="title-bar">
<th> Sr. no. </th>
<th> Product Name </th>
<th> Rate </th>
<th> Quantity </th>
<th> Amount </th>
</thead>
<tbody>
{% for item in orders %}
<tr style="border-bottom: 1px solid #585696;">
<td> {{ forloop.counter|stringformat:"i" }} </td>
<td> {{ item.product.name }} </td>
<td> $ {{ item.price|stringformat:".2f" }} </td>
<td> {{ item.quantity|stringformat:"i" }} </td>
<td> $ {{ item.line_cost|stringformat:".2f" }} </td>
</tr>
{% endfor %}
<tr class="title-bar" style="border-bottom: 1px solid #585696;">
<td></td>
<th> Cart Total </th>
<td></td>
<td></td>
<th> $ {{ order.total_cost }} </th>
</tr>
<tr style="border-bottom: 1px solid #585696;">
<td></td>
<th> Shipping Charges </th>
<td></td>
<td></td>
<th> $ {{ order.shipping_charges }} </th>
</tr>
{% if discounts %}
<tr style="border-bottom: 1px solid #585696;">
<td></td>
<th> Discounts </th>
<td></td>
<td></td>
<th> $ {{ total_doscount }} </th>
</tr>
{% endif %}
<tr class="title-bar" style="border-bottom: 1px solid #585696;">
<td></td>
<th> Total </th>
<td></td>
<td></td>
<th> $ {{ order.total_cost }} </th>
</tr>
</tbody>
</table>
<h1 class="order-header"> Customer Details </h1>
<table class="table-stripped">
<thead class="title-bar">
<th> Name </th>
<th> Phone Number </th>
<th> Payment Method </th>
</thead>
<tbody>
<tr>
<td> {{ order.first_name }} {{ order.last_name }} </td>
<td> {{ order.user.phone_number }} </td>
<td> {{ order.payment_method }} </td>
</tr>
</tbody>
</table>
<h1 class="order-header"> Address and Delivery </h1>
<table class="table-stripped">
<thead class="title-bar">
<th> Billing Address </th>
<th> Shipping Address </th>
<th> Date and Time </th>
</thead>
<tbody>
<tr>
<td> {{ order.billing_address }} </td>
<td> {{ order.shipping_address }} </td>
<td> {{ order.delivery_datetime }} </td>
</tr>
</tbody>
</table>
<h3 class="notes order-header"> Notes: </h3>
<p> {{ order.notes }}</p>
<div class="footer">
<p> If you have any questions, contact us at care@estreetmart.sg or call us on +65 69781677 </p>
<h3 class="text-center"> Thank you for purchasing at <a href="https://www.estreetmart.sg"> https://www.estreetmart.sg </a></h3>
</div>
</div>
</div>
</body>
</html>
是否有任何技巧或库可用于在 django 中获取 pdf 输出?请任何帮助清理此 pdf 文件将不胜感激。谢谢。
解决方案
推荐阅读
- java - 在 @Query 中使用 #{principal} 会导致“在 null 上找不到属性或字段‘principal’”
- google-bigquery - BigQuery:API 查询在不使用 order by 的情况下给出有序结果
- javascript - javascript 全局多维数组
- python - 返回函数内的输入值
- python - 如何从谷歌学者网站抓取特定标签的所有子标签
- android - 如何将空活动设置为默认活动并在 Android Studio 中命名?
- java - 以下哪些字符串对象将准备好进行垃圾回收?
- python - 如何按动态列值选择行
- python - 启动器中的致命错误:无法使用“c:\python27\python.exe”创建进程
- javascript - 提示一个字符串。在数组中识别它,如果是,则返回作为该字符串的事实写入的信息