[reportlab-users] decimal and/or dot-leader tabs

Glenn Linderman v+python at g.nevcal.com
Sun Sep 11 21:46:30 EDT 2011


On 9/11/2011 5:44 PM, Glenn Linderman wrote:

> However, it would seem that the case of manually calling addEntry

> doesn't work properly, which would seem to be a code bug, or else a

> severe misunderstanding of the documentation. I tried both build and

> multiBuild just in case, but it would seem that the multiBuild should

> work even if it is overkill, but both produce only placeholder entries

> for TableOfContents :(

>

> Does anyone have a working example of a TableOfContents built with

> addEntry?

>

> I'll work up a failing sample next, I guess, since this is blocking

> progress.


OK, stuff the attached two files into a directory, run the .py with
Python 2.6 (or so), and get a placeholder TOC in spite of adding two
entries to it.

This either demonstrates a problem in the reportlab code or docs, or a
problem in my code and understanding of the reportlab docs. HELP :)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20110911/98840c01/attachment.html>
-------------- next part --------------
#!/usr/bin/python2
# -*- coding: utf-8 -*-
__version__ = '11.9.8'
import sys, os, atexit
import reportlab.rl_config
from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import *
from reportlab.platypus.tableofcontents import TableOfContents
from reportlab.lib.styles import StyleSheet1, PropertySet, TA_LEFT, TA_CENTER, black
from reportlab.lib.units import inch
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
theFile = 'testrl6.pdf'
theTitle = 'test'
theAuthor = 'rkivs.com'
thePgWid = 3.25
thePgHei = 3.25
theMargin = .25
theCovWid = 412
theCovHei = 412
theIxCount = 0
defsize = 10
deflead = 11
h1size = 18
h1lead = 10
h3size = 12
h3lead = 4
elements = []
outline = []
morefonts = []

def add_font( font, ffn ):
# moved above, but needed here
# from reportlab.pdfbase import pdfmetrics
# from reportlab.pdfbase.ttfonts import TTFont
pdf = Canvas( sys.stdout )
for sf, sffn in [['', ''], ['-Bold', 'Bd'], ['-Italic', 'I'], ['-BoldItalic', 'BI']]:
f = font + sf
sff = 'c:\\windows\\fonts\\' + ffn + sffn + '.ttf'
ttf = TTFont( f, sff )
morefonts.append( f )
pdfmetrics.registerFont( ttf )
pdfmetrics.registerFontFamily(
font,
normal=font,
bold=font+'-Bold',
italic=font+'-Italic',
boldItalic=font+'-BoldItalic',
)

def mkpage( canvas, doc ):
for ix in outline:
canvas.addOutlineEntry( *ix )

class MyParaSt(PropertySet):
defaults = {
'fontName':'Times-New-Roman',
'fontSize': defsize,
'leading': deflead,
'leftIndent':0,
'rightIndent':0,
'firstLineIndent':0,
'alignment':TA_LEFT,
'spaceBefore':0,
'spaceAfter':0,
'bulletFontName':'Times-New-Roman',
'bulletFontSize':10,
'bulletIndent':0,
'textColor': black,
'backColor':None,
'wordWrap':None,
'borderWidth': 0,
'borderPadding': 0,
'borderColor': None,
'borderRadius': None,
'allowWidows': 1,
'allowOrphans': 0,
'textTransform':None, #uppercase lowercase (captitalize not yet) or None or absent
}

def mkdoc():
doc = SimpleDocTemplate(
theFile,
pagesize=[ thePgWid*inch, thePgHei*inch],
leftMargin=theMargin*inch,
rightMargin=theMargin*inch,
topMargin=theMargin*inch,
bottomMargin=theMargin*inch,
title=theTitle,
author=theAuthor,
)
doc.multiBuild( elements, onFirstPage=mkpage )

atexit.register( mkdoc )

def init():
global styles
add_font('Times-New-Roman', 'times')
styles = StyleSheet1()
styles.add( MyParaSt(
name='h1',
fontSize=h1size,
spaceAfter=h1lead,
leftIndent=.3*inch,
firstLineIndent=-.3*inch,
# alignment=TA_CENTER,
))
styles.add( MyParaSt(
name='h3',
fontSize=h3size,
spaceAfter=h3lead,
leftIndent=.3*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='u',
leftIndent=.3*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='ru',
leftIndent=.6*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='i',
fontName='Times-New-Roman-Italic',
leftIndent=.3*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='ri',
fontName='Times-New-Roman-Italic',
leftIndent=.6*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='b',
fontName='Times-New-Roman-Bold',
leftIndent=.3*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='rb',
fontName='Times-New-Roman-Bold',
leftIndent=.6*inch,
firstLineIndent=-.3*inch,
))
styles.add( MyParaSt(
name='p'))
styles.add( MyParaSt(
name='rp',
leftIndent=.2*inch,
))

init()

# There is one function below corresponding to each type of paragraph needed.
# These also correspond to the usual set of paragraph types defined for
# Ebook. The functions in Pdf( Ebook ) emit calls to these functions as needed.

# figure out how to select a particular group of fonts, such as Chinese

