2017-01-24 42 views
0

我已經在Elastic Beanstalk上安裝了weasyprint。 HTML模板的印刷工作到目前爲止,但不能打印SVG圖像。Elastic Beanstalk上的Django Weasyprint - 無法加載GDK-Pixbuf

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的打印SVGs?如果是這樣,我該如何在Amazon Linux上安裝它?

Yum does not have gdk-pixbuf2 available for installation 

回答

0

在進一步調查之後,我意識到,不應該在所有使用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服務於錯誤的content_type的svg。

修復此錯誤後,不再發生錯誤,我可以用weasyprint打印SVG。

0

我們也遇到了這個問題。除非您絕對需要最新版本的WeasyPrint,否則您可以將其設置爲requirements.txt中不需要gdk-pixbuf2的版本。

WeasyPrint==0.26 

(它可能比0.26以後verison工作,這正是我在我的項目之一)

1

我發現,通過人工建造GDK-pixbuf2爲我工作的解決方案,我下面的腳本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 
+0

這個工作很好,謝謝!我簡化了它,因爲我只需要gdk-pixbuf,因爲我無法在生成的文件中顯示JPEG,並且現在情況已經完美。 – capcom

+0

非常歡迎您 –