[reportlab-users] Custom Flowables - Can't get shapes.Drawing() to paint to the story/canvas
J. R. Carroll
jrc.csus at gmail.com
Thu Nov 29 14:47:44 EST 2012
Hi,
I haven't forgotten about my other thread, but in preparation to
(hopefully) finalize my findings and report back, I started making a "print
test page" (which, once I am done, I'd be happy to share).
I am having a problem following the reportlab manual/examples online for
creating my own custom flowable. As I understand it, a shapes.Drawing()
object is not a flowable by default (correct?). If I want it to be a
flowable I have to create my own flowable object wrapped around it (or did
I misunderstand)?
I have created my own custom class to deal with the strip chart grids I am
working with (as noted in my previous thread I started), but I cannot get
the class to return a flowable that story.build() can work with (see the
error at the bottom of this email).
What I am trying to achieve is a "small sample grid" (which is drawn by my
class), then immediately below that is the grid color name + RGB color
reference. Eventually, I was going to build it so it would print 4-wide,
and as many necessary "down". So it would be a matrix of finely printed,
super small/fine, grid lines with different colors (which I can then use to
determine which colors would work best on a variety of printers + work with
our marketing department and MD's to select the colors that will work the
best in the field (and be accepted).
Here is my code (and below is the error I am currently getting):
PS: I know there are some 'funny code tidbits' floating in there - I
started down one path, then decided to use story to build, and some stuff
just got left in, I'll clean it up, I promise, but if you see something
that is redundant, assume I know about it. If it's something that is
broken, that's what I want to know!!! :P Thanks again everyone! Also, if
you comment out all references to para2 in my code below - it flows just
fine with the dictionary of color labels/values.
'''
Created on Nov 29, 2012
@author: nammer
'''
import reportlab.pdfgen.canvas as canvas
import reportlab.platypus as pl
import reportlab.graphics as graphics
import reportlab.graphics.shapes as shapes
import reportlab.pdfgen.textobject as text
from reportlab.platypus import Paragraph, SimpleDocTemplate, Spacer
from reportlab.platypus.flowables import Flowable, KeepTogether
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.units import mm
from reportlab.lib.pagesizes import LETTER
from reportlab.graphics import renderPDF
__all_colors__ = shapes.colors.getAllNamedColors()
__style_sheets__ = getSampleStyleSheet()
__margin__ = 40
__gutter__ = 15
story = []
style_one = __style_sheets__['BodyText']
style_one.fontSize = 7
class Grid_test(Flowable):
"""Makes a small grid for test printing"""
def __init__(self, color=(None, None), dimensions=(None, None),
scale=1*mm, minor_grid=0, major_grid=.3, xoffset=0,
size=None, canvas=None):
self.iCanvas = shapes.Drawing()
self.colorName = color[0]
self.colorRGB = color[1]
self.width = dimensions[0]
self.height = dimensions[1]
self.grid_size = scale
self.grid_minor = minor_grid
self.grid_major = major_grid
self.xoffset = xoffset
self.size = size
def wrap(self, *args):
return (self.xoffset, self.size)
def draw(self):
"""Draw vertical lines first!"""
_h = self.height*self.grid_size
_w = self.width*self.grid_size
for i in range(0, self.width, 1):
l = i*self.grid_size
if (i % 5.0 == 0):
self.iCanvas.add(shapes.Line(l, 0, l, _h,
strokeWidth=self.grid_major,
strokeColor=self.colorRGB))
else:
self.iCanvas.add(shapes.Line(l, 0, l, _h,
strokeWidth=self.grid_minor,
strokeColor=self.colorRGB))
"""Draw horizontal lines next!"""
for n in range(0, self.height, 1):
l = n*self.grid_size
if (n % 5.0 == 0):
self.iCanvas.add(shapes.Line(0, l, _w, l,
strokeWidth=self.grid_major,
strokeColor=self.colorRGB))
else:
self.iCanvas.add(shapes.Line(0, l, _w, l,
strokeWidth=self.grid_minor,
strokeColor=self.colorRGB))
renderPDF.draw(self.iCanvas, page, 50, 50)
print "this is working!"
page = SimpleDocTemplate("test.pdf",
pagesize=LETTER,
rightMargin=50,
leftMargin=50,
topMargin=50,
bottomMargin=50)
aW = LETTER[0]-__margin__
aH = LETTER[1]-__margin__
startX = 0 + __margin__
startY = 0 + __margin__
advX = startX + __gutter__
advY = startY + __gutter__
maxX = aW - (__margin__*2)
maxY = aH - (__margin__*2)
grid_dim = (40, 10)
for color in __all_colors__:
v = Grid_test(color=(color, __all_colors__.get(color)),
dimensions=grid_dim, canvas=page)
color_Info = (str(color).capitalize() + " " +
str(__all_colors__.get(color)))
para_one = Paragraph(color_Info, style_one)
para_one.keepWithNext = True
para_two = Paragraph(v, style_one)
para_one.wrap(aW, aH)
para_two.wrap(aW, aH)
story.append(para_one)
story.append(Spacer(0,12))
#
# if aW < w:
# renderPDF.draw(v.draw_grid(), page, advX, (aH-grid_dim[0]))
# para.drawOn(page, advX, (aH-grid_dim[0]-11))
# elif aW > w:
# print "this worked"
# aW = startX
# aH = aH - 60
# renderPDF.draw(v.draw_grid(), page, startX, (aH-grid_dim[0]))
# para.drawOn(page, startX, (aH-grid_dim[0]-11))
# advY = advY - 60
# else:
# print "WIDTH", w,"aW", aW
aW = aW
advX = advX + 120
page.build(story)
===============ERROR======================
Traceback (most recent call last):
File "/home/nammer/workspace/jReportLab/src/printPDF/printtest.py", line
110, in <module>
para_two = Paragraph(v, style_one)
File
"/usr/local/lib/python2.6/dist-packages/reportlab/platypus/paragraph.py",
line 919, in __init__
self._setup(text, style, bulletText or
getattr(style,'bulletText',None), frags, cleanBlockQuotedText)
File
"/usr/local/lib/python2.6/dist-packages/reportlab/platypus/paragraph.py",
line 932, in _setup
text = cleaner(text)
File
"/usr/local/lib/python2.6/dist-packages/reportlab/platypus/paragraph.py",
line 105, in cleanBlockQuotedText
L=filter(truth,map(_lineClean, split(text, '\n')))
File
"/usr/local/lib/python2.6/dist-packages/reportlab/platypus/paragraph.py",
line 66, in split
return [uword.encode('utf8') for uword in text.split(delim)]
TypeError: split() takes exactly 3 arguments (2 given)
----
J. R. Carroll
Independent Researcher through Hurtz Labs
Research Methods, Test Development, and Statistics
www.jrcresearch.net
Cell: (650) 776-6613
Email: jrcarroll at jrcresearch.net
jrcarroll at hurtzlab.com
jrc.csus at gmail.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20121129/59e10d09/attachment.html>
More information about the reportlab-users
mailing list