[reportlab-users] Bulleted text
Saketh Bhamidipati
saketh.bhamidipati at gmail.com
Fri Jun 30 07:55:36 EDT 2006
On 6/30/06, Robin Becker <robin at reportlab.com> wrote:
>
> Saketh Bhamidipati wrote:
> > Here is the code that I want to retain:
> >
> > Two frames:
> > textframe = Frame(2 * inch, 1 * inch, 5.75 * inch, 9 * inch, id = 'TF',
> > showBoundary = 1)
> > headingframe = Frame( 0.5 * inch, 1 * inch, 1.5 * inch, 9 * inch, id =
> 'HF',
> > showBoundary = 1)
> > Page template:
> > def myPageTemplate(canvas, doc):
> > canvas.saveState()
> > canvas.setFont('Times-Roman', 12)
> > canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108, Title)
> > canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch, Author)
> > canvas.setFont('Times-Roman', 10)
> > canvas.drawCentredString (PAGE_WIDTH / 2.0, inch / 2.0, "%d" %
> doc.page)
> > canvas.restoreState()
> >
> > I need to be able to write text to the textframe, and every time I do
> > that I
> > also need to write a heading to the headingframe. Unfortunately, I can't
>
> > seem to find how to combine the low-level features of ReportLab with the
> > high-level features of Platypus.
> >
> > If I could access the canvas directly in my derived BaseDocTemplate,
> then I
> > would be able to manually write the headings and texts. However, for
> some
> > reason, the canvas is inaccessible in the BaseDocTemplate.
> >
> > Here is a semi-pseudocode function that I would like to write, but I'm
> not
> > sure how to implement it:
> > def writeText(flowabletext, heading): # heading is a string
> > textframe.write(flowabletext)
> > headingframe.write(heading, flowabletext.getY())
> >
> > This is what I need to do:
> >
> > 1. Retain the frames and page template that I have written - they are
> > what I need.
> > 2. Make a function that writes a block of paragraphs - each paragraph
> > having a bullet - to the textframe, and writes a heading to the
> heading
> > frame with the same Y-coordinate as the first line of the block of
> > paragraphs.
> >
> > I am utterly confused, as the ReportLab documentation and the examples
> do
> > not explain how to do do something like this. Assistance is greatly
> > appreciated.
>
> The problem here is that you don't explain (or perhaps care) what happens
> when
> one or other of the frames fills up. There is an example in the
> test_platypus_general.py script that shows a document template filling
> parallel
> frames.
>
>
> The approach there works because we're very careful about the content.
> --
> Robin Becker
> _______________________________________________
> reportlab-users mailing list
> reportlab-users at reportlab.com
> http://two.pairlist.net/mailman/listinfo/reportlab-users
>
Both frames will up with text in my current code when I append the Story to
them.
#!/usr/bin/python
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
from reportlab.platypus.frames import Frame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.platypus.xpreformatted import XPreformatted
from reportlab.lib.styles import ParagraphStyle, PropertySet
from reportlab.lib.enums import TA_LEFT, TA_CENTER, TA_RIGHT
from reportlab.lib.units import inch
from reportlab.lib.colors import black
PAGE_HEIGHT = defaultPageSize[1]; PAGE_WIDTH = defaultPageSize[0]
styles = getSampleStyleSheet()
def bullet(text):
return (u"\N{BULLET} " + unicode(text))
Title = "Hello, World!"
Author = "Saketh Bhamidipati"
textframe = Frame(2 * inch, 1 * inch, 5.75 * inch, 9 * inch, id = 'TF',
showBoundary = 1)
headingframe = Frame(0.5 * inch, 1 * inch, 1.5 * inch, 9 * inch, id = 'HF',
showBoundary = 1)
class NotalonTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
self.textframe = textframe
self.headingframe = headingframe
self.allowSplitting = 1
self.showBoundary = 1
apply(BaseDocTemplate.__init__, (self, filename), kw)
template = PageTemplate('Normal', [self.headingframe, self.textframe],
myPageTemplate)
self.addPageTemplates(template)
def fillText(self, flowable):
f = self.textframe
f.addFromList(flowable, "hanuman.pdf")
def fillHeading(self, flowable):
f = self.headingframe
while len(flowable)>0 and f is self.headingframe:
self.handle_flowable(flowable)
def build(self, flowable1, flowable2):
self._startBuild()
while(len(flowable1) > 0 or len(flowable2) > 0):
self.clean_hanging()
self.fillText(flowable1)
self.fillHeading(flowable2)
self._endBuild()
def myPageTemplate(canvas, doc):
canvas.saveState()
canvas.setFont('Times-Roman', 12)
canvas.drawCentredString(PAGE_WIDTH / 2.0, PAGE_HEIGHT - 108, Title)
canvas.drawString(PAGE_WIDTH - 2 * inch, PAGE_HEIGHT - inch, Author)
canvas.setFont('Times-Roman', 10)
canvas.drawCentredString(PAGE_WIDTH / 2.0, inch / 2.0, "%d" % doc.page)
canvas.restoreState()
def drawHeadings(canvas, doc):
pass
def go():
doc = NotalonTemplate("hanuman.pdf")
Story = []
style = styles["Normal"]
Story.append(Paragraph(bullet("Luke, I <i>am</i> your father. Now die. "
* 200) , style))
doc.build(Story, Story)
go()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://two.pairlist.net/pipermail/reportlab-users/attachments/20060630/cae1cffe/attachment.html
More information about the reportlab-users
mailing list