Beispiel #1
0
 def _flowable(self, n):
     tags = {
         'name': lambda node: self.__name(node),
         'para': lambda node: platypus.Paragraph(self._textual(node), self.styles.para_style_get(node), **(utils.attr_get(node, [], {'bulletText': 'str'}))),
         'xpre': lambda node: platypus.XPreformatted(self._textual(node), self.styles.para_style_get(node), **(utils.attr_get(node, [], {'bulletText': 'str', 'dedent': 'int', 'frags': 'int'}))),
         'pre': lambda node: platypus.Preformatted(self._textual(node), self.styles.para_style_get(node), **(utils.attr_get(node, [], {'bulletText': 'str', 'dedent': 'int'}))),
         'illustration': lambda node: self._illustration(node),
         'blockTable': lambda node: self._table(node),
         'title': lambda node: platypus.Paragraph(self._textual(node), reportlab.lib.styles.getSampleStyleSheet()['Title'], **(utils.attr_get(node, [], {'bulletText': 'str'}))),
         'h1': lambda node: platypus.Paragraph(self._textual(node), reportlab.lib.styles.getSampleStyleSheet()['Heading1'], **(utils.attr_get(node, [], {'bulletText': 'str'}))),
         'h2': lambda node: platypus.Paragraph(self._textual(node), reportlab.lib.styles.getSampleStyleSheet()['Heading2'], **(utils.attr_get(node, [], {'bulletText': 'str'}))),
         'h3': lambda node: platypus.Paragraph(self._textual(node), reportlab.lib.styles.getSampleStyleSheet()['Heading3'], **(utils.attr_get(node, [], {'bulletText': 'str'}))),
         'image': lambda node: platypus.Image(node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255), **(utils.attr_get(node, ['width', 'height', 'preserveAspectRatio', 'anchor']))),
         'spacer': lambda node: platypus.Spacer(
             width=utils.unit_get(node.getAttribute('width') if node.hasAttribute('width') else '1cm'),
             height=utils.unit_get(node.getAttribute('length'))),
         'barCode': lambda node: code39.Extended39(self._textual(node)),
         'pageBreak': lambda node: platypus.PageBreak(),     # FIXME: it is not in RML std
         'nextPage': lambda node: platypus.PageBreak(),
         'condPageBreak': lambda node: platypus.CondPageBreak(**(utils.attr_get(node, ['height']))),
         'setNextTemplate': lambda node: platypus.NextPageTemplate(str(node.getAttribute('name'))),
         'nextFrame': lambda node: platypus.CondPageBreak(1000),  # TODO: change the 1000 !
         'ul': lambda node: self._list(node),
         'keepInFrame': lambda node: self.__keep_in_frame(node),
     }
     retvalue = tags.get(n.localName)
     if retvalue:
         return retvalue(n)
     else:
         sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (n.localName,))
Beispiel #2
0
 def _flowable(self, node):
     if node.localName == 'para':
         style = self.styles.para_style_get(node)
         return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'})))
     elif node.localName == 'name':
         self.styles.names[
             node.getAttribute('id')] = node.getAttribute('value')
         return None
     elif node.localName == 'xpre':
         style = self.styles.para_style_get(node)
         return platypus.XPreformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str', 'dedent': 'int', 'frags': 'int'})))
     elif node.localName == 'pre':
         style = self.styles.para_style_get(node)
         return platypus.Preformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str', 'dedent': 'int'})))
     elif node.localName == 'illustration':
         return self._illustration(node)
     elif node.localName == 'blockTable':
         return self._table(node)
     elif node.localName == 'title':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Title']
         return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'})))
     elif node.localName == 'h1':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading1']
         return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'})))
     elif node.localName == 'h2':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading2']
         return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'})))
     elif node.localName == 'h3':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading3']
         return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText': 'str'})))
     elif node.localName == 'image':
         return platypus.Image(node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255), **(utils.attr_get(node, ['width', 'height', 'preserveAspectRatio', 'anchor'])))
     elif node.localName == 'spacer':
         if node.hasAttribute('width'):
             width = utils.unit_get(node.getAttribute('width'))
         else:
             width = utils.unit_get('1cm')
         length = utils.unit_get(node.getAttribute('length'))
         return platypus.Spacer(width=width, height=length)
     elif node.localName == 'barCode':
         return code39.Extended39(self._textual(node))
     elif node.localName == 'pageBreak':
         return platypus.PageBreak()
     elif node.localName == 'condPageBreak':
         return platypus.CondPageBreak(**(utils.attr_get(node, ['height'])))
     elif node.localName == 'setNextTemplate':
         return platypus.NextPageTemplate(str(node.getAttribute('name')))
     elif node.localName == 'nextFrame':
         return platypus.CondPageBreak(1000)  # TODO: change the 1000 !
     elif node.localName == 'ul':
         return self._list(node)
     else:
         sys.stderr.write(
             'Warning: flowable not yet implemented: %s !\n' % (node.localName,))
         return None
