11

I installed the package weasyprint according to the instructions Installing weasyprint (Django project). My system: win 10. I have installed gtk3 and it is present in my PATH PAYH gtk3

import weasyprint
...
@staff_member_required
def order_admin_pdf(request, order_id):
    # Получаем заказ по ID:
    order = get_object_or_404(Order, id=order_id)
    # Передаем объект в функцию render_to через генерацию шаблона pdf.html HTML в виде строки:
    html = render_to_string('shop/orders/order_admin_pdf.html',
                            {'order': order})
    # Создаем объект овтета с типом содержимого application/pdf и заголовком Content-Disposition:
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'filename=order_{}.pdf"'.format(order.id)
    # Вызов метода weasyprint для получения PDF документа:
    weasyprint.HTML(string=html).write_pdf(response,
                                           stylesheets=[weasyprint.CSS(
                                               settings.STATIC_ROOT + 'css/pdf.css')])
    return response

OSError: cannot load library 'gobject-2.0': error 0x7e. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0'

Echo Foe
  • 398
  • 1
  • 2
  • 16

5 Answers5

9

Starting from Python 3.8 DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.

If you followed the installation guide from the official documentation then the following example works.

import os

os.add_dll_directory(r"C:\Program Files\GTK3-Runtime Win64\bin")

from weasyprint import HTML

HTML('https://weasyprint.org/').write_pdf('weasyprint-website.pdf')

In essence you need to call add_dll_directory() before interacting with WeasyPrint.

Martin
  • 2,135
  • 8
  • 39
  • 42
  • The question was asked a long time ago and since then a lot has changed in my understanding) This answer is more suitable for solving this issue. – Echo Foe Nov 03 '21 at 06:21
  • Thanks. I was facing similar issue – boyenec Jan 03 '22 at 15:13
  • Same issues, I wish there was a way to have the DLLs in a sub dir of my project and only use those I need without a full GTK3 install. I know this is impossible but makes the distribution of the program difficult. –  Jun 30 '23 at 05:36
5

I got desperate and decided to install the library gtk2 C:\Program Files (x86)\GTK2\lib\ and specify the first in the PATH list. It worked... But my OS - win 10 x64. Why the library GTK3 refused to work, I do not know.

Echo Foe
  • 398
  • 1
  • 2
  • 16
1

For me this was about env variables not set correctly.

I had to add GTK3 as a System env variable in Windows for my project to work (Windows 10).

secavfr
  • 628
  • 1
  • 9
  • 24
1

For MacOS this helped. Bascially you have to create few links.

sudo ln -s /opt/homebrew/opt/glib/lib/libgobject-2.0.0.dylib /usr/local/lib/gobject-2.0
sudo ln -s /opt/homebrew/opt/pango/lib/libpango-1.0.dylib /usr/local/lib/pango-1.0
sudo ln -s /opt/homebrew/opt/harfbuzz/lib/libharfbuzz.dylib /usr/local/lib/harfbuzz
sudo ln -s /opt/homebrew/opt/fontconfig/lib/libfontconfig.1.dylib /usr/local/lib/fontconfig-1
sudo ln -s /opt/homebrew/opt/pango/lib/libpangoft2-1.0.dylib /usr/local/lib/pangoft2-1.0

https://github.com/Kozea/WeasyPrint/issues/1556#issuecomment-1097977671

Aleem
  • 549
  • 6
  • 13
0

I made a django website for windows 10 with the weasyprint library. Also installed GTK+ for Windows Runtime Environment (version 3). The same errors were thrown.

I tried to swap paths in path, updated windows to 11, but it didn't help. Then i pasted 'sitecustomize.py' like Tontyna https://github.com/Kozea/WeasyPrint/issues/971#issuecomment-544195744 to the virtual environment folder '/Lib/site-packages',

another error occurred: Fontconfig error: Cannot load default config file.

It turned out that the 'C:\Users<User>\anaconda3\fontconfig.dll' library is being loaded.

In the file 'sitecustomize.py' I changed the line: os.environ['PATH'] = GTK_FOLDER + os.pathsep + os.environ.get('PATH', '') to the line: os.environ['PATH'] = GTK_FOLDER and it WORKS