How to create header and footer for multiple pages in 'borb'?

198 views Asked by At

I'm using Borb for Python. I can’t find in the examples or documentation how to create a header and footer for multi-page documents. I would also like the footer to indicate the current page of all. Can someone share an example?

https://github.com/jorisschellekens/borb-examples#93-creating-a-stunning-flyer There is an example here with creating a footer, but it doesn’t say how to place it on several pages

1

There are 1 answers

2
Joris Schellekens On

disclaimer: I am the author of borb, the library used in the answer

This is a quick little test I wrote to add page numbers to a Document

import random
from _decimal import Decimal

from borb.pdf import Alignment
from borb.pdf import Document
from borb.pdf import Lipsum
from borb.pdf import PDF
from borb.pdf import Page
from borb.pdf import PageLayout
from borb.pdf import Paragraph
from borb.pdf import SingleColumnLayout
from borb.pdf.canvas.geometry.rectangle import Rectangle
from tests.test_case import TestCase


class TestAddPageNr(TestCase):

    def test_add_page_nr(self):

        doc: Document = Document()

        # seed random
        random.seed(1024)

        # add initial Page
        p: Page = Page()
        doc.add_page(p)

        # PageLayout
        layout: PageLayout = SingleColumnLayout(p)

        # add content
        for _ in range(0, 20):
            layout.add(Paragraph(Lipsum.generate_lipsum_text(5)))

        # get the number of pages
        number_of_pages: int = int(doc.get_document_info().get_number_of_pages())

        # iterate over each Page
        for page_nr in range(0, number_of_pages):
            page: Page = doc.get_page(page_nr)

            # add page nr
            # do this in the utmost right corner
            Paragraph(f"page {page_nr+1} out of {number_of_pages}",
                      horizontal_alignment=Alignment.RIGHT,
                      vertical_alignment=Alignment.BOTTOM).paint(page, Rectangle(
                page.get_page_info().get_width() - Decimal(100),
                Decimal(0),
                Decimal(100),
                Decimal(20)
            ))

        # save
        with open(self.get_first_output_file(), "wb") as in_file_handle:
            PDF.dumps(in_file_handle, doc)

The key here is the following code:

        # get the number of pages
        number_of_pages: int = int(doc.get_document_info().get_number_of_pages())

        # iterate over each Page
        for page_nr in range(0, number_of_pages):
            page: Page = doc.get_page(page_nr)

            # add page nr
            # do this in the utmost right corner
            Paragraph(f"page {page_nr+1} out of {number_of_pages}",
                      horizontal_alignment=Alignment.RIGHT,
                      vertical_alignment=Alignment.BOTTOM).paint(page, Rectangle(
                page.get_page_info().get_width() - Decimal(100),
                Decimal(0),
                Decimal(100),
                Decimal(20)
            ))

This code gets the number of Page objects currently in the Document.

The loop below it then gets each Page in turn, and adds content to it (a Paragraph) at a fixed location (the bottom right corner).