Compdigitec Labs

« | Home | »

Concatenating PDFs while padding/extending odd-length PDFs

By admin | October 10, 2020

When concatenating PDFs to consolidate print jobs, it is nice to have single-page or PDFs with odd numbers of pages to not have to share sides with another unrelated document.

There is a short script available at https://unix.stackexchange.com/questions/66418/how-can-i-merge-pdf-files-so-that-each-file-begins-on-an-odd-page-number ; however, it does not appear to be updated for PyPDF2. So here is an update for Python3 and PyPDF2.

Prep:

sudo apt install python3-pypdf2

#!/usr/bin/env python3
# Inspired from https://unix.stackexchange.com/a/66455

import copy
import sys
from PyPDF2 import PdfFileWriter, PdfFileReader

def main():
    if len(sys.argv) < 2:
        print("No input PDFs specified", file=sys.stderr)
        return 1

    pdf_writer = PdfFileWriter()
    output_page_number = 0
    alignment = 2           # to align on even pages

    # So we can close the file objects later.
    # https://stackoverflow.com/questions/44375872/pypdf2-returning-blank-pdf-after-copy
    file_objects = []

    for filename in sys.argv[1:]:
        # Store the file object for closing
        f = open(filename, 'rb')
        file_objects.append(f)

        # Open the input PDF
        pdf_reader = PdfFileReader(f)

        # Add input pages
        for page in pdf_reader.pages:
            pdf_writer.addPage(page)
            output_page_number += 1

        # Add filler pages
        while output_page_number % alignment != 0:
            pdf_writer.addBlankPage()
            output_page_number += 1

    # Write output PDF while input files are still open.
    pdf_writer.write(sys.stdout.buffer)

    # Close open files.
    while len(file_objects) > 0:
        file_objects.pop().close()

    return 0

sys.exit(main())

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux | 8 Comments »

8 Responses to “Concatenating PDFs while padding/extending odd-length PDFs”

  1. slot999 Says:
    March 2nd, 2022 at 20:04

    … [Trackback]

    […] Here you will find 94686 more Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  2. nova88 Says:
    March 3rd, 2022 at 06:16

    … [Trackback]

    […] There you will find 38671 more Information to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  3. localcornholeboards Says:
    March 4th, 2022 at 10:14

    … [Trackback]

    […] Here you will find 84405 additional Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  4. Firearms For Sale Says:
    March 13th, 2022 at 10:15

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  5. this page Says:
    April 15th, 2022 at 04:09

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  6. สล็อตวอเลท ไม่มีขั้นต่ำ Says:
    May 2nd, 2022 at 18:46

    … [Trackback]

    […] Find More to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  7. คาสิโนออนไลน์ Says:
    June 1st, 2022 at 07:33

    … [Trackback]

    […] There you can find 21160 more Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  8. magic mushroom 2022 Says:
    June 7th, 2022 at 04:36

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

Comments