Advertisement
Guest User

Simple footnotes in reportlab (Failing)

a guest
May 4th, 2010
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.13 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from reportlab.platypus import BaseDocTemplate, Paragraph, PageTemplate
  5. from reportlab.platypus.doctemplate import Indenter
  6. from reportlab.platypus.flowables import *
  7. from reportlab.platypus.tables import *
  8. from reportlab.platypus.xpreformatted import *
  9. from reportlab.lib.styles import getSampleStyleSheet
  10. from copy import copy
  11.  
  12. from reportlab.platypus.flowables import _listWrapOn, _FUZZ, Flowable
  13. from reportlab.platypus.frames import Frame
  14. from reportlab.lib.units import *
  15.  
  16. topFlowables=[]
  17.  
  18. class MarkingFrame(Frame):
  19.     """This frame marks things the first flowable added as
  20.    _atTop.
  21.  
  22.    """
  23.  
  24.     def add (self, flowable, canv, trySplit=0):
  25.         flowable._atTop=self._atTop
  26.         return Frame.add(self, flowable, canv, trySplit)
  27.  
  28. from reportlab.platypus.flowables import _listWrapOn, _FUZZ, Flowable
  29.  
  30.  
  31. class Sinker(Flowable):
  32.     '''A flowable that always takes the rest of the frame.
  33.    It then draws its contents (a list of sub-flowables)
  34.    at the bottom of that space'''
  35.  
  36.     def __init__(self, content):
  37.         self.content = content
  38.  
  39.     def wrap (self, aW, aH):
  40.         self.width, self.height = _listWrapOn(self.content, aW, None)
  41.         return self.width, aH
  42.  
  43.     def draw (self):
  44.         canv = self.canv
  45.         canv.saveState()
  46.         x = canv._x
  47.         y = canv._y
  48.         y += self.height
  49.         aW = self.width
  50.         for c in self.content:
  51.             w, h = c.wrapOn(canv, aW, 0xfffffff)
  52.             if (w < _FUZZ or h < _FUZZ) and not getattr(c, '_ZEROSIZE', None):
  53.                 continue
  54.             y -= h
  55.             canv.saveState()
  56.             c.drawOn(canv, x, y, _sW=aW - w)
  57.             canv.restoreState()
  58.         canv.restoreState()
  59.  
  60.  
  61. def go():
  62.         Story=[]
  63.         styles = getSampleStyleSheet()
  64.         doc = BaseDocTemplate("test_footnotes.pdf",
  65.             pageTemplates=PageTemplate(frames=[MarkingFrame(2*cm,2*cm,15*cm,15*cm)]))
  66.         st=styles['Normal']
  67.         st.spaceAfter=6
  68.         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)]
  69.  
  70.         footnote1=Paragraph('''<bullet>FN</bullet> This is a footnote 1. ''',st)
  71.         footnote1.isFootnote=True
  72.        
  73.         footnote2=Paragraph('''<bullet>FN</bullet> This is a footnote 2. ''',st)
  74.         footnote2.isFootnote=True
  75.  
  76.         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)
  77.  
  78.         elements.insert(5,footnote1)
  79.         elements.insert(8,longpara)
  80.         elements.insert(12,footnote2)
  81.  
  82.         flag = True
  83.         while True:
  84.             print 'Building'
  85.             doc.multiBuild(elements)
  86.             ## Rearrange footnotes if needed
  87.             if flag:
  88.                 #from pudb import set_trace; set_trace()
  89.                 print 'Rearranging footnotes'
  90.                 newStory=[]
  91.                 fnPile=[]
  92.                 for e in elements:
  93.                     print e.__class__
  94.                     if getattr(e,'isFootnote',False):
  95.                         # Add it to the pile
  96.                         #if not isinstance (e, MySpacer):
  97.                         fnPile.append(e)
  98.                     elif getattr(e, '_atTop', False):
  99.                         if fnPile:
  100.                             newStory.append(Sinker(fnPile))
  101.                         newStory.append(e)
  102.                         fnPile=[]
  103.                     else:
  104.                         newStory.append(e)
  105.                 elements = newStory+[Sinker(fnPile)]
  106.                 for e in elements:
  107.                     if hasattr(e, '_postponed'):
  108.                         delattr(e,'_postponed')
  109.                 flag = False
  110.                 continue
  111.             break
  112.  
  113.  
  114. go()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement