Exemple #1
0
 def beforeDrawPage(self, canvas, doc):
     canvas.setFont(serif_font, 8)
     canvas.saveState()
     if pdfstyles.show_title_page_footer:
         canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert)
         footertext = [_(titlepagefooter)]
         if pdfstyles.show_creation_date:
             locale.setlocale(locale.LC_ALL, "")
             footertext.append(
                 pdfstyles.creation_date_txt % time.strftime(pdfstyles.creation_date_format, time.localtime())
             )
         lines = [formatter.cleanText(line, escape=False) for line in footertext]
         txt = "<br/>".join(line if isinstance(line, unicode) else unicode(line, "utf-8") for line in lines)
         p = Paragraph(txt, text_style(mode="footer"))
         w, h = p.wrap(print_width, print_height)
         canvas.translate((page_width - w) / 2.0, footer_margin_vert - h - 0.25 * cm)
         p.canv = canvas
         p.draw()
     canvas.restoreState()
     if self.cover:
         width, height = self._scale_img(pdfstyles.title_page_image_size, self.cover)
         if pdfstyles.title_page_image_pos[0] is None:
             x = (page_width - width) / 2.0
         else:
             x = max(0, min(page_width - width, pdfstyles.title_page_image_pos[0]))
         if pdfstyles.title_page_image_pos[1] is None:
             y = (page_height - height) / 2.0
         else:
             y = max(0, min(page_height - height, pdfstyles.title_page_image_pos[1]))
         canvas.drawImage(self.cover, x, y, width, height)
Exemple #2
0
 def draw(self):
     """Draw rotated canvas"""
     self.canv.saveState()
     self.canv.translate(0, 0)
     self.canv.rotate(self.angle)
     Paragraph.draw(self)
     self.canv.restoreState()
Exemple #3
0
class Figure(Flowable):

    def __init__(self, imgFile, captionTxt, captionStyle, imgWidth=None, imgHeight=None, margin=(0, 0, 0, 0),
                 padding=(0, 0, 0, 0), align=None, borderColor=(0.75, 0.75, 0.75), no_mask=False, url=None):

        imgFile = imgFile
        self.imgPath = imgFile
        # workaround for http://code.pediapress.com/wiki/ticket/324
        # see http://two.pairlist.net/pipermail/reportlab-users/2008-October/007526.html
        if no_mask:
            self.i = Image(imgFile, width=imgWidth, height=imgHeight, mask=None)
        else:
            self.i = Image(imgFile, width=imgWidth, height=imgHeight)
        self.imgWidth = imgWidth
        self.imgHeight = imgHeight
        self.c = Paragraph(captionTxt, style=captionStyle)
        self.margin = margin  # 4-tuple. margins in order: top, right, bottom, left
        self.padding = padding  # same as above
        self.borderColor = borderColor
        self.align = align
        self.cs = captionStyle
        self.captionTxt = captionTxt
        self.availWidth = None
        self.availHeight = None
        self.url = url

    def draw(self):
        canv = self.canv
        if self.align == "center":
            canv.translate((self.availWidth - self.width) / 2, 0)
        canv.saveState()
        canv.setStrokeColor(Color(self.borderColor[0], self.borderColor[1], self.borderColor[2]))
        canv.rect(self.margin[3], self.margin[2], self.boxWidth, self.boxHeight)
        canv.restoreState()
        canv.translate(self.margin[3] + self.padding[3], self.margin[2] + self.padding[2] - 2)
        self.c.canv = canv
        self.c.draw()
        canv.translate((self.boxWidth - self.padding[1] - self.padding[3] - self.i.drawWidth) / 2, self.captionHeight + 2)
        self.i.canv = canv
        self.i.draw()
        if self.url:
            frags = urlparse.urlsplit(self.url.encode('utf-8'))
            clean_url = urlparse.urlunsplit((frags.scheme,
                                             frags.netloc,
                                             urllib.quote(frags.path, safe='/'),
                                             urllib.quote(frags.query, safe='=&'),
                                             frags.fragment,)).decode('utf-8')
            canv.linkURL(clean_url, (0, 0, self.imgWidth, self.imgHeight), relative=1, thickness=0)

    def wrap(self, availWidth, availHeight):
        self.availWidth = availWidth
        self.availHeight = availHeight
        contentWidth = max(self.i.drawWidth, self.c.wrap(self.i.drawWidth, availHeight)[0])
        self.boxWidth = contentWidth + self.padding[1] + self.padding[3]
        (self.captionWidth, self.captionHeight) = self.c.wrap(contentWidth, availHeight)
        self.captionHeight += self.cs.spaceBefore + self.cs.spaceAfter
        self.boxHeight = self.i.drawHeight + self.captionHeight + self.padding[0] + self.padding[2]
        self.width = self.boxWidth + self.margin[1] + self.margin[3]
        self.height = self.boxHeight + self.margin[0] + self.margin[2]
        return (self.width, self.height)
