This is my view:
from django.conf import settings
from django.http import HttpResponse
from django.template.loader import render_to_string
import weasyprint
@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'] = 'filename="order_{}.pdf"'.format(order.id)
weasyprint.HTML(string=html).write_pdf(response,
stylesheets=[weasyprint.CSS(settings.STATIC_ROOT + 'css/pdf.css')])
return response
when i want to import (import weasyprint) it gives me a error.
ERROR:
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/orders/order/
Django Version: 1.8.6
Exception Type: OSError
Exception Value:
dlopen() failed to load a library: cairo / cairo-2
I already installed weasyprint and cairocffi. I'm using osx El Capitan.
This was solved for me by installing the following dependencies on Ubuntu:
sudo apt-get install libpango1.0-0
sudo apt-get install libcairo2
sudo apt-get install libpq-dev
Check out the dependencies at the link:
http://weasyprint.readthedocs.io/en/latest/install.html
I also had the same issue on a fresh installation of weasyprint on OSX EL CAPITAN.
This is how I solved it.
Firstly, cairo was not found by when installed via pip, so I tried installing it via homebrew using the following command
brew install cairo pango gdk-pixbuf libxml2 libxslt libffi
Once this is done, I tried to find out the path of cairo installation. For my case, the location was /usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/ I just exported this to my DYLD library path
export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/
Then I uninstalled and installed weasyprint again
pip uninstall weasyprint
pip install weasyprint
Post that, I tried to run weasyprint, but got a new error
Traceback (most recent call last):
File "/Users/anurag/VirtualEnvs/test/bin/weasyprint", line 11, in <module>
load_entry_point('WeasyPrint==0.31', 'console_scripts', 'weasyprint')()
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 565, in load_entry_point
return get_distribution(dist).load_entry_point(group, name)
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2598, in load_entry_point
return ep.load()
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2258, in load
return self.resolve()
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/pkg_resources/__init__.py", line 2264, in resolve
module = __import__(self.module_name, fromlist=['__name__'], level=0)
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/__init__.py", line 338, in <module>
from .css import PARSER, preprocess_stylesheet # noqa
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/__init__.py", line 30, in <module>
from . import computed_values
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/css/computed_values.py", line 18, in <module>
from .. import text
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/text.py", line 216, in <module>
'libgobject-2.0.dylib')
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/weasyprint/text.py", line 212, in dlopen
return ffi.dlopen(names[0]) # pragma: no cover
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 139, in dlopen
lib, function_cache = _make_ffi_library(self, name, flags)
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 770, in _make_ffi_library
backendlib = _load_backend_lib(backend, libname, flags)
File "/Users/anurag/VirtualEnvs/test/lib/python2.7/site-packages/cffi/api.py", line 759, in _load_backend_lib
return backend.load_library(name, flags)
OSError: cannot load library gobject-2.0: dlopen(gobject-2.0, 2): image not found
I tried to find out the location of object library. Found it in /opt/local/lib and set the fallback library path
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
After that, I tried running weasyprint again, and it worked
(test)anurag-mac:~ anurag$ weasyprint --version
WeasyPrint version 0.31
I hope someone else also finds it useful.
UPDATE-1
Although the above method worked, MySQL python started giving errors because of this and culprit was defining a fallback library path. So I removed this line
export DYLD_FALLBACK_LIBRARY_PATH=/opt/local/lib
which gave me the gobject error again, then I tried finding the location of its installation and appended to the DYLD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/:/usr/local/homebrew/Cellar/glib/2.48.2/lib/
After doing that, I got a similar error for pango. After correcting all the errors, this is the final library path which worked
export DYLD_LIBRARY_PATH=/usr/local/homebrew/Cellar/cairo/1.14.6_1/lib/:/usr/local/homebrew/Cellar/glib/2.48.2/lib/:/usr/local/homebrew/Cellar/pango/1.40.3/lib/
I had the same error with boxes.py on macOS Mojave. My solution was to install cairocffi with pip3 and cairo with brew. The two commands fail on their own, but together they work for boxes.py:
pip3 install cairocffi
brew install cairo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With