3

I am working on a report that includes a mixture of tables and images. The images [ graphs, actually ] are saved to the filesystem in .png form.

The method that actually renders the PDF is:

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

Background Information

  • The page is set up as an A4, in landscape orientation
  • My development environment is a virtualenv; PIL 1.1.7 and reportlab 2.6 are installed and functional
  • The "Report" class used above is simply a thin wrapper around SimpleDocTemplate that sets up some defaults but delegates to SimpleDocTemplate's build implementation. Its code is:

    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)
    

What I have already looked at

  • I have confirmed that the images exist on disk. The paths are fully qualified paths.
  • PIL is installed, and is able to read the images correctly
  • The space assigned to the image is adequate; I have confirmed this by calculation. Also, if I increase the image size, ReportLab complains about the Image Flowable being too large. The current dimension should fit.
  • I have tested with and without the page breaks; they do not seem to make any difference

The Problem

The table, headings and page templates render OK but the images are blank. Earlier today, I had encountered this issue [ when setting up the templates used by this report ]. The workaround was to use canvas.drawInlineImage(... in place of canvas.DrawImage(... . It therefore looks as though there is an issue with my setup; I could use some pointers on how to debug it.

Update

I was able to apply a variant of the same workaround used in this linked question ( use canvas.drawInlineImage in place of canvas.drawImage. I subclassed `Image' as follows:

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

The only change from the "stock" Image class is in one line - using self.canv.drawInlineImage where there was self.canvas.drawImage before. This "works" in the sense that the images are finally visible in my PDF. The reason why drawImage is not working still remains a mystery.

I have tried @PedroRomano's suggestion ( to make sure the images RGBA ), and even tried JPEG images instead of PNGs. These did not make a difference.

Community
  • 1
  • 1
Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
  • 1
    Could this be the problem: [png mode "I" image inserted using drawImage but "blank" in resulting pdf](http://two.pairlist.net/pipermail/reportlab-users/2011-February/009930.html)? Apparently ReportLab only supports _"rgb (rgba with some image types)"_. – Pedro Romano Oct 22 '12 at 20:05
  • @PedroRomano - I have run tests with PNGs that are in RGBA mode ( using PIL for conversion ) and with JPGs. No success. – Ngure Nyaga Oct 23 '12 at 06:15
  • The only difference between `drawImage` and `drawInlineImage` is how the image is embedded in the PDF. See http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html#reportlab.pdfgen.canvas.Canvas.drawImage Could it be that you are not closing the report properly, so that the external image hasn't been added yet? – Roland Smith Oct 23 '12 at 09:51
  • @RolandSmith Thanks; I considered that - but, in this case, I am using the Platypus ( http://www.reportlab.com/apis/reportlab/dev/platypus.html ) component of ReportLab. Platypus still delegates to the canvas methods ( such as drawImage ) internally; but, from my reading of the docs, when I call build() on a document template, I should not have to close the PDF. In fact, SimpleDocTemplate does not expose the document canvas, so there is no easy way to manually close the PDF – Ngure Nyaga Oct 23 '12 at 11:28

2 Answers2

1

I eventually brought this matter to a close by using a custom Image subclass:

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

Saving the graphs in vector graphics formats eg EPS, saving as JPEG, saving as PNG with and without the alpha channel all did not seem to make a difference.

Ngure Nyaga
  • 2,989
  • 1
  • 20
  • 30
0

The optimal solution would be to generate your graphs in a vector format like postscript that is supported by reportlab. A lot of UNIX software can do this out of the box, and on windows you can use the excellent PDFCreator.

If you have to use raster images for your graphs, try converting your images to JPEG format. Those can be easily embedded in a PDF file using the DCTDecode filter. (This is e.g. what jpeg2pdf does.)

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • This report process is part of a larger Python application that needs to be in Python ( uses numpy, pandas quite a bit ). I have considered "bridging" to another reporting solution [ I have lots of experience with JasperReports ], but I am reserving that as my "last ditch" choice. However, I will evaluate / code up a vector based solution and see if that works. Thanks. – Ngure Nyaga Oct 23 '12 at 04:53
  • If you are using matplotlib to generate the graphs, you can save them as a vector format (e.g. PDF) instead of PNG using the `format` keyword argument of `matplotlib.pyplot.savefig`. See http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.savefig – Roland Smith Oct 23 '12 at 09:45
  • I will code up a solution that generates the graphs in EPS format and see how that goes. – Ngure Nyaga Oct 23 '12 at 11:30
  • I rendered the graphs as EPS and they still did not show in the PDF. The only "solution" that seems to work across the board is the hack that uses `canvas.drawInlineImage`. – Ngure Nyaga Oct 29 '12 at 09:17
  • It might be a good idea to send a bug report to the ReportLab people. – Roland Smith Oct 29 '12 at 20:06