那么不能使用 weasyprint 最好的办法是什么?
So can't use weasyprint what is the best thing to do?
提前致谢。
我正在尝试将 django 项目加载到服务器上。我意识到我无法为 weasyrprint 更新开罗。我想将代码更改为其他内容。我在想pylatex??这是 html 到 pdf 的。
在我的订单应用中 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
在我的付款中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')
终于在我的订单应用中了 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>
在 Luis 的帮助下,这就是我的想法。它将无法加载,但我可以创建测试 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
我的解决方法:
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
我已经使用 xhtml2pdf 一段时间了,使用起来没有任何问题。你也可以试试看!
您可以使用 pip(Python 包索引)命令安装它:###
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)
提前致谢。 我正在尝试将 django 项目加载到服务器上。我意识到我无法为 weasyrprint 更新开罗。我想将代码更改为其他内容。我在想pylatex??这是 html 到 pdf 的。 在我的订单应用中 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
在我的付款中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')
终于在我的订单应用中了 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>
在 Luis 的帮助下,这就是我的想法。它将无法加载,但我可以创建测试 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
我的解决方法:
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
我已经使用 xhtml2pdf 一段时间了,使用起来没有任何问题。你也可以试试看!
您可以使用 pip(Python 包索引)命令安装它:###
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)