简体   繁体   English

那么不能使用 weasyprint 怎么办呢?

[英]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!你也可以试一试!

You can install it using the pip (Python Package Index) command:###您可以使用 pip(Python Package 索引)命令安装它:###

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.

相关问题 在精灵板上分离图像的最佳方法是什么? - What is the best thing to use to seperate images on sprite sheets? 以下“删除列表中的重复项”代码中的问题是什么。 我知道我们可以使用 append() 来做同样的事情 - What is the problem in the below code for "removing duplicates in list". I know we can use append() to do the same thing 我大学的集群无法连接到互联网,因此我无法使用conda来使用python 2.7创建环境。 我还可以做些什么? - My university's cluster doesn't connect to the internet, so I can't use conda to create an environment with python 2.7. What else can I do? SLIM对python最接近的是什么? /使用EMACS的python的最佳方法是什么? - What's the closest thing to SLIME for python ? / What's the best way to use python from EMACS? 如何将计算结果存储在 Python 中,这样我的程序就不会对同一事物进行两次计算? - How do I store results of a computation in Python so my program doesn't compute the same thing twice? 为什么赋值会默默地失败或做错事,我该如何阻止它这样做 - Why does value assignment silently fail or do the wrong thing and how can I stop it from doing so 更新到最新的ubuntu已成功完成,因此我无法访问mysql……我该怎么办? - updating to newest ubuntu has made it so I can't access mysql…what do I do? 编译器找不到Py_InitModule()..它是否已被弃用,如果是这样,我应该使用什么? - Compiler can't find Py_InitModule() .. is it deprecated and if so what should I use? 我应该怎么做才能使用字典映射 filter() function 结果? - What should I do so that I can use a dictionary mapping the filter() function result? Python:阻塞线程以使其可以被中断的最佳方法是什么? - Python: What's the best way to block a thread so that it can be interrupted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM