[reportlab-users] PLATYPUS & PDF
Robin Becker
robin at reportlab.com
Wed Oct 5 06:41:46 EDT 2005
george wrote:
> How do I get pdf bookmarks into PLATYPUS documents?
>
> A brief example please
>
> george
>
.....
There's an example in the test_platypus_toc.py script. Basically inherit from
your document template class and use an afterFlowable method.
class MyDocTemplate(BaseDocTemplate):
"The document template used for all PDF documents."
_invalidInitArgs = ('pageTemplates',)
def __init__(self, filename, **kw):
frame1 = Frame(2.5*cm, 2.5*cm, 15*cm, 25*cm, id='F1')
self.allowSplitting = 0
apply(BaseDocTemplate.__init__, (self, filename), kw)
template = PageTemplate('normal', [frame1], myMainPageFrame)
self.addPageTemplates(template)
def afterFlowable(self, flowable):
"Registers TOC entries and makes outline entries."
if flowable.__class__.__name__ == 'Paragraph':
styleName = flowable.style.name
if styleName[:7] == 'Heading':
# Register TOC entries.
level = int(styleName[7:])
text = flowable.getPlainText()
pageNum = self.page
self.notify('TOCEntry', (level, text, pageNum))
# Add PDF outline entries (not really needed/tested here).
key = str(hash(flowable))
c = self.canv
c.bookmarkPage(key)
c.addOutlineEntry(text, key, level=level, closed=0)
--
Robin Becker
More information about the reportlab-users
mailing list