I am writing pdfs with reportlab inside a django view, they are very simple, header, content and footer.
I use SimpleDocTemplate, which works very well to draw tables in the content, footer and drwan header using:
build([data], onFirstPage=drawPageFrame, onLaterPages=drawPageFrame).
My question is: how can I draw an image, for example, using Canvas.drawImage (...)? I need a "floating" image ... located above the text where I want, and with SimpleDocTemplate I don't have a Canvas object for this.
Search I found this:
The table layout element uses streams. Packers typically set the canv attribute to each fluid when it is wrapped, split, or around wrapping, splitting, and drawing methods, for example. Inside these methods, you have access to the canvas using the canv self attribute.
How can this be used?
Ummmm, more testing material:
flowables.Macro flowables.CallerMacro
# - * - coding: utf-8 - * -
from reportlab.lib.pagesizes import A4, landscape, portrait
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import Table, Flowable, SimpleDocTemplate, Paragraph, Spacer, Image
from reportlab.lib import randomtext
from reportlab import platypus
import os, random
styles = getSampleStyleSheet ()
path = os.path.realpath (os.path.dirname (__ file__))
def drawPageFrame (canvas, doc):
canvas.saveState ()
canvas.drawImage (path + "/ ujiPDF.jpg", 50,50,57,57)
canvas.restoreState ()
doc = SimpleDocTemplate ("salida.pdf", pagesize = A4)
elementos = []
com = 'canvas.drawImage ("' + path + '/ ujiPDF.jpg", 100,100,57,57)'
print com
elementos.append (platypus.flowables.Macro ('canvas.saveState ()'))
print platypus.flowables.Macro (com)
elementos.append (platypus.flowables.Macro (com))
elementos.append (platypus.flowables.Macro ('canvas.restoreState ()'))
para = Paragraph (randomtext.randomText (randomtext.PYTHON, 20), styles ["Normal"])
elementos.append (para)
doc.build (elementos, onFirstPage = drawPageFrame, onLaterPages = drawPageFrame)
This is a macro approach ... clean output, but without a second image.