Beispiel #3
0
    def _flowable(self, node, extra_style=None):
        if node.tag=='pto':
            return self._pto(node)
        if node.tag=='para':
            style = self.styles.para_style_get(node)
            if extra_style:
                style.__dict__.update(extra_style)
            result = []
            for i in self._textual(node).split('\n'):
                result.append(platypus.Paragraph(i, style, **(utils.attr_get(node, [], {'bulletText':'str'}))))
            return result
        elif node.tag=='barCode':
            try:
                from reportlab.graphics.barcode import code128
                from reportlab.graphics.barcode import code39
                from reportlab.graphics.barcode import code93
                from reportlab.graphics.barcode import common
                from reportlab.graphics.barcode import fourstate
                from reportlab.graphics.barcode import usps
                from reportlab.graphics.barcode import createBarcodeDrawing

            except ImportError:
                _logger.warning("Cannot use barcode renderers:", exc_info=True)
                return None
            args = utils.attr_get(node, [], {'ratio':'float','xdim':'unit','height':'unit','checksum':'int','quiet':'int','width':'unit','stop':'bool','bearers':'int','barWidth':'float','barHeight':'float'})
            codes = {
                'codabar': lambda x: common.Codabar(x, **args),
                'code11': lambda x: common.Code11(x, **args),
                'code128': lambda x: code128.Code128(str(x), **args),
                'standard39': lambda x: code39.Standard39(str(x), **args),
                'standard93': lambda x: code93.Standard93(str(x), **args),
                'i2of5': lambda x: common.I2of5(x, **args),
                'extended39': lambda x: code39.Extended39(str(x), **args),
                'extended93': lambda x: code93.Extended93(str(x), **args),
                'msi': lambda x: common.MSI(x, **args),
                'fim': lambda x: usps.FIM(x, **args),
                'postnet': lambda x: usps.POSTNET(x, **args),
                'ean13': lambda x: createBarcodeDrawing('EAN13', value=str(x), **args),
                'qrcode': lambda x: createBarcodeDrawing('QR', value=x, **args),
            }
            code = 'code128'
            if node.get('code'):
                code = node.get('code').lower()
            return codes[code](self._textual(node))
        elif node.tag=='name':
            self.styles.names[ node.get('id')] = node.get('value')
            return None
        elif node.tag=='xpre':
            style = self.styles.para_style_get(node)
            return platypus.XPreformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str','dedent':'int','frags':'int'})))
        elif node.tag=='pre':
            style = self.styles.para_style_get(node)
            return platypus.Preformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str','dedent':'int'})))
        elif node.tag=='illustration':
            return  self._illustration(node)
        elif node.tag=='blockTable':
            return  self._table(node)
        elif node.tag=='title':
            styles = reportlab.lib.styles.getSampleStyleSheet()
            style = styles['Title']
            return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str'})))
        elif re.match('^h([1-9]+[0-9]*)$', (node.tag or '')):
            styles = reportlab.lib.styles.getSampleStyleSheet()
            style = styles['Heading'+str(node.tag[1:])]
            return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str'})))
        elif node.tag=='image':
            image_data = False
            if not node.get('file'):
                if node.get('name'):
                    if node.get('name') in self.doc.images:
                        _logger.debug("Image %s read ", node.get('name'))
                        image_data = self.doc.images[node.get('name')].read()
                    else:
                        _logger.warning("Image %s not defined", node.get('name'))
                        return False
                else:
                    import base64
                    newtext = node.text
                    if self.localcontext:
                        newtext = utils._process_text(self, node.text or '')
                    image_data = base64.decodestring(newtext)
                if not image_data:
                    _logger.debug("No inline image data")
                    return False
                image = StringIO(image_data)
            else:
                _logger.debug("Image get from file %s", node.get('file'))
                image = _open_image(node.get('file'), path=self.doc.path)
            return platypus.Image(image, mask=(250,255,250,255,250,255), **(utils.attr_get(node, ['width','height'])))
        elif node.tag=='spacer':
            if node.get('width'):
                width = utils.unit_get(node.get('width'))
            else:
                width = utils.unit_get('1cm')
            length = utils.unit_get(node.get('length'))
            return platypus.Spacer(width=width, height=length)
        elif node.tag=='section':
            return self.render(node)
        elif node.tag == 'pageNumberReset':
            return PageReset()
        elif node.tag in ('pageBreak', 'nextPage'):
            return platypus.PageBreak()
        elif node.tag=='condPageBreak':
            return platypus.CondPageBreak(**(utils.attr_get(node, ['height'])))
        elif node.tag=='setNextTemplate':
            return platypus.NextPageTemplate(str(node.get('name')))
        elif node.tag=='nextFrame':
            return platypus.CondPageBreak(1000)           # TODO: change the 1000 !
        elif node.tag == 'setNextFrame':
            from reportlab.platypus.doctemplate import NextFrameFlowable
            return NextFrameFlowable(str(node.get('name')))
        elif node.tag == 'currentFrame':
            from reportlab.platypus.doctemplate import CurrentFrameFlowable
            return CurrentFrameFlowable(str(node.get('name')))
        elif node.tag == 'frameEnd':
            return EndFrameFlowable()
        elif node.tag == 'hr':
            width_hr=node.get('width') or '100%'
            color_hr=node.get('color')  or 'black'
            thickness_hr=node.get('thickness') or 1
            lineCap_hr=node.get('lineCap') or 'round'
            return platypus.flowables.HRFlowable(width=width_hr,color=color.get(color_hr),thickness=float(thickness_hr),lineCap=str(lineCap_hr))
        else:
            sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.tag,))
            return None
