Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- from reportlab.platypus import BaseDocTemplate, Paragraph, PageTemplate
- from reportlab.platypus.doctemplate import Indenter
- from reportlab.platypus.flowables import *
- from reportlab.platypus.tables import *
- from reportlab.platypus.xpreformatted import *
- from reportlab.lib.styles import getSampleStyleSheet
- from copy import copy
- from reportlab.platypus.flowables import _listWrapOn, _FUZZ, Flowable
- from reportlab.platypus.frames import Frame
- from reportlab.lib.units import *
- topFlowables=[]
- class MarkingFrame(Frame):
- """This frame marks things the first flowable added as
- _atTop.
- """
- def add (self, flowable, canv, trySplit=0):
- flowable._atTop=self._atTop
- return Frame.add(self, flowable, canv, trySplit)
- from reportlab.platypus.flowables import _listWrapOn, _FUZZ, Flowable
- class Sinker(Flowable):
- '''A flowable that always takes the rest of the frame.
- It then draws its contents (a list of sub-flowables)
- at the bottom of that space'''
- def __init__(self, content):
- self.content = content
- def wrap (self, aW, aH):
- self.width, self.height = _listWrapOn(self.content, aW, None)
- return self.width, aH
- def draw (self):
- canv = self.canv
- canv.saveState()
- x = canv._x
- y = canv._y
- y += self.height
- aW = self.width
- for c in self.content:
- w, h = c.wrapOn(canv, aW, 0xfffffff)
- if (w < _FUZZ or h < _FUZZ) and not getattr(c, '_ZEROSIZE', None):
- continue
- y -= h
- canv.saveState()
- c.drawOn(canv, x, y, _sW=aW - w)
- canv.restoreState()
- canv.restoreState()
- def go():
- Story=[]
- styles = getSampleStyleSheet()
- doc = BaseDocTemplate("test_footnotes.pdf",
- pageTemplates=PageTemplate(frames=[MarkingFrame(2*cm,2*cm,15*cm,15*cm)]))
- st=styles['Normal']
- st.spaceAfter=6
- elements=[Paragraph('''%s - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed dolor nibh, ut porta mauris. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non nisl erat, vitae gravida tellus. Proin vulputate risus ac neque facilisis consectetur.'''%i,st) for i in range(15)]
- footnote1=Paragraph('''<bullet>FN</bullet> This is a footnote 1. ''',st)
- footnote1.isFootnote=True
- footnote2=Paragraph('''<bullet>FN</bullet> This is a footnote 2. ''',st)
- footnote2.isFootnote=True
- longpara=Paragraph(('''%s - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed sed dolor nibh, ut porta mauris. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis non nisl erat, vitae gravida tellus. Proin vulputate risus ac neque facilisis consectetur.'''%'LONG')*6,st)
- elements.insert(5,footnote1)
- elements.insert(8,longpara)
- elements.insert(12,footnote2)
- flag = True
- while True:
- print 'Building'
- doc.multiBuild(elements)
- ## Rearrange footnotes if needed
- if flag:
- #from pudb import set_trace; set_trace()
- print 'Rearranging footnotes'
- newStory=[]
- fnPile=[]
- for e in elements:
- print e.__class__
- if getattr(e,'isFootnote',False):
- # Add it to the pile
- #if not isinstance (e, MySpacer):
- fnPile.append(e)
- elif getattr(e, '_atTop', False):
- if fnPile:
- newStory.append(Sinker(fnPile))
- newStory.append(e)
- fnPile=[]
- else:
- newStory.append(e)
- elements = newStory+[Sinker(fnPile)]
- for e in elements:
- if hasattr(e, '_postponed'):
- delattr(e,'_postponed')
- flag = False
- continue
- break
- go()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement