Search code examples
pythondjangocairoweasyprint

Weaseyprint, Cairo, Dajngo on Pythonanywhere 25MAY21 can not pass a warning


Sorry I know there seems to be a lot about this topic. But I do not see a real resolution?

I am trying to place a Django ecommerce pizza shop for learning Django on the website. Locally this works great no issues. I matched my environment locally to that on the ENV for the server. I got this issue resolved locally when I updated Cairo on my computer. So the emulated server works great.

Python 3.8.0 Server Pythonanywhere

Here is the error and follow on info.

Error from error log on ther server. 2021-05-28 16:13:41,156: /home/williamc1jones/.virtualenvs/myvirtualenv/lib/python3.8/site-packages/weasyprint/document.py:35: UserWarning: There are known rendering problems and missing features with cairo < 1.15.4. WeasyPrint may work with older versions, but please read the note about the needed cairo version on the "Install" page of the documentation before reporting bugs. http://weasyprint.readthedocs.io/en/latest/install.html

views.py file in order app

import weasyprint   
from django.urls import reverse
from django.shortcuts import render, redirect
from django.contrib.admin.views.decorators import staff_member_required
from django.shortcuts import get_object_or_404
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
from cart.cart import Cart
from .models import OrderItem, Order
from .forms import OrderCreateForm
from .tasks import order_created


def order_create(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order,
                                         product=item['product'],
                                         price=item['price'],
                                         quantity=item['quantity'])
            # clear the cart
            cart.clear()
            # launch asynchronous task
            order_created.delay(order.id)
            # set the order in the session
            request.session['order_id'] = order.id
            # redirect for payment
            return redirect(reverse('payment:process'))
    else:
        form = OrderCreateForm()
    return render(request,
                  'orders/order/create.html',
                  {'cart': cart, 'form': form})


@staff_member_required
def admin_order_detail(request, order_id):
    order = get_object_or_404(Order, id=order_id)
    return render(request,
                  'admin/orders/order/detail.html',
                  {'order': order})


@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

A pip freeze shot of the env

-f /usr/share/pip-wheels
amqp==2.6.1
appdirs==1.4.4
billiard==3.6.4.0
braintree==3.59.0
cairocffi==1.2.0
CairoSVG==2.5.2
celery==4.4.2
certifi==2020.12.5
cffi==1.14.5
chardet==4.0.0
click==8.0.1
cssselect2==0.4.1
defusedxml==0.7.1
distlib==0.3.1
Django==2.1.15
django-pyodbc-azure-2019==2.1.0.0
filelock==3.0.12
Flask==2.0.1
Flask-WeasyPrint==0.6
html5lib==1.1
idna==2.10
itsdangerous==2.0.1
Jinja2==3.0.1
kombu==4.6.11
MarkupSafe==2.0.1
pbr==5.5.1
pdfrw==0.4
Pillow==8.2.0
pycparser==2.20
pyodbc==4.0.30
Pyphen==0.10.0
pyth==0.6.0
pytz==2021.1
requests==2.25.1
six==1.15.0
stevedore==3.3.0
tinycss2==1.1.0
urllib3==1.26.4
vine==1.3.0
virtualenv==20.4.4
virtualenv-clone==0.5.4
virtualenvwrapper==4.8.4
wcwidth==0.2.5
WeasyPrint==52
webencodings==0.5.1
Werkzeug==2.0.1
whitenoise==5.2.0

My website is tested at http://williamc1jones.pythonanywhere.com/

this is one of the weird times that the warning is an error. I know this because I had the exact same issue on my local computer. When I updated Cairo on my computer, the warning went away, and then it worked correctly. As of right now, though you wont see it in the error log. It gets stuck in a loop, and won't bypass to the credit card input on the next screen.

I also asked on https://www.pythonanywhere.com/forums/topic/29823/#id_post_94745.

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

  • Yes I wanted to thank everyone for their help. While I have a time lime for my project I will dit the post to see my work around as well. Thanks.