def __init__(self, filename, **kw):
     self.allowSplitting = 0
     BaseDocTemplate.__init__(self, filename, **kw)
     self.addPageTemplates([
         PageTemplate(
             'normal',
             [
                 Frame(inch,
                       inch,
                       6.27 * inch,
                       9.69 * inch,
                       id='first',
                       topPadding=0,
                       rightPadding=0,
                       leftPadding=0,
                       bottomPadding=0,
                       showBoundary=ShowBoundaryValue(color="red"))
             ],
             onPage=onPage('normal'),
         ),
         PageTemplate(
             'auto',
             [
                 Frame(inch,
                       inch,
                       6.27 * inch,
                       9.69 * inch,
                       id='first',
                       topPadding=0,
                       rightPadding=0,
                       leftPadding=0,
                       bottomPadding=0,
                       showBoundary=ShowBoundaryValue(color="red"))
             ],
             onPage=onPage('auto'),
             autoNextPageTemplate='autoFollow',
         ),
         PageTemplate(
             'autoFollow',
             [
                 Frame(inch,
                       inch,
                       6.27 * inch,
                       9.69 * inch,
                       id='first',
                       topPadding=0,
                       rightPadding=0,
                       leftPadding=0,
                       bottomPadding=0,
                       showBoundary=ShowBoundaryValue(color="red"))
             ],
             onPage=onPage('autoFollow'),
         ),
     ])
 def test2(self):
     '''ensure showBoundaryValue works as expected'''
     from reportlab.platypus.frames import ShowBoundaryValue
     assert (1 if ShowBoundaryValue(width=1) else 0) == 1
     assert (1 if ShowBoundaryValue(color=None, width=1) else 0) == 0
     assert (1 if ShowBoundaryValue(width=-1) else 0) == 0
     assert bool(ShowBoundaryValue(width=1)) == True
     assert bool(ShowBoundaryValue(color=None, width=1)) == False
     assert bool(ShowBoundaryValue(width=-1)) == False
 def __init__(self, filename, **kw):
     self.allowSplitting = 0
     kw['showBoundary'] = 1
     BaseDocTemplate.__init__(self, filename, **kw)
     self.addPageTemplates([
         PageTemplate(
             'normal',
             [
                 Frame(inch,
                       inch,
                       6.27 * inch,
                       9.69 * inch,
                       id='first',
                       topPadding=0,
                       rightPadding=0,
                       leftPadding=0,
                       bottomPadding=0,
                       showBoundary=ShowBoundaryValue(color="red"))
             ],
         ),
     ])
Esempio n. 4
0
 def balanced(spam=None):
     L = [
         Paragraph(
             """
         What concepts does PLATYPUS deal with?
         """, styleSheet['Heading2']),
         Paragraph(
             """
         The central concepts in PLATYPUS are Flowable Objects, Frames, Flow
         Management, Styles and Style Sheets, Paragraphs and Tables.  This is
         best explained in contrast to PDFgen, the layer underneath PLATYPUS.
         PDFgen is a graphics library, and has primitive commans to draw lines
         and strings.  There is nothing in it to manage the flow of text down
         the page.  PLATYPUS works at the conceptual level fo a desktop publishing
         package; you can write programs which deal intelligently with graphic
         objects and fit them onto the page.
         """, styleSheet['BodyText']),
         Paragraph(
             """
         How is this document organized?
         """, styleSheet['Heading2']),
         Paragraph(
             """
         Since this is a test script, we'll just note how it is organized.
         the top of each page contains commentary.  The bottom half contains
         example drawings and graphic elements to whicht he commentary will
         relate.  Down below, you can see the outline of a text frame, and
         various bits and pieces within it.  We'll explain how they work
         on the next page.
         """, styleSheet['BodyText']),
     ]
     if spam:
         for text in getParagraphs(spam):
             L.append(Paragraph(text, styleSheet['BodyText']))
     story.append(
         BalancedColumns(L,
                         spaceBefore=20,
                         spaceAfter=30,
                         showBoundary=ShowBoundaryValue(
                             color=colors.lightgreen, width=2)))
Esempio n. 5
0
def build_frame_style(context, style_css, inline_attributes=freeze({})):
    frame_attr = {}
    border = {}

    keys = style_css.keys()
    keys.sort()
    # revert keys to avoid css inheritance
    for key in keys:
        value = style_css[key]
        if key.startswith('border'):
            border.update(p_border_style(key, value))
        if key in ('height', 'width'):
            frame_attr[key] = value
        elif key.startswith('padding'):
            frame_attr.update(frame_padding_style(key, value))
        elif key.startswith('margin'):
            frame_attr.update(body_margin_style(key, value))

    if border:
        sb = ShowBoundaryValue(get_color(border.get('borderColor', 'black')),
                               border.get('borderWidth', 1))
        frame_attr['showBoundary'] = sb
    return frame_attr
Esempio n. 6
0
    def atPage(self, name, pseudopage, declarations):
        c = self.c
        data = {}
        name = name or "body"
        pageBorder = None

        if declarations:
            result = self.ruleset([self.selector('*')], declarations)

            if declarations:
                data = result[0].values()[0]
                pageBorder = data.get("-pdf-frame-border", None)

        if name in c.templateList:
            log.warn(self.c.warning("template '%s' has already been defined", name))

        if "-pdf-page-size" in data:
            c.pageSize = xhtml2pdf.default.PML_PAGESIZES.get(str(data["-pdf-page-size"]).lower(), c.pageSize)

        isLandscape = False
        if "size" in data:
            size = data["size"]
            if type(size) is not types.ListType:
                size = [size]
            sizeList = []
            for value in size:
                valueStr = str(value).lower()
                if type(value) is types.TupleType:
                    sizeList.append(getSize(value))
                elif valueStr == "landscape":
                    isLandscape = True
                elif valueStr in xhtml2pdf.default.PML_PAGESIZES:
                    c.pageSize = xhtml2pdf.default.PML_PAGESIZES[valueStr]
                else:
                    log.warn(c.warning("Unknown size value for @page"))

            if len(sizeList) == 2:
                c.pageSize = tuple(sizeList)
            if isLandscape:
                c.pageSize = landscape(c.pageSize)

        padding_top = self._getFromData(data, 'padding-top', 0, getSize)
        padding_left = self._getFromData(data, 'padding-left', 0, getSize)
        padding_right = self._getFromData(data, 'padding-right', 0, getSize)
        padding_bottom = self._getFromData(data, 'padding-bottom', 0, getSize)
        border_color = self._getFromData(data, ('border-top-color', 'border-bottom-color',\
                                                'border-left-color', 'border-right-color'), None, getColor)
        border_width = self._getFromData(data, ('border-top-width', 'border-bottom-width',\
                                                'border-left-width', 'border-right-width'), 0, getSize)

        for prop in ("margin-top", "margin-left", "margin-right", "margin-bottom",
                     "top", "left", "right", "bottom", "width", "height"):
            if prop in data:
                c.frameList.append(self._pisaAddFrame(name, data, first=True, border=pageBorder, size=c.pageSize))
                break

        # Frames have to be calculated after we know the pagesize
        frameList = []
        staticList = []
        for fname, static, border, x, y, w, h, fdata in c.frameList:
            fpadding_top = self._getFromData(fdata, 'padding-top', padding_top, getSize)
            fpadding_left = self._getFromData(fdata, 'padding-left', padding_left, getSize)
            fpadding_right = self._getFromData(fdata, 'padding-right', padding_right, getSize)
            fpadding_bottom = self._getFromData(fdata, 'padding-bottom', padding_bottom, getSize)
            fborder_color = self._getFromData(fdata, ('border-top-color', 'border-bottom-color',\
                                                      'border-left-color', 'border-right-color'), border_color, getColor)
            fborder_width = self._getFromData(fdata, ('border-top-width', 'border-bottom-width',\
                                                      'border-left-width', 'border-right-width'), border_width, getSize)

            if border or pageBorder:
                frame_border = ShowBoundaryValue()
            else:
                frame_border = ShowBoundaryValue(color=fborder_color, width=fborder_width)

            #fix frame sizing problem.
            if static:
                x, y, w, h = getFrameDimensions(fdata, c.pageSize[0], c.pageSize[1])
            x, y, w, h = getCoords(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warn(self.c.warning("Negative width or height of frame. Check @frame definitions."))

            frame = Frame(
                x, y, w, h,
                id=fname,
                leftPadding=fpadding_left,
                rightPadding=fpadding_right,
                bottomPadding=fpadding_bottom,
                topPadding=fpadding_top,
                showBoundary=frame_border)

            if static:
                frame.pisaStaticStory = []
                c.frameStatic[static] = [frame] + c.frameStatic.get(static, [])
                staticList.append(frame)
            else:
                frameList.append(frame)

        background = data.get("background-image", None)
        if background:
            #should be relative to the css file
            background = self.c.getFile(background, relative=self.c.cssParser.rootPath)

        if not frameList:
            log.warn(c.warning("missing explicit frame definition for content or just static frames"))
            fname, static, border, x, y, w, h, data = self._pisaAddFrame(name, data, first=True, border=pageBorder,
                                                                         size=c.pageSize)
            x, y, w, h = getCoords(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warn(c.warning("Negative width or height of frame. Check @page definitions."))

            if border or pageBorder:
                frame_border = ShowBoundaryValue()
            else:
                frame_border = ShowBoundaryValue(color=border_color, width=border_width)

            frameList.append(Frame(
                x, y, w, h,
                id=fname,
                leftPadding=padding_left,
                rightPadding=padding_right,
                bottomPadding=padding_bottom,
                topPadding=padding_top,
                showBoundary=frame_border))

        pt = PmlPageTemplate(
            id=name,
            frames=frameList,
            pagesize=c.pageSize,
        )
        pt.pisaStaticList = staticList
        pt.pisaBackground = background
        pt.pisaBackgroundList = c.pisaBackgroundList

        if isLandscape:
            pt.pageorientation = pt.LANDSCAPE

        c.templateList[name] = pt
        c.template = None
        c.frameList = []
        c.frameStaticList = []

        return {}, {}
Esempio n. 7
0
    def atPage(self, name, pseudopage, data, isLandscape, pageBorder):
        c = self.c
        name = name or "body"

        if name in c.templateList:
            log.warning(
                self.c.warning("template '%s' has already been defined", name))

        padding_top = self._getFromData(data, 'padding-top', 0, getSize)
        padding_left = self._getFromData(data, 'padding-left', 0, getSize)
        padding_right = self._getFromData(data, 'padding-right', 0, getSize)
        padding_bottom = self._getFromData(data, 'padding-bottom', 0, getSize)
        border_color = self._getFromData(
            data, ('border-top-color', 'border-bottom-color',
                   'border-left-color', 'border-right-color'), None, getColor)
        border_width = self._getFromData(
            data, ('border-top-width', 'border-bottom-width',
                   'border-left-width', 'border-right-width'), 0, getSize)

        for prop in ("margin-top", "margin-left", "margin-right",
                     "margin-bottom", "top", "left", "right", "bottom",
                     "width", "height"):
            if prop in data:
                c.frameList.append(
                    self._pisaAddFrame(name,
                                       data,
                                       first=True,
                                       border=pageBorder,
                                       size=c.pageSize))
                break

        # Frames have to be calculated after we know the pagesize
        frameList = []
        staticList = []
        for fname, static, border, x, y, w, h, fdata in c.frameList:
            fpadding_top = self._getFromData(fdata, 'padding-top', padding_top,
                                             getSize)
            fpadding_left = self._getFromData(fdata, 'padding-left',
                                              padding_left, getSize)
            fpadding_right = self._getFromData(fdata, 'padding-right',
                                               padding_right, getSize)
            fpadding_bottom = self._getFromData(fdata, 'padding-bottom',
                                                padding_bottom, getSize)
            fborder_color = self._getFromData(
                fdata, ('border-top-color', 'border-bottom-color',
                        'border-left-color', 'border-right-color'),
                border_color, getColor)
            fborder_width = self._getFromData(
                fdata, ('border-top-width', 'border-bottom-width',
                        'border-left-width', 'border-right-width'),
                border_width, getSize)

            if border or pageBorder:

                frame_border = ShowBoundaryValue(
                    width=int(border))  #frame_border = ShowBoundaryValue() to
                #frame_border = ShowBoundaryValue(width=int(border))
            else:
                frame_border = ShowBoundaryValue(color=fborder_color,
                                                 width=fborder_width)

            # fix frame sizing problem.
            if static:
                x, y, w, h = getFrameDimensions(fdata, c.pageSize[0],
                                                c.pageSize[1])
            x, y, w, h = getCoords(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warning(
                    self.c.warning(
                        "Negative width or height of frame. Check @frame definitions."
                    ))

            frame = Frame(x,
                          y,
                          w,
                          h,
                          id=fname,
                          leftPadding=fpadding_left,
                          rightPadding=fpadding_right,
                          bottomPadding=fpadding_bottom,
                          topPadding=fpadding_top,
                          showBoundary=frame_border)

            if static:
                frame.pisaStaticStory = []
                c.frameStatic[static] = [frame] + c.frameStatic.get(static, [])
                staticList.append(frame)
            else:
                frameList.append(frame)

        background = data.get("background-image", None)
        if background:
            # should be relative to the css file
            background = self.c.getFile(background,
                                        relative=self.c.cssParser.rootPath)

        if not frameList:
            log.warning(
                c.warning(
                    "missing explicit frame definition for content or just static frames"
                ))
            fname, static, border, x, y, w, h, data = self._pisaAddFrame(
                name, data, first=True, border=pageBorder, size=c.pageSize)
            x, y, w, h = getCoords(x, y, w, h, c.pageSize)
            if w <= 0 or h <= 0:
                log.warning(
                    c.warning(
                        "Negative width or height of frame. Check @page definitions."
                    ))

            if border or pageBorder:
                frame_border = ShowBoundaryValue()
            else:
                frame_border = ShowBoundaryValue(color=border_color,
                                                 width=border_width)

            frameList.append(
                Frame(x,
                      y,
                      w,
                      h,
                      id=fname,
                      leftPadding=padding_left,
                      rightPadding=padding_right,
                      bottomPadding=padding_bottom,
                      topPadding=padding_top,
                      showBoundary=frame_border))

        pt = PmlPageTemplate(
            id=name,
            frames=frameList,
            pagesize=c.pageSize,
        )
        pt.pisaStaticList = staticList
        pt.pisaBackground = background
        pt.pisaBackgroundList = c.pisaBackgroundList

        if isLandscape:
            pt.pageorientation = pt.LANDSCAPE

        c.templateList[name] = pt
        c.template = None
        c.frameList = []
        c.frameStaticList = []

        return {}, {}