Draw Images With Canvas And Use Simpledoctemplate
Solution 1:
You don't need to subclass the whole document template if I understand you right, you just want a flowable that you can put INTO a SimpleDocTemplate. You can achieve that with a very easy subclass of Flowable itself.
-> In particular since people often ask about how to put matplotlib objects into reportlab, I'll show how to generate a plot with matplotlib and then use the modified flowable to put that plot into a SimpleDocTemplate (without saving the file to disk). The concept applies to any file or anything you can feed into a cStringIO
-> The below DOES allow you to put the figure over the text (change the height from negative to positive to push it above the imaginary line at the top of the section the plot goes into)
The key concept is that each Flowable does, itself, also contain a canvas we can draw onto.
import matplotlib.pyplot as plt
import cStringIO
from reportlab.lib.units import inch, cm
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
import reportlab.lib, reportlab.platypus
class flowable_fig(reportlab.platypus.Flowable):
def __init__(self, imgdata):
reportlab.platypus.Flowable.__init__(self)
self.img = reportlab.lib.utils.ImageReader(imgdata)
def draw(self):
self.canv.drawImage(self.img, 0, 0, height = -2*inch, width=4*inch)
# http://www.reportlab.com/apis/reportlab/2.4/pdfgen.html
doc = SimpleDocTemplate(("report.pdf"),pagesize=letter,
rightMargin=72,leftMargin=72,
topMargin=72,bottomMargin=18)
Story=[]
styles=getSampleStyleSheet()
ptext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi massa dolor, vulputate quis elit sed, sagittis consectetur erat. Sed lobortis nisi eros, eu maximus enim iaculis ac. Vestibulum sagittis urna nec interdum aliquam. Pellentesque ornare velit ut ante ullamcorper, vulputate accumsan nisi vulputate. Fusce consectetur dolor quam. Phasellus hendrerit, ligula vel consectetur pretium, lorem neque dapibus eros, ornare suscipit ipsum dolor id nisl. Sed vel orci id leo efficitur lobortis sit amet id risus. Nullam euismod, ipsum a posuere scelerisque, ante lorem ultrices nibh, ut feugiat metus ex congue enim. Nam lobortis, metus id pellentesque feugiat, arcu orci rutrum felis, quis luctus urna nisl at nulla. Donec eu eros pharetra dolor congue facilisis at ac magna. Nullam eu ultricies metus. Sed sodales, libero viverra pellentesque tempus, magna purus convallis nibh, eu condimentum tortor erat tincidunt turpis. Vestibulum scelerisque tincidunt egestas. Nullam commodo diam nisl, sed consequat ex sagittis eu.'
Story.append(Paragraph(ptext, styles["Normal"]))
fig = plt.figure(figsize=(10, 3))
plt.plot([1,2,3,4])
plt.ylabel('This is a boring plot')
imgdata = cStringIO.StringIO()
fig.savefig(imgdata, format='png')
imgdata.seek(0) # rewind the data
pic = flowable_fig(imgdata)
Story.append(pic)
doc.build(Story)
You can expand this minimum code snipped to accomplish whatever you like and you can add as many pieces to the Story variable (which is just a list of flowables really). The magic is just that we're handing the doc builder a Flowable that uses the Flowable's own canvas to draw the figure.
EDIT: I almost forgot, because this gives us full access to drawImage, we can also make that picture or plot have transparency so other things can be seen through it. The docs on drawImage have the details.
Solution 2:
There is Image class in the platypus. Just use from reportlab.platypus import Image
and you got access to this class. It works like other classes inside and smth.append(Image(filename))
appends image to object you need to build into pdf. Founded at Tyler Lessman personal website
Solution 3:
Your best option is to create a subclass of SimpleDocTemplate or BaseDocTemplate. In the build method you will have access to the canvas. If you want to tap into everything that SimpleDocTemplate does, you might try copying it directly from site-packages/reportlab/platypus/doctemplate.py
.
Post a Comment for "Draw Images With Canvas And Use Simpledoctemplate"