Django Weasyprint on Elastic Beanstalk -无法加载GDK-Pixbuf

bzzcjhmw  于 2022-11-19  发布在  Go
关注(0)|答案(4)|浏览(121)

我已经在弹性豆茎上安装了weasyprint。html模板的打印工作到目前为止,但我不能打印SVG图像。
Weasyprint抛出以下错误:

Failed to load image at "https://myurl/media/X247QAQ2IO.svg" (Could not load GDK-Pixbuf. PNG and SVG are the only image formats available.)

我需要gdk-pixbuf来打印SVG吗?如果需要,我该如何在Amazon Linux上安装它?

Yum does not have gdk-pixbuf2 available for installation
zkure5ic

zkure5ic1#

我找到了一个解决方案,为我的工作,通过手动构建gdk-pixbuf 2,我的脚本下面派生自https://gist.github.com/whyvez/1e0212a35da97aa8f1b1,他们需要安装图像魔术
weazy.conf

files:
 "/opt/elasticbeanstalk/hooks/appdeploy/pre/00_instal_weasyprint_prerequisites.sh":
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash

      yum install -y libxml2-devel libxslt-devel python-devel redhat-rpm-config libffi-devel cairo pango

      export PKG_CONFIG_PATH=/usr/lib64/pkgconfig:/usr/lib/pkgconfig
      export PATH=/usr/bin:$PATH
      export LDFLAGS=-L/usr/lib64:/usr/lib
      export LD_LIBRARY_PATH=/usr/lib64:/usr/lib
      export CPPFLAGS=-I/usr/include

      sudo yum-config-manager --enable epel
      sudo yum update -y
      sudo yum install -y gcc gcc-c++ glib2-devel.x86_64 libxml2-devel.x86_64 libpng-devel.x86_64 \
      libjpeg-turbo-devel.x86_64 gobject-introspection.x86_64 gobject-introspection-devel.x86_64

      wget http://ftp.gnome.org/pub/GNOME/sources/libcroco/0.6/libcroco-0.6.8.tar.xz
      tar xvfJ libcroco-0.6.8.tar.xz
      cd libcroco-0.6.8
      ./configure --prefix=/usr
      make
      sudo make install
      cd ..

      wget http://ftp.gnome.org/pub/GNOME/sources/gdk-pixbuf/2.28/gdk-pixbuf-2.28.2.tar.xz
      tar xvfJ gdk-pixbuf-2.28.2.tar.xz
      cd gdk-pixbuf-2.28.2
      ./configure --prefix=/usr --without-libtiff
      make
      sudo make install
      cd ..

      sudo yum install -y pixman-devel.x86_64 harfbuzz-devel.x86_64 freetype-devel.x86_64

      wget wget http://www.freedesktop.org/software/fontconfig/release/fontconfig-2.10.91.tar.gz
      tar xvf fontconfig-2.10.91.tar.gz
      cd fontconfig-2.10.91
      ./configure --prefix=/usr --enable-libxml2
      make
      sudo make install
      cd ..

      wget http://cairographics.org/releases/cairo-1.12.14.tar.xz
      tar xvfJ cairo-1.12.14.tar.xz
      cd cairo-1.12.14
      ./configure --prefix=/usr
      make
      sudo make install
      cd ..

      wget http://ftp.gnome.org/pub/GNOME/sources/pango/1.34/pango-1.34.1.tar.xz
      tar xvfJ pango-1.34.1.tar.xz
      cd pango-1.34.1
      ./configure --prefix=/usr
      make
      sudo make install
      cd ..

      wget http://ftp.gnome.org/pub/GNOME/sources/librsvg/2.40/librsvg-2.40.6.tar.xz
      tar xvfJ librsvg-2.40.6.tar.xz
      cd librsvg-2.40.6
      ./configure --prefix=/usr
      make
      sudo make install
      cd ..

      sudo ldconfig /usr/lib
nx7onnlm

nx7onnlm2#

经过进一步的调查,我意识到这个错误不应该发生在使用svg图像。这是weasyprint源代码的相关部分:

if mime_type == 'image/svg+xml':
        # No fallback for XML-based mimetypes as defined by MIME
        # Sniffing Standard, see https://mimesniff.spec.whatwg.org/
        image = SVGImage(string, url)
    else:
        # Try to rely on given mimetype
        try:
            if mime_type == 'image/png':
                try:
                    surface = cairocffi.ImageSurface.create_from_png(
                            BytesIO(string))
                except Exception as exception:
                        raise ImageLoadingError.from_exception(exception)
                else:
                    image = RasterImage(surface)
            else:
                image = None
        except ImageLoadingError:
            image = None

        # Relying on mimetype didn't work, give the image to GDK-Pixbuf
        if not image:
            if pixbuf is None:
                raise ImageLoadingError(
                        'Could not load GDK-Pixbuf. PNG and SVG are '
                        'the only image formats available.')

正如你所看到的,如果它是一个png或者svg,并且有正确的mime类型,pixbuf根本就不会被使用。阅读了这篇文章后,我意识到这一定是svg本身的问题。图像服务器,在我的例子中是S3,为svg提供了错误的content_type。
修复后,错误不再发生,我可以打印SVG与weasyprint。

pokxtpni

pokxtpni3#

我们也遇到了这个问题。除非你绝对需要最新版本的WeasyPrint,否则你可以在requirements.txt中将其设置为不需要gdk-pixbuf2的版本。

WeasyPrint==0.26

(It可能适用于比0.26更晚的版本,这正是我在我的一个项目中所拥有的)

ygya80vv

ygya80vv4#

我也有同样的问题,很长一段时间..修复安装weazyprint==52.5,并卸载django-weazyprint

pip install WeasyPrint==52.5

在中创建了一个配置文件,如以下主题中所述:https://gist.github.com/Abdoulrasheed/039b1f4afd4268a8832d8066881d7b23
终于起作用了

相关问题