pythondjangopdfweasyprintpylatex

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. I realized I was unable to update Cairo for weasyrprint. I would like to to change the code to some thing else. I was thinking pylatex?? This is for html to pdf. In my orders app 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

# 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

<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.

@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

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

Solution

  • I've been using xhtml2pdf for a while, and had no problems using it. You can can give it a try too!

    You can install it using the pip (Python Package Index) command:###

    pip install 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)