[英]So can't use weasyprint what is the best thing to do?
Thanks in advance.提前致谢。 I am trying to load a django project onto a server.
我正在尝试将 django 项目加载到服务器上。 I realized I was unable to update Cairo for weasyrprint.
我意识到我无法为 weasyrprint 更新 Cairo。 I would like to to change the code to some thing else.
我想将代码更改为其他内容。 I was thinking pylatex??
我在想pylatex?? This is for html to pdf.
这适用于 html 到 pdf。 In my orders app views.py
在我的订单应用views.py
@staff_member_required
def admin_order_pdf(request, order_id):
order = get_object_or_404(Order, id=order_id)
html = render_to_string('orders/order/pdf.html',
{'order': order})
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
weasyprint.HTML(string=html).write_pdf(response,
stylesheets=[weasyprint.CSS(
settings.STATIC_ROOT + 'css/pdf.css')])
return response
In my payment tasks.py在我的付款tasks.py
# generate PDF
html = render_to_string('orders/order/pdf.html', {'order': order})
out = BytesIO()
stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')]
weasyprint.HTML(string=html).write_pdf(out,
stylesheets=stylesheets)
# attach PDF file
email.attach(f'order_{order.id}.pdf',
out.getvalue(),
'application/pdf')
Finally in my orders app pdf.html最后在我的订单应用程序 pdf.html
<html>
<body>
<h1>Mom and Pops</h1>
<p>
Invoice no. {{ order.id }}</br>
<span class="secondary">
{{ order.created|date:"M d, Y" }}
</span>
</p>
<h3>Bill to</h3>
<p>
{{ order.first_name }} {{ order.last_name }}<br>
{{ order.email }}<br>
{{ order.address }}<br>
{{ order.postal_code }}, {{ order.city }}
</p>
<h3>Items bought</h3>
<table>
<thead>
<tr>
<th>Product</th>
<th>Price</th>
<th>Quantity</th>
<th>Cost</th>
</tr>
</thead>
<tbody>
{% for item in order.items.all %}
<tr class="row{% cycle "1" "2" %}">
<td>{{ item.product.name }}</td>
<td class="num">${{ item.price }}</td>
<td class="num">{{ item.quantity }}</td>
<td class="num">${{ item.get_cost }}</td>
</tr>
{% endfor %}
<tr class="total">
<td colspan="3">Total</td>
<td class="num">${{ order.get_total_cost }}</td>
</tr>
</tbody>
</table>
<span class="{% if order.paid %}paid{% else %}pending{% endif %}">
{% if order.paid %}Paid{% else %}Pending payment{% endif %}
</span>
</body>
</html>
Upon help of Luis this is what I came up with.在路易斯的帮助下,这就是我想出的。 it will fail to load but I can create the test pdf which means that it is something in the script.
它将无法加载,但我可以创建测试 pdf 这意味着它在脚本中。
@staff_member_required
def admin_order_pdf(request, order_id):
template_path = 'orders/order/pdf.html'
order = get_object_or_404(Order, id=order_id)
pdf = open('order.pdf', "w+b")
context = {'order': order}
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
template = get_template(template_path)
source_html = render_to_string('orders/order/pdf.html', context)
pisa_status = pisa.CreatePDF(source_html, dest=pdf)
if pisa_status.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response
My work around:我的工作:
views.py in orders app订单应用中的views.py
import xhtml2pdf
from xhtml2pdf import pisa
def admin_order_pdf(request, order_id):
template_path = 'orders/order/pdf.html'
order = get_object_or_404(Order, id=order_id)
context = {'order': order}
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = f'filename=order_{order.id}.pdf'
template = get_template(template_path)
html = render_to_string('orders/order/pdf.html', context)
pdf = open('order.pdf', "w+b")
#creating the pdf
pisa_status = pisa.CreatePDF(html, dest=response)
if pisa_status.err:
return HttpResponse('We had some errors <pre>' + html + '</pre>')
return response
I've been using xhtml2pdf for a while, and had no problems using it.我已经使用 xhtml2pdf 有一段时间了,使用它没有任何问题。 You can can give it a try too!
你也可以试一试!
pip install xhtml2pdf pip 安装 xhtml2pdf
@staff_member_required
def admin_order_pdf(request, order_id):
order = get_object_or_404(Order, id=order_id)
pdf = open('order.pdf', "w+b")
context = {'order': order}
source_html = render_to_string('pdf.html', context)
pisa.CreatePDF(source_html, dest=pdf)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.