在進一步調查之後,我意識到,不應該在所有使用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。
這個工作很好,謝謝!我簡化了它,因爲我只需要gdk-pixbuf,因爲我無法在生成的文件中顯示JPEG,並且現在情況已經完美。 – capcom
非常歡迎您 –