Exemple #4
0
 def beforeDrawPage(self,canvas,doc):
     canvas.setFont(serif_font,8)
     canvas.saveState()
     if pdfstyles.show_title_page_footer:
         canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert )
         footertext = [_(titlepagefooter)]
         if pdfstyles.show_creation_date:
             footertext.append('PDF generated at: %s' % strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()))
         p = Paragraph('<br/>'.join([formatter.cleanText(line, escape=False) for line in footertext]),
                       text_style(mode='footer'))
         w,h = p.wrap(print_width, print_height)
         canvas.translate( (page_width-w)/2.0, footer_margin_vert - h - 0.25*cm)
         p.canv = canvas
         p.draw()
     canvas.restoreState()
     if self.cover:
         width, height = self._scale_img(pdfstyles.title_page_image_size, self.cover)
         if pdfstyles.title_page_image_pos[0] == None:
             x = (page_width - width) / 2.0
         else:
             x = max(0, min(page_width-width, pdfstyles.title_page_image_pos[0]))
         if pdfstyles.title_page_image_pos[1] == None:
             y = (page_height - height) / 2.0
         else:
             y = max(0, min(page_height-height, pdfstyles.title_page_image_pos[1]))
         canvas.drawImage(self.cover, x, y, width , height)
Exemple #5
0
    def draw(self):

        # Add outline entry
        self.canv.bookmarkHorizontal(self.parent_id, 0, 0 + self.height)
        # self.section_header_depth is for Issue 391
        if self.canv.firstSect and self.level < self.section_header_depth:
            self.canv.sectName = self.stext
            self.canv.firstSect = False
            if self.snum is not None:
                self.canv.sectNum = self.snum
            else:
                self.canv.sectNum = ""
        self.canv.addOutlineEntry(self.stext, self.parent_id, int(self.level),
                                  False)
        Paragraph.draw(self)
Exemple #6
0
    def draw(self):
        # Don't forget the angles have been transposed from CW (pypdf2) to CCW (reportlab) here in the __init__ method!
        if self.angle == 0:
            tx, ty = 0, self.my_height / 2.0
        elif self.angle == 180:
            tx, ty = self.width, -self.my_height
        elif self.angle == 270:
            tx, ty = self.my_width, 0
        elif self.angle == 90:
            tx, ty = -self.my_width / 2.0, -self.width
        else:
            tx, ty = 0, 0

        self.canv.saveState()
        self.canv.translate(tx, ty)
        self.canv.rotate(self.angle)
        Paragraph.draw(self)
        self.canv.restoreState()
Exemple #7
0
 def beforeDrawPage(self, canvas, doc):
     canvas.setFont(serif_font, 8)
     canvas.saveState()
     if pdfstyles.show_title_page_footer:
         canvas.line(footer_margin_hor, footer_margin_vert,
                     page_width - footer_margin_hor, footer_margin_vert)
         footertext = [_(titlepagefooter)]
         if pdfstyles.show_creation_date:
             locale.setlocale(locale.LC_ALL, '')
             footertext.append(pdfstyles.creation_date_txt % time.strftime(
                 pdfstyles.creation_date_format, time.localtime()))
         lines = [
             formatter.cleanText(line, escape=False) for line in footertext
         ]
         txt = '<br/>'.join(
             line if isinstance(line, unicode) else unicode(line, 'utf-8')
             for line in lines)
         p = Paragraph(txt, text_style(mode='footer'))
         w, h = p.wrap(print_width, print_height)
         canvas.translate((page_width - w) / 2.0,
                          footer_margin_vert - h - 0.25 * cm)
         p.canv = canvas
         p.draw()
     canvas.restoreState()
     if self.cover:
         width, height = self._scale_img(pdfstyles.title_page_image_size,
                                         self.cover)
         if pdfstyles.title_page_image_pos[0] == None:
             x = (page_width - width) / 2.0
         else:
             x = max(
                 0,
                 min(page_width - width, pdfstyles.title_page_image_pos[0]))
         if pdfstyles.title_page_image_pos[1] == None:
             y = (page_height - height) / 2.0
         else:
             y = max(
                 0,
                 min(page_height - height,
                     pdfstyles.title_page_image_pos[1]))
         canvas.drawImage(self.cover, x, y, width, height)
Exemple #8
0
    def draw(self):

        # Add outline entry
        self.canv.bookmarkHorizontal(self.parent_id, 0, 0 + self.height)
        # self.section_header_depth is for Issue 391
        if self.canv.firstSect and self.level < self.section_header_depth:
            self.canv.sectName = self.stext
            self.canv.firstSect = False
            if self.snum is not None:
                self.canv.sectNum = self.snum
            else:
                self.canv.sectNum = ""

        # Close entry if it's below the toc depth
        # (we add 1 to self.level as it's zero-indexed, but toc_depth is one-indexed.)
        closed = None
        if (self.level + 1) >= self.toc_depth:
            closed = True

        self.canv.addOutlineEntry(self.stext, self.parent_id, int(self.level), closed)
        Paragraph.draw(self)
Exemple #9
0
 def beforeDrawPage(self, canvas, doc):
     canvas.setFont(serif_font, 8)
     canvas.saveState()
     if pdfstyles.show_title_page_footer:
         canvas.line(footer_margin_hor, footer_margin_vert,
                     page_width - footer_margin_hor, footer_margin_vert)
         footertext = [_(titlepagefooter)]
         if pdfstyles.show_creation_date:
             footertext.append(
                 'PDF generated at: %s' %
                 strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()))
         p = Paragraph(
             '<br/>'.join([
                 formatter.cleanText(line, escape=False)
                 for line in footertext
             ]), text_style(mode='footer'))
         w, h = p.wrap(print_width, print_height)
         canvas.translate((page_width - w) / 2.0, 0.2 * cm)
         p.canv = canvas
         p.draw()
     canvas.restoreState()
     if self.cover:
         width, height = self._scale_img(pdfstyles.title_page_image_size,
                                         self.cover)
         if pdfstyles.title_page_image_pos[0] == None:
             x = (page_width - width) / 2.0
         else:
             x = max(
                 0,
                 min(page_width - width, pdfstyles.title_page_image_pos[0]))
         if pdfstyles.title_page_image_pos[1] == None:
             y = (page_height - height) / 2.0
         else:
             y = max(
                 0,
                 min(page_height - height,
                     pdfstyles.title_page_image_pos[1]))
         canvas.drawImage(self.cover, x, y, width, height)
Exemple #10
0
 def beforeDrawPage(self,canvas,doc):
     canvas.setFont(serif_font,8)
     canvas.saveState()
     if pdfstyles.show_title_page_footer:
         canvas.line(footer_margin_hor, footer_margin_vert, page_width - footer_margin_hor, footer_margin_vert )
         footertext = [_(titlepagefooter)]
         if pdfstyles.show_creation_date:
             footertext.append('PDF generated at: %s' % strftime("%a, %d %b %Y %H:%M:%S %Z", gmtime()))
         p = Paragraph('<br/>'.join([formatter.cleanText(line, escape=False) for line in footertext]),
                       text_style(mode='footer'))
         w,h = p.wrap(print_width, print_height)
         canvas.translate( (page_width-w)/2.0, 0.2*cm)
         p.canv = canvas
         p.draw()
     canvas.restoreState()
     if self.cover:
         width = 12 * cm
         img = Image.open(self.cover)
         w,h = img.size
         height = width/w*h 
         x = (page_width - width) / 2.0
         y = (page_height - height) / 2.0
         canvas.drawImage(self.cover, x, y, width , height)
Exemple #11
0
 def draw(self):
     self.canv.saveState()
     self.canv.translate(0, 0)
     self.canv.rotate(self.angle)
     Paragraph.draw(self)
     self.canv.restoreState()
Exemple #12
0
 def draw(self):
     self.canv.saveState()
     self.canv.translate(0,0)
     self.canv.rotate(self.angle)
     Paragraph.draw(self)
     self.canv.restoreState()
Exemple #13
0
    def draw(self):

        # Insert page number
        '''
        if 0: #for line in self.blPara.lines:
            try:
                for frag in line.words:
                    #print 111,frag.pageNumber, frag.text
                    if frag.pageNumber:
                        frag.text = str(self.canv.getPageNumber())
            except Exception, e:
                log.debug("PmlParagraph", exc_info=1)
        '''

        # Create outline
        if getattr(self, "outline", False):

            # Check level and add all levels
            last = getattr(self.canv, "outlineLast", -1) + 1
            while last < self.outlineLevel:
                # print "(OUTLINE",  last, self.text
                key = getUID()
                self.canv.bookmarkPage(key)
                self.canv.addOutlineEntry(
                    self.text,
                    key,
                    last,
                    not self.outlineOpen)
                last += 1
            self.canv.outlineLast = self.outlineLevel

            key = getUID()
            # print " OUTLINE", self.outlineLevel, self.text
            self.canv.bookmarkPage(key)
            self.canv.addOutlineEntry(
                self.text,
                key,
                self.outlineLevel,
                not self.outlineOpen)
            last += 1

        #else:
        #    print repr(self.text)[:80]
        
        # Draw the background and borders here before passing control on to
        # ReportLab. This is because ReportLab can't handle the individual
        # components of the border independently. This will also let us
        # support more border styles eventually.
        canvas = self.canv
        style = self.style
        bg = style.backColor
        leftIndent = style.leftIndent
        bp = style.borderPadding

        x = leftIndent - bp
        y = -bp
        w = self.width - (leftIndent + style.rightIndent) + 2 * bp
        h = self.height + 2 * bp
        
        canvas.saveState()
        if bg:
            # draw a filled rectangle (with no stroke) using bg color
            canvas.setFillColor(bg)
            canvas.rect(x, y, w, h, fill=1, stroke=0)
            
        def _drawBorderLine(bstyle, width, color, x1, y1, x2, y2):            
            # We need width and border style to be able to draw a border
            if width and getBorderStyle(bstyle):
                # If no color for border is given, the text color is used (like defined by W3C)
                if color is None:
                    color = style.textColor
                canvas.setStrokeColor(color)
                canvas.setLineWidth(width)
                canvas.line(x1, y1, x2, y2)
        
        _drawBorderLine(style.borderTopStyle,
                        style.borderTopWidth,
                        style.borderTopColor,                        
                        x, y+h, x+w, y+h)
        _drawBorderLine(style.borderBottomStyle,
                        style.borderBottomWidth,
                        style.borderBottomColor,
                        x, y, x+w, y)
        _drawBorderLine(style.borderLeftStyle,
                        style.borderLeftWidth,
                        style.borderLeftColor,
                        x, y, x, y+h)
        _drawBorderLine(style.borderRightStyle,
                        style.borderRightWidth,
                        style.borderRightColor,
                        x+w, y, x+w, y+h)
        
        canvas.restoreState()
                    
        # we need to hide the bg color (if any) so Paragraph won't try to draw it again
        style.backColor = None
        
        # offset the origin to compensate for the padding
        canvas.saveState()
        canvas.translate(style.paddingLeft, -style.paddingTop)
        
        # Call the base class draw method to finish up
        Paragraph.draw(self)
        canvas.restoreState()
        
        # Reset color because we need it again if we run 2-PASS like we 
        # do when using TOC
        style.backColor = bg
Exemple #14
0
    def draw(self):

        # Insert page number
        '''
        if 0: #for line in self.blPara.lines:
            try:
                for frag in line.words:
                    #print 111,frag.pageNumber, frag.text
                    if frag.pageNumber:
                        frag.text = str(self.canv.getPageNumber())
            except Exception, e:
                log.debug("PmlParagraph", exc_info=1)
        '''

        # Create outline
        if getattr(self, "outline", False):

            # Check level and add all levels
            last = getattr(self.canv, "outlineLast", -1) + 1
            while last < self.outlineLevel:
                # print "(OUTLINE",  last, self.text
                key = getUID()
                self.canv.bookmarkPage(key)
                self.canv.addOutlineEntry(self.text, key, last,
                                          not self.outlineOpen)
                last += 1
            self.canv.outlineLast = self.outlineLevel

            key = getUID()
            # print " OUTLINE", self.outlineLevel, self.text
            self.canv.bookmarkPage(key)
            self.canv.addOutlineEntry(self.text, key, self.outlineLevel,
                                      not self.outlineOpen)
            last += 1

        #else:
        #    print repr(self.text)[:80]

        # Draw the background and borders here before passing control on to
        # ReportLab. This is because ReportLab can't handle the individual
        # components of the border independently. This will also let us
        # support more border styles eventually.
        canvas = self.canv
        style = self.style
        bg = style.backColor
        leftIndent = style.leftIndent
        bp = style.borderPadding

        x = leftIndent - bp
        y = -bp
        w = self.width - (leftIndent + style.rightIndent) + 2 * bp
        h = self.height + 2 * bp

        canvas.saveState()
        if bg:
            # draw a filled rectangle (with no stroke) using bg color
            canvas.setFillColor(bg)
            canvas.rect(x, y, w, h, fill=1, stroke=0)

        def _drawBorderLine(bstyle, width, color, x1, y1, x2, y2):
            # We need width and border style to be able to draw a border
            if width and getBorderStyle(bstyle):
                # If no color for border is given, the text color is used (like defined by W3C)
                if color is None:
                    color = style.textColor
                canvas.setStrokeColor(color)
                canvas.setLineWidth(width)
                canvas.line(x1, y1, x2, y2)

        _drawBorderLine(style.borderTopStyle, style.borderTopWidth,
                        style.borderTopColor, x, y + h, x + w, y + h)
        _drawBorderLine(style.borderBottomStyle, style.borderBottomWidth,
                        style.borderBottomColor, x, y, x + w, y)
        _drawBorderLine(style.borderLeftStyle, style.borderLeftWidth,
                        style.borderLeftColor, x, y, x, y + h)
        _drawBorderLine(style.borderRightStyle, style.borderRightWidth,
                        style.borderRightColor, x + w, y, x + w, y + h)

        canvas.restoreState()

        # we need to hide the bg color (if any) so Paragraph won't try to draw it again
        style.backColor = None

        # offset the origin to compensate for the padding
        canvas.saveState()
        canvas.translate(style.paddingLeft, -style.paddingTop)

        # Call the base class draw method to finish up
        Paragraph.draw(self)
        canvas.restoreState()

        # Reset color because we need it again if we run 2-PASS like we
        # do when using TOC
        style.backColor = bg
Exemple #15
0
 def draw(self):
     Paragraph.draw(self)
     TOC.append((self._label,self.canv.getPageNumber(),self._key))
     self.canv.bookmarkHorizontal('TOC_%s' % self._key,0,+20)
 def draw(self):
     Paragraph.draw(self)
     TOC.append((self._label, self.canv.getPageNumber(), self._key))
     self.canv.bookmarkHorizontal('TOC_%s' % self._key, 0, +20)
Exemple #17
0
 def draw(self):
     self.canv.rotate(90)
     self.canv.translate(1, -self.height)
     Paragraph.draw(self)