Structuring a PDF
Some elements can be automatically generated when creating a PDF to add a better look and structure to your elements. In this recipe, we'll see how to add a header and footer, and how to create links to other parts of the document.
Getting ready
We'll use the fpdf
module to create PDF documents:
$ echo "fpdf==1.7.2" >> requirements.txt
$ pip install -r requirements.txt
How to do it...
- The
structuring_pdf.py
script is available in GitHub here: https://github.com/PacktPublishing/Python-Automation-Cookbook-Second-Edition/blob/master/Chapter05/structuring_pdf.py. The most relevant bits are displayed here:import fpdf from random import randint class StructuredPDF(fpdf.FPDF): LINE_HEIGHT = 5 def footer(self): self.set_y(-15) self.set_font('Times', 'I', 8) page_number = 'Page {number}/{{nb}}'.format(number=self.page_no()) self.cell(0, self...