【问题标题】:Reportlab [ Platypus ] - Image does not renderReportlab [鸭嘴兽] - 图像不呈现
【发布时间】:2012-10-12 16:43:36
【问题描述】:

我正在编写一份包含表格和图像混合的报告。图像 [ 实际上是图形 ] 以 .png 格式保存到文件系统中。

实际渲染PDF的方法是:

def _render_report(report_data):
    file_name = get_file_name() # generate random filename for report
    rpt = Report(settings.MEDIA_ROOT + os.sep + file_name)
    Story = []    
    for (an, sam, event), props  in report_data.iteritems():
        Story.append(Paragraph("%s - sample %s results for %s" % (an.name, sam.name, event.name), styles["Heading2"]))
        data_list = [['Lab', 'Method', 'Instrument', 'Unit', 'Raw Result', 'Converted Result', 'Outlier']]
        for (index, series) in props['frame'].iterrows():
            data_list.append(_format([
                Paragraph(Lab.objects.get(pk=series['labs']).name, styles['BodyText']),
                Paragraph(Method.objects.get(pk=series['methods']).name, styles['BodyText']),
                Paragraph(Instrument.objects.get(pk=series['instruments']).name, styles['BodyText']),
                Paragraph(Unit.objects.get(pk=series['units']).name, styles['BodyText']),
                series['raw_results'],
                series['results'],
                series['outlier']
            ]))
        table = Table(data_list, colWidths=[45 * mm, 35 * mm, 35 * mm, 25 * mm, 35 * mm, 35 * mm, 35 * mm], repeatRows=1)
        Story.append(table)
        Story.append(PageBreak())

        if props['graph'] is not None:
            Story.append(Image("/tmp/%s" % props['graph'], width=10 * inch, height=6 * inch))
            Story.append(PageBreak())
    rpt.draw(Story, onFirstPage=setup_header_and_footer, onLaterPages=setup_header_and_footer)
    return file_name

背景信息

  • 页面设置为 A4,横向
  • 我的开发环境是virtualenv; PIL 1.1.7 和 reportlab 2.6 已安装并正常运行
  • 上面使用的“Report”类只是SimpleDocTemplate 的简单包装器,它设置了一些默认值,但委托给SimpleDocTemplate 的build 实现。它的代码是:

    class Report(object):
        def __init__(self, filename, doctitle="Report", docauthor="<default>",
            docsubject="<default>", doccreator="<default>", orientation="landscape", size=A4):
            DEFAULTS = {
                'leftMargin' : 10 * mm,
                'rightMargin' : 10 * mm,
                'bottomMargin' : 15 * mm,
                'topMargin' : 36 * mm,
                'pagesize' : landscape(size) if orientation == "landscape" else portrait(size),
                'title' : doctitle,
                'author' : docauthor,
                'subject' : docsubject,
                'creator' : doccreator
            }
            self.doc = SimpleDocTemplate(filename, **DEFAULTS)
    
        def draw(self, flowables, onFirstPage=setup_header_and_footer, onLaterPages=setup_header_and_footer):
            self.doc.build(flowables, onFirstPage=setup_header_and_footer,
                onLaterPages=setup_header_and_footer, canvasmaker=NumberedCanvas)
    

我已经看过的内容

  • 我已确认图像存在于磁盘上。这些路径是完全限定的路径。
  • PIL 已安装,并且能够正确读取图像
  • 分配给图像的空间足够;我已经通过计算证实了这一点。此外,如果我增加图像大小,ReportLab 会抱怨 Image Flowable 太大。当前尺寸应该适合。
  • 我已经测试过分页符和不分页符;他们似乎没有任何区别

问题

表格、标题和页面模板呈现正常,但图像为空白。今天早些时候,我遇到了this issue [在设置此报告使用的模板时]。解决方法是使用 canvas.drawInlineImage(... 代替 canvas.DrawImage(... 。因此,我的设置似乎存在问题;我可以使用一些关于如何调试它的指针。

更新

我能够应用在this linked question 中使用的相同解决方法的变体(使用canvas.drawInlineImage 代替canvas.drawImage。我将“Image”子类化如下:

class CustomImage(Image):
"""
Override - to use inline image instead; inexplicable bug with non inline images
"""
def draw(self):
    lazy = self._lazy
    if lazy>=2: self._lazy = 1
    self.canv.drawInlineImage(self.filename,
        getattr(self,'_offs_x',0),
        getattr(self,'_offs_y',0),
        self.drawWidth,
        self.drawHeight
    )
    if lazy>=2:
        self._img = None
        self._lazy = lazy

“库存”图像类的唯一变化是在一行中 - 使用 self.canv.drawInlineImage 之前有 self.canvas.drawImage。 从图像最终在我的 PDF 中可见的意义上说,这“有效”。 drawImage 不起作用的原因仍然是个谜。

我已经尝试过@PedroRomano 的建议(以确保图像 RGBA),甚至尝试使用 JPEG 图像而不是 PNG。这些并没有什么不同。

【问题讨论】:

  • 这可能是问题所在:png mode "I" image inserted using drawImage but "blank" in resulting pdf?显然 ReportLab 只支持 "rgb (rgba with some image types)".
  • @PedroRomano - 我已经对处于 RGBA 模式的 PNG(使用 PIL 进行转换)和 JPG 进行了测试。没有成功。
  • drawImagedrawInlineImage 之间的唯一区别是图像在 PDF 中的嵌入方式。见reportlab.com/apis/reportlab/2.4/…会不会是你没有正确关闭报表,导致外部图片还没有添加?
  • @RolandSmith 谢谢;我考虑过 - 但是,在这种情况下,我使用的是 ReportLab 的鸭嘴兽 (reportlab.com/apis/reportlab/dev/platypus.html) 组件。 Platypus 仍然在内部委托给画布方法(例如 drawImage );但是,根据我对文档的阅读,当我在文档模板上调用 build() 时,我不应该关闭 PDF。事实上,SimpleDocTemplate 并没有暴露文档画布,因此没有简单的方法可以手动关闭 PDF

标签: python reportlab


【解决方案1】:

我最终通过使用自定义 Image 子类结束了这件事:

class CustomImage(Image):
"""
Override - to use inline image instead; inexplicable bug with non inline images
"""
def draw(self):
    lazy = self._lazy
    if lazy>=2: self._lazy = 1
    self.canv.drawInlineImage(self.filename,
        getattr(self,'_offs_x',0),
        getattr(self,'_offs_y',0),
        self.drawWidth,
        self.drawHeight
    )
    if lazy>=2:
        self._img = None
        self._lazy = lazy

以矢量图形格式(例如 EPS)保存图形、保存为 JPEG、保存为带有和不带有 Alpha 通道的 PNG 似乎都没有什么不同。

【讨论】:

    【解决方案2】:

    最佳解决方案是以矢量格式生成图表,如 reportlab supported 的 postscript。很多 UNIX 软件开箱即用,您可以在 Windows 上使用出色的 PDFCreator

    如果您的图表必须使用光栅图像,请尝试将图像转换为 JPEG 格式。这些可以使用DCTDecode 过滤器轻松嵌入到 PDF 文件中。 (例如jpeg2pdfdoes。)

    【讨论】:

    • 这个报告过程是一个更大的 Python 应用程序的一部分,它需要在 Python 中(使用 numpy,pandas 相当多)。我已经考虑过“桥接”到另一个报告解决方案 [我在 JasperReports 方面有很多经验],但我将其保留为我的“最后一搏”选择。但是,我将评估/编写基于矢量的解决方案,看看是否可行。谢谢。
    • 如果您使用 matplotlib 生成图形,您可以使用 matplotlib.pyplot.savefigformat 关键字参数将它们保存为矢量格式(例如 PDF)而不是 PNG。见matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig
    • 我将编写一个以 EPS 格式生成图形的解决方案,看看效果如何。
    • 我将图表渲染为 EPS,但它们仍然没有显示在 PDF 中。唯一似乎全面有效的“解决方案”是使用canvas.drawInlineImage 的黑客攻击。
    • 向 ReportLab 人员发送错误报告可能是个好主意。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多