【问题标题】:Embed .SVG files into PDF using reportlab使用 reportlab 将 .SVG 文件嵌入 PDF
【发布时间】:2013-05-07 00:11:42
【问题描述】:

我在 python 中编写了一个脚本,该脚本生成 matplotlib 图形并使用 reportlab 将它们放入 pdf 报告中。

我在将 SVG 图像文件嵌入我的 PDF 文件时遇到了困难。我在使用 PNG 图像时没有遇到任何问题,但我想使用 SVG 格式,因为这样可以在 PDF 报告中生成质量更好的图像。

这是我收到的错误消息:

IOError: cannot identify image file

有没有人有建议或者你以前解决过这个问题吗?

【问题讨论】:

标签: python pdf svg matplotlib reportlab


【解决方案1】:

昨天我成功使用 svglib 将 SVG Image 添加为 reportlab Flowable。

所以这个绘图是reportlab绘图的一个实例,见这里:

from reportlab.graphics.shapes import Drawing

reportlab Drawing 继承 Flowable:

from reportlab.platypus import Flowable

这是一个最小的示例,它还展示了如何正确缩放它(您必须只指定路径和因子):

from svglib.svglib import svg2rlg
drawing = svg2rlg(path)
sx = sy = factor
drawing.width, drawing.height = drawing.minWidth() * sx, drawing.height * sy
drawing.scale(sx, sy)
#if you want to see the box around the image
drawing._showBoundary = True

【讨论】:

  • 是的,这个例子缺乏 Don Kirkby 所说的完整运行,但我已经编写了一个名为 autobasedoc 的 pip 包,它几乎抽象了所有内容,您只需在 matplotlib 函数前面放置一个装饰器.似乎没有人使用过它,我想知道为什么......
  • 完整示例请参见此处:stackoverflow.com/questions/32608199/…
【解决方案2】:

正如skidzo 所提到的,您完全可以使用svglib 包来做到这一点,您可以在这里找到:https://pypi.python.org/pypi/svglib/

根据该网站,Svglib 是一个纯 Python 库,用于读取 SVG 文件并使用 ReportLab 开源工具包将它们(在合理的程度上)转换为其他格式。

你可以使用pip来安装svglib。

这是一个完整的示例脚本:

# svg_demo.py

from reportlab.graphics import renderPDF, renderPM
from reportlab.platypus import SimpleDocTemplate
from svglib.svglib import svg2rlg


def svg_demo(image_path, output_path):
    drawing = svg2rlg(image_path)
    renderPDF.drawToFile(drawing, output_path)

if __name__ == '__main__':
    svg_demo('/path/to/image.svg', 'svg_demo.pdf')

【讨论】:

  • 如果您只是安装 svglib,您将获得一个不错的命令行工具 svg2pdf,您可以使用它从 shell 脚本中执行此操作。太棒了!
【解决方案3】:

skidzo's answer 非常有用,但不是如何在 reportlab PDF 中将 SVG 文件用作可流动文件的完整示例。希望这对试图弄清楚最后几个步骤的其他人有所帮助:

from io import BytesIO

import matplotlib.pyplot as plt
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import SimpleDocTemplate, Paragraph
from svglib.svglib import svg2rlg


def plot_data(data):
    # Plot the data using matplotlib.
    plt.plot(data)

    # Save the figure to SVG format in memory.
    svg_file = BytesIO()
    plt.savefig(svg_file, format='SVG')

    # Rewind the file for reading, and convert to a Drawing.
    svg_file.seek(0)
    drawing = svg2rlg(svg_file)

    # Scale the Drawing.
    scale = 0.75
    drawing.scale(scale, scale)
    drawing.width *= scale
    drawing.height *= scale

    return drawing


def main():
    styles = getSampleStyleSheet()
    pdf_path = 'sketch.pdf'
    doc = SimpleDocTemplate(pdf_path)

    data = [1, 3, 2]
    story = [Paragraph('Lorem ipsum!', styles['Normal']),
             plot_data(data),
             Paragraph('Dolores sit amet.', styles['Normal'])]

    doc.build(story)


main()

【讨论】:

    【解决方案4】:

    您需要确保在代码中导入 PIL(Python 图像库),以便 ReportLab 可以使用它来处理 SVG 等图像类型。否则只能支持几种基本的图片格式。

    也就是说,我记得在使用 PIL 和矢量图形时遇到了一些麻烦。我不知道我是否尝试过 SVG,但我记得在使用 EPS 时遇到了很多麻烦。

    【讨论】:

      猜你喜欢
      • 2011-08-20
      • 2012-02-09
      • 2010-12-19
      • 2013-01-30
      • 2011-12-14
      • 2017-04-09
      • 2011-10-06
      • 2013-10-17
      • 2017-08-13
      相关资源
      最近更新 更多