如何转换 SVG 图像以用于 Pisa / XHTML2PDF?

发布于 2024-07-18 14:37:33 字数 231 浏览 4 评论 0原文

我正在使用 Pisa/XHTML2PDF 在 Django 中动态生成 PDF。 不幸的是,我还需要包含 SVG 图像,我认为这不是一件容易的事。

a) 将 SVG 转换为 PNG / JPG(在 Python 中)或 b) 在从 Pisa 导出的 PDF 中包含 SVG 的最佳方法是什么?

I'm using Pisa/XHTML2PDF to generate PDFs on the fly in Django. Unfortunately, I need to include SVG images as well, which I don't believe is an easy task.

What's the best way to go about either a) converting the SVGs to PNG / JPG (in Python) or b) including SVGs in the PDF export from Pisa?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年不长 2024-07-25 14:37:33

有基于 Java 的 Apache Batik SVG 工具包

有关 C# 的类似问题中,建议使用 Inkscape 的命令行版本 用于此目的。

对于 Python,这里有一个来自 此讨论的有用建议线程

import rsvg
from gtk import gdk
h = rsvg.Handle('svg-file.svg')
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')

Lukasz建议的步骤from gtk import gdk是必要的并且必须先于创建 pixbuf,否则您将无法获得 save 方法,如 所观察到的原始海报

There's the Java based Apache Batik SVG toolkit.

In a similar question regarding C# it was proposed using the command line version of Inkscape for this.

For Python, here's a useful suggestion from this discussion thread:

import rsvg
from gtk import gdk
h = rsvg.Handle('svg-file.svg')
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')

the step from gtk import gdk, suggested by Lukasz, is necessary and has to precede creation of the pixbuf, otherwise you will not get the save method, as observed by the original poster.

三人与歌 2024-07-25 14:37:33

“我的 rsvg 可以工作,但是当我尝试保存时,我得到的是: AttributeError: 'gtk.gdk.Pixbuf' object has no attribute 'save' – Nick Sergeant 2009 年 4 月 25 日 0:10”

您需要导入 gdk访问 pixbuf 方法:

import rsvg
from gtk import gdk
h = rsvg.Handle('svg-file.svg')
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')

并从包含 svg 数据的字符串进行转换:

import rsvg
from gtk import gdk
h = rsvg.Handle()
h.write(svg_string)
h.close()
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')

"I got rsvg working, but here's what I get when I try to save: AttributeError: 'gtk.gdk.Pixbuf' object has no attribute 'save' – Nick Sergeant Apr 25 '09 at 0:10"

You need to import gdk to have access to pixbuf methods:

import rsvg
from gtk import gdk
h = rsvg.Handle('svg-file.svg')
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')

And to convert from string that contains svg data:

import rsvg
from gtk import gdk
h = rsvg.Handle()
h.write(svg_string)
h.close()
pixbuf = h.get_pixbuf()
pixbuf.save('foobar.png', 'png')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文