[reportlab-users] Has next Page?

J. R. Carroll jrc.csus at gmail.com
Mon Dec 17 08:34:59 EST 2012


Estartu,

I am having to do a custom class so I can generate "Page 1 of <max pages>"
- which is similar to what you are wanting to do; I took an example and
made minor modifications from a code snippet I got off of ActiveState - but
since then I have seen reportlab solutions that come directly from
reportlab. All-the-same, this relates to you because I can imagine that
you could use the same code and instead of printing 'Page # of #" you could
just use the following *PSEUDO CODE:*
*
*
if current_page_number < max_page_number:
print "Turn the Page"
else:
pass # or, print "End of Document"



*Here is the reportlab code I am using (I had to do some quick edits to
remove some company details, so if you see something awry it might be
because I made an edit there - but look down around "Page # of #" below and
that's where you could write something to say "turn to next page") - if you
need help doing this, I can't imagine it taking more than a few minutes,
let me know and I'll be happy to assist:*

# Imports from ReportLab
import reportlab.pdfgen.canvas as canvas

from reportlab.lib.pagesizes import LETTER, A4
from reportlab.lib.units import mm
from reportlab.rl_config import defaultPageSize
from reportlab.lib import colors


class NumberedCanvas(canvas.Canvas):
"""A subclass/child of the superclass canvas.Canvas. Modifications to
the
parent class are to add page numbers, titles, and dates to the save-page
states when self.save() is called.

Additional arguments are initialized upon class instantiation which set
variables native to the type of report being run. """

def __init__(self, *args, **kwargs):
"""
Instantiates a subclass of canvas.Canvas
"""

self.REPORT_TITLE = kwargs.pop('titled', None)
self.title_type = kwargs.pop('title_type', None)
self.time = kwargs.pop('time', None)

canvas.Canvas.__init__(self, *args, **kwargs)
self._saved_page_states = []

def showPage(self):
"""
Overwridden method of canvas.Canvas that counts number of pages
"""

self._saved_page_states.append(dict(self.__dict__))
self._startPage()

def save(self, titled=None):
"""
Save and add page info to each page (page number and date-time
stamp)
"""

num_pages = len(self._saved_page_states)
headerimage = "../../include/images/our-logo.gif"
_title = titled

for state in self._saved_page_states:
self.__dict__.update(state)
self.draw_footer(num_pages)
self.draw_header(headerimage)
canvas.Canvas.showPage(self)

canvas.Canvas.save(self)

def draw_footer(self, page_count):
"""
Draws the footer information (date, filename, page# --- in that
order)
"""

_off_y = 8

self.setFont("Courier", 8)
self.saveState()
self.setStrokeColor(colors.black)
self.drawRightString(defaultPageSize[1] - 110, _off_y * mm, "Page
%d of %d" % (self._pageNumber, page_count))
self.drawString(20 * mm, _off_y * mm, self.time)
self.drawCentredString((self._pagesize[0] / 2), _off_y * mm,
self.REPORT_TITLE)
self.restoreState()




----


J. R. Carroll
Cell: (650) 776-6613
www.jrcresearch.net
www.ontvp.com
Email: jrcarroll at jrcresearch.net
jrcarroll at hurtzlab.com
jrc.csus at gmail.com
<https://www.facebook.com/J.R.Car>
<https://twitter.com/jNammer><http://www.linkedin.com/in/jrcarroll>




On Mon, Dec 17, 2012 at 1:42 AM, Gerhard Schmidt <estartu at augusta.de> wrote:


> Estartu

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20121217/7cab4b9c/attachment.html>


More information about the reportlab-users mailing list