Beispiel #4
0
 def makepar(self, txt, style, **kw):
     if style.wrap:
         return platypus.Paragraph(txt, style, **kw)
     return platypus.XPreformatted(txt, style, **kw)
Beispiel #5
0
class Story:

    def __init__(self,stylesheet):
        self._elements = []
        self.stylesheet=stylesheet
        
    def append(self,elem):
        self._elements.append(elem)
        return elem
        
    def getDefaultParaStyle(self):
        return self._paraStyle


    def formatParagraph(self,**kw):
        # parent paragraph styles don't get updated by manual
        # formattings
        #self._paraStyle = self._paraStyle.child(**kw)
        s = self.getDefaultParaStyle()

        # print s
      
        # s is now usually equal to self._paraStyle, but not for
        # example in a TableRow where it could be the column's style
      
        for k,v in kw.items():
            setattr(s,k,v)

  
    def report(self,rpt):
        raise NotImplementedError

    def img(self,filename,
            width=None,height=None,
            style=None):
        if style is None:
            style = self.getDefaultParaStyle()
        elem = platypus.Image(filename,width,height)
        elem.style = style
        return self.append(elem)

    def table(self,*args,**kw):
        t = TableInstance(*args,**kw) 
        t = platypus.Table(rows,colWidths) #,repeatRows=repeatRows)
        # style of the flowable:
        # t.style = tableInstance._flowStyle.child()
        t.setStyle(TableStyle(cellFormats))


    def par(self,txt,style,wrap=True):
        try:
            txt.decode('utf8')
        except UnicodeDecodeError,e:
            print repr(txt)
            raise
        
        if wrap:
            elem = platypus.Paragraph(txt,style)
        else:
            elem = platypus.XPreformatted(txt,style)
            
        return self.append(elem)
Beispiel #6
0
 def _flowable(self, node):
     if node.localName=='para':
         style = self.styles.para_style_get(node)
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='name':
         self.styles.names[ node.getAttribute('id')] = node.getAttribute('value')
         return None
     elif node.localName=='xpre':
         style = self.styles.para_style_get(node)
         return platypus.XPreformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int','frags':'int'})))
     elif node.localName=='pre':
         style = self.styles.para_style_get(node)
         return platypus.Preformatted(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str','dedent':'int'})))
     elif node.localName=='illustration':
         return  self._illustration(node)
     elif node.localName=='blockTable':
         return  self._table(node)
     elif node.localName=='title':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Title']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h1':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading1']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h2':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading2']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='h3':
         styles = reportlab.lib.styles.getSampleStyleSheet()
         style = styles['Heading3']
         return platypus.Paragraph(
             self._textual(node), style,
             **(utils.getAttrsAsDict(node, [], {'bulletText':'str'})))
     elif node.localName=='image':
         return platypus.Image(
             node.getAttribute('file'), mask=(250, 255, 250, 255, 250, 255),
             **(utils.getAttrsAsDict(node, ['width','height'])))
     elif node.localName=='spacer':
         if node.hasAttribute('width'):
             width = utils.as_pt(node.getAttribute('width'))
         else:
             width = utils.as_pt('1cm')
         length = utils.as_pt(node.getAttribute('length'))
         return platypus.Spacer(width=width, height=length)
     elif node.localName=='pageBreak':
         return platypus.PageBreak()
     elif node.localName=='condPageBreak':
         return platypus.CondPageBreak(**(utils.getAttrsAsDict(node, ['height'])))
     elif node.localName=='setNextTemplate':
         return platypus.NextPageTemplate(str(node.getAttribute('name')))
     elif node.localName=='nextFrame':
         return platypus.CondPageBreak(1000)           # TODO: change the 1000 !
     elif barcode_codes and node.localName=='barCode':
         code = barcode_codes.get(node.getAttribute('code'), Code128)
         attr_types = {'barWidth': 'pt', 'barHeight': 'pt',
                       'fontName': 'text', 'fontSize': 'pt',
                       'humanReadable': 'bool'}
         return code(
             self._textual(node),
             **utils.getAttrsAsDict(node, ['barWidth', 'barHeight'], attr_types))
     else:
         sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.localName,))
         return None
Beispiel #7
0
         'fim': lambda x: usps.FIM(x, **args),
         'postnet': lambda x: usps.POSTNET(x, **args),
     }
     code = 'code128'
     if node.get('code'):
         code = node.get('code').lower()
     return codes[code](self._textual(node))
 elif node.tag == 'name':
     self.styles.names[node.get('id')] = node.get('value')
     return None
 elif node.tag == 'xpre':
     style = self.styles.para_style_get(node)
     return platypus.XPreformatted(
         self._textual(node), style,
         **(utils.attr_get(node, [], {
             'bulletText': 'str',
             'dedent': 'int',
             'frags': 'int'
         })))
 elif node.tag == 'pre':
     style = self.styles.para_style_get(node)
     return platypus.Preformatted(
         self._textual(node), style,
         **(utils.attr_get(node, [], {
             'bulletText': 'str',
             'dedent': 'int'
         })))
 elif node.tag == 'illustration':
     return self._illustration(node)
 elif node.tag == 'blockTable':
     return self._table(node)