2

I am merging units of information(.PDF) into a sigle PDF. I have a lot of variables whithin a lot of filters (you can select just one of each filter) that need to be used in order to define the complete manual, look : Manual Generator in portuguese

My major problem is basically add a page number to the PDF while its merging itself, i was using the code here and here, but unsuccessfully The simplified code i am using is:

from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4

print(var1.get(), var2.get(), var3.get(), var4.get(), var5.get(), var6.get())
merger.append(Modelo) #filter one
merger.append(Escada) #filter two
output = open('ModeloMerg.pdf', 'wb')
merger.write(output)

pdf = PdfFileReader('ModeloMerg.pdf')

packet = io.BytesIO()
can = canvas.Canvas(packet, pagesize=A4)
can.drawString(10, 100, "Page" + str(15)) #just a random test number
can.save()
packet.seek(0)

watermark = PdfFileReader(packet)
watermark_page = watermark.getPage(0)



for page in range(pdf.getNumPages()):

    pdf_page = pdf.getPage(page)
    pdf_page.mergePage(watermark_page)
    pdf_writer.addPage(pdf_page)

with open('out.pdf', 'wb') as fh:
    pdf_writer.write(fh)
Dener Strossi
  • 21
  • 1
  • 3

1 Answers1

2

I know this is old, but I worked out a solution that might help someone that comes across this in the future. It's not the most efficient, but it is just an example.

I use a combination of FPDF and PyPDF2 to accomplish adding page numbers. You could probably modify my simple example to be more efficient if you are already merging documents, but I do it this way because I want the text of the page numbering to be "page [x] out of [x]".

If you don't want the total page number to be listed, you could simplify it.

import os
import PyPDF2
from fpdf import FPDF


class NumberPDF(FPDF):
    def __init__(self, numberOfPages):
        super(NumberPDF, self).__init__()
        self.numberOfPages = numberOfPages

    # Overload Header
    def header(self):
        pass

    # Overload Footer
    def footer(self):
        self.set_y(-15)
        self.set_font('Arial', 'I', 8)
        self.cell(0, 10, f"Page {self.page_no()} of {self.numberOfPages}", 0, 0, 'C')


# Grab the file you want to add pages to
inputFile = PyPDF2.PdfFileReader("original.pdf")
outputFile = "originalWithPageNumbers.pdf"

# Create a temporary numbering PDF using the overloaded FPDF class, passing the number of pages
# from your original file
tempNumFile = NumberPDF(inputFile.getNumPages())

# Add a new page to the temporary numbering PDF (the footer function runs on add_page and will 
# put the page number at the bottom, all else will be blank
for page in range(inputFile.getNumPages()):
    tempNumFile.add_page()

# Save the temporary numbering PDF
tempNumFile.output("tempNumbering.pdf")

# Create a new PDFFileReader for the temporary numbering PDF
mergeFile = PyPDF2.PdfFileReader("tempNumbering.pdf")

# Create a new PDFFileWriter for the final output document
mergeWriter = PyPDF2.PdfFileWriter()

# Loop through the pages in the temporary numbering PDF
for x, page in enumerate(mergeFile.pages):
    # Grab the corresponding page from the inputFile
    inputPage = inputFile.getPage(x)
    # Merge the inputFile page and the temporary numbering page
    inputPage.mergePage(page)
    # Add the merged page to the final output writer
    mergeWriter.addPage(inputPage)

# Delete the temporary file
os.remove("tempNumbering.pdf")

# Write the merged output
with open(outputFile, 'wb') as fh:
    mergeWriter.write(fh)
Matt Binford
  • 620
  • 8
  • 15
  • 1
    doesn;'t seem to work from `for` loop merging parts onward, tempNumbering.pdf is ok but the merged file pages become blank page without numbering. Not sure it has to do with newer syntax - e.g.: `merge_page(page)`, `mergeWriter.add_page(inputPage)` – ZenF Apr 04 '23 at 08:16