class Page():
# To use this class, all functions that do content generation should
# call do, flagging whether they should start on a new page or not.
# If a function both needs to start on a page, and also generates
# content that would (partially) fill the page (or multiple pages),
# then it should call do( True ) and also do( False ).
# If it only generates anchors, that should be at the top of a page,
# then it should only call do( True ).
# If it only generates content, that has no particular page boundary
# requirements, then it should only call do( False ).
def __init__( self ):
self.save = True # a new doc starts at the top of a page!
def do( self, flag ):
if flag == self.save:
return
if flag:
elements.append( PageBreak())
self.save = flag
page = Page()

class DQ(): # Data Queue
def __init__( self ):
self.pending = ''
self.recenttoc = None
def w( self, txt, style=styles['p'], tocnum=None ):
add = True
if self.pending:
txt = self.pending + txt
self.pending = ''
if tocnum is None:
elem = Paragraph( txt, style )
self.recenttoc = None
else:
if self.recenttoc:
add = False
elem = self.recenttoc
else:
elem = TableOfContents()
self.recenttoc = elem
elem.levelStyles = [ style ]
print "TOC addEntry:", txt, tocnum
elem.addEntry( 0, txt, tocnum )
if add:
elements.append( elem )
def q( self, txt ):
self.pending += txt
dq = DQ()

def addlink( txt, id, level=0, closed=None ):
outline.append(( txt, id, level, closed ))

def addanchor( aid ):
anchor = '<a name="%s"/>' % aid
for ix in range( theIxCount ):
anchor += '<a name="%sx%d"/>' % ( aid, ix )
dq.q( anchor )

def cover( img=None, aid=None, style=styles['h1']):
page.do( True )
if aid:
addanchor( aid )
dq.w('', style )
if img:
res = 150
page.do( False )
im = Image( img, theCovWid/res*inch, theCovHei/res*inch )
im.hAlign = 'CENTER'
im.vAlign = 'CENTER'
elements.append( im )
page.do( True )

def h1( txt, aid, style=styles['h1']):
page.do( True )
page.do( False )
addanchor( aid )
dq.w( txt, style )

def h3( txt, aid, level, style=styles['h3'], link=True, anchor=True ):
page.do( True )
page.do( False )
if link:
addlink( txt, aid, level, 0 )
if anchor:
addanchor( aid )
dq.w( txt, style )

def empty( style=styles['p']):
page.do( False )
elements.append( Spacer( 1*inch, .06*inch ))

def toc( txt, num, style=styles['u']):
page.do( False )
dq.w( txt, style, num )

def u( txt, style=styles['u']):
page.do( False )
dq.w( txt, style )

def ru( txt, style=styles['ru']):
page.do( False )
dq.w( txt, style )

def i( txt, style=styles['i']):
page.do( False )
dq.w( txt, style )

def ri( txt, style=styles['ri']):
page.do( False )
dq.w( txt, style )

def b( txt, style=styles['b']):
page.do( False )
dq.w( txt, style )

def rb( txt, style=styles['rb']):
page.do( False )
dq.w( txt, style )

def p( txt, style=styles['p']):
page.do( False )
dq.w( txt, style )

def indented( txt, style=styles['rp']):
page.do( False )
dq.w( txt, style )

# above is boilerplate styles and templates common to all ebooks
# below is specifically generated for this ebook.

addlink( theTitle, 'doc', 0, 0 )
addlink('Table of Contents', 'toc', 1, 0 )
addlink('test', 'chapters', 1, 0 )
addanchor('chapters')
cover('testrl6.jpg', 'doc')
page.do( True )
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
empty()
u('A collection of test pages')
empty()
u('invented from whole cloth.')
empty()
empty()
u('version: 11.9.10')
empty()
u('For more information, see')
u('http://rkivs.com/digitalmusic/')
addanchor('toc')
h3('Table of Contents', 'x4', 2 )
empty()
toc('A very titular title', 1 )
toc('Very familiar nonsense', 2 )
h3('1. A very titular title', 'c1', 2 )
empty()
u('This page doesn’t say anything useful, but is wide to scroll.')
u('But it says it very carefully.')
u('Attempting to avoid offense,')
u('It provides instead nonsense.')
empty()
ri('Alternate:')
ri('Tihs pgae dneo’st say atinynhg uefsul, but is wdie to srlocl.')
ri('But it syas it vrey cauellrfy')
ri('Apmttitneg to aviod ofsenfe')
ri('It pivrdoes ianetsd nnssonee.')
empty()
h3('2. Very familiar nonsense', 'c2', 2 )
empty()
u('Lorem ipsum dolor sit amet,')
u('consectetur adipisicing elit,')
u('sed do eiusmod tempor incididunt')
u('ut labore et dolore magna aliqua.')
empty()
u('Ut enim ad minim veniam,')
u('quis nostrud exercitation ullamco laboris')
u('nisi ut aliquip ex ea commodo consequat.')
u('Duis aute irure dolor in reprehenderit')
u('in voluptate velit esse cillum')
u('dolore eu fugiat nulla pariatur.')
u('Excepteur sint occaecat cupidatat non proident,')
u('sunt in culpa qui officia deserunt mollit anim id est laborum.')
empty()
cover('testrl6.jpg')
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testrl6.jpg
Type: image/jpeg
Size: 16321 bytes
Desc: not available
Url : <http://two.pairlist.net/pipermail/reportlab-users/attachments/20110911/98840c01/attachment-0001.jpg>


More information about the reportlab-users mailing list