【问题标题】:Django Weasyprint on Elastic Beanstalk - Could not load GDK-PixbufElastic Beanstalk 上的 Django Weasyprint - 无法加载 GDK-Pixbuf
【发布时间】:2017-06-08 19:12:32
【问题描述】:

我已经在 Elastic Beanstalk 上安装了 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

【问题讨论】:

  • 注意——这个库实际上被称为gdkpixbuf-2.0,所以yum install gdkpixbuf-2.0可以工作:)

标签: django amazon-web-services amazon-elastic-beanstalk weasyprint


【解决方案1】:

我通过手动构建 gdk-pixbuf2 找到了一个对我有用的解决方案,我下面的脚本来自 https://gist.github.com/whyvez/1e0212a35da97aa8f1b1,他们需要在其中安装 image magic

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

【讨论】:

  • 效果很好,谢谢!我简化了它,因为我只需要 gdk-pixbuf,因为我无法在生成的文件中显示 JPEG,而且现在一切正常。
  • 您能帮我处理您的脚本吗?更具体地说,我需要在 lambda 函数中有 gdkPixbuf
  • 不确定 lambda。您必须阅读如何在部署包上构建依赖项
  • 我知道这有点过时了,但我希望有人能提供帮助。当我应用它时,图像会出现,这很棒。但是href链接停止工作,事实并非如此。我已尝试将各种软件包升级到最新版本,但实施失败并出现 OSError: cannot load library 'libpangocairo-1.0.so.0': /lib64/libpangoft2-1.0.so.0: undefined symbol: FcWeightFromOpenTypeDouble
【解决方案2】:

经过进一步调查,我意识到使用 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.')

如您所见,如果它是具有正确 mime 类型 pixbuf 的 png 或 svg,则根本不使用。读完这篇文章后,我意识到这一定是 svg 本身的问题。 在我的例子中,图像服务器 S3 为 svg 提供了错误的 content_type。

修复此问题后,错误不再发生,我可以使用 weasyprint 打印 SVG。

【讨论】:

    【解决方案3】:

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

    WeasyPrint==0.26
    

    (它可能适用于 0.26 之后的版本,这正是我在我的一个项目中所拥有的)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-28
      • 2020-11-25
      • 2014-08-03
      • 2015-01-25
      相关资源
      最近更新 更多