Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created July 27, 2012 14:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save doobeh/3188318 to your computer and use it in GitHub Desktop.
Save doobeh/3188318 to your computer and use it in GitHub Desktop.
Installing WeasyPrint on Windows

Installing WeasyPrint on Windows

WeasyPrint converts HTML including images to PDF, it's cross platform but Windows requires a decent amount of massaging to persuade it to work.

To install Pango and Cairo download the all in one bundle of the GTK+ stack and extract the archive to C:\GTK.

You'll need to add the GTK bin folder to your system path so the various libraries can be found when requested by WeasyPrint, to do that go to the 'System' in the Control Panel, then open 'Advanced System Settings' then 'Environment Variables' and edit the System variable called Path by adding ;c:\gtk to the end of the current value.

With that out of the way, we need to install a few Python libraries that WeasyPrint uses to interface with the GTK stack. The easiest way is from precompiled binaries, so head over to Christoph Gohkle's Python Packages and download and install Py2Cairo, PyGObject and PyGTK for your version of Python (I've tested with 32bit 2.7). You'll also need to install the lxml package he provides.

Now you should be able to just pip install WeasyPrint

@fintrader
Copy link

I had to add C:\gtk\bin to my path

Thanks for the guide!

@fintrader
Copy link

For 64bit compatibility do the following:

  1. Use this .exe installer for the GTK+ stack: http://tschoonj.github.io/blog/2014/02/08/gtk2-64-bit-windows-runtime-environment-installer/ and install it to the C:\gtk directory. Afterward, confirm the C:\gtk\bin is in your PATH environment variable.

  2. Use the 64-bit pycairo, pygobject and pygtk binaries http://www.lfd.uci.edu/~gohlke/pythonlibs/#pygtk

You're all good to go!

@matt-dale
Copy link

Thank you for this. It made my installation so smooth.

@quevon24
Copy link

in windows 10 i have this error: dlopen() failed to load a library: cairo / cairo-2

how can i fix that? Thanks

@pseudobabble
Copy link

pseudobabble commented Oct 6, 2016

I still have this issue. I have followed instructions here and from the linked SO page.

Everything installed fine, just cannot import weasyprint without getting the dreaded dlopen() error :/

I'm thinking its something to do with my path, but everything there seems fine too. GTK+2/bin is on the path, as instructed.

EDIT 1:
Very strange. I can import cairo no problem. But using ctypes.util doesn't find it. Hmmm.
In the end I commented out the dlopen() function, and replaced it with import cairo. Now, upon trying import weasyprint I get a different error:

Traceback (most recent call last):

  File "<ipython-input-6-fd6b8b8a57f1>", line 1, in <module>
    import weasyprint

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\__init__.py", line 336, in <module>
    from .css import PARSER, preprocess_stylesheet  # noqa

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\css\__init__.py", line 29, in <module>
    from . import properties

ImportError: cannot import name properties

But properties.py is right there... Definitely something wrong with the path.Still investigating. I'm new to all this, but it turns out debugging is fun!

EDIT 2:
Now, without having touched anything, a new errror:

Traceback (most recent call last):

  File "<ipython-input-1-fd6b8b8a57f1>", line 1, in <module>
    import weasyprint

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\__init__.py", line 336, in <module>
    from .css import PARSER, preprocess_stylesheet  # noqa

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\css\__init__.py", line 30, in <module>
    from . import computed_values

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\css\computed_values.py", line 18, in <module>
    from .. import text

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\text.py", line 208, in <module>
    'libpangocairo-1.0.so', 'libpangocairo-1.0.dylib')

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\weasyprint\text.py", line 200, in dlopen
    return ffi.dlopen(names[0])  # pragma: no cover

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\cffi\api.py", line 139, in dlopen
    lib, function_cache = _make_ffi_library(self, name, flags)

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\cffi\api.py", line 769, in _make_ffi_library
    backendlib = _load_backend_lib(backend, libname, flags)

  File "C:\Users\hjohnson\AppData\Local\Continuum\Anaconda2\lib\site-packages\cffi\api.py", line 758, in _load_backend_lib
    return backend.load_library(name, flags)

OSError: cannot load library pangocairo-1.0: error 0x7e

Investigating.

@MohnishMagesh
Copy link

Once installation is complete, where should I save these files?
Py2Cairo, PyGObject and PyGTK

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment