Django Reportlab Header Footer Example

Last time we looked at how to generate a very simple PDF using ReportLab and Django, ReportLab and Django – Part 1 – The Set Up and a Basic Example.  This time let's make the PDF a little bit more interesting with some headers and footers.  ReportLab gives a pretty good amount of control when it comes to adding headers and footers to your PDF.  To start off let's bring back the simple __init__ method we had from last time and add a header/footer method to it.from reportlab.lib.pagesizes import letter, A4 from reportlab.lib.styles import getSampleStyleSheet class MyPrint: def __init__(self, buffer, pagesize): self.buffer = buffer if pagesize == 'A4': self.pagesize = A4 elif pagesize == 'Letter': self.pagesize = letter self.width, self.height = self.pagesize @staticmethod def _header_footer(canvas, doc): # Save the state of our canvas so we can draw on it canvas.saveState() styles = getSample...