From 5eef6eacf2a9d0518d072dd8570143e7036d82c7 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Thu, 14 Dec 2023 01:08:49 +0100
Subject: [PATCH] Allow page ranges beyond input, pad output with blank pages.

---
 README.txt   | 2 ++
 bookmaker.py | 7 ++++---
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/README.txt b/README.txt
index 57fc9cf..f27bf84 100644
--- a/README.txt
+++ b/README.txt
@@ -79,4 +79,6 @@ FURTHER NOTES:
 
 For arguments like -p, page numbers are assumed to start with 1 (not 0, which is treated as an invalid page number value).
 
+If -p defines a page range beyond the source document's last page, the remaining pages will be created blank.
+
 The target page shape so far is assumed to be A4 in portrait orientation; bookmaker.py normalizes all pages to this format before applying crops, and removes any source PDF /Rotate commands (for their production of landscape orientations).
diff --git a/bookmaker.py b/bookmaker.py
index 2440747..bc26619 100755
--- a/bookmaker.py
+++ b/bookmaker.py
@@ -211,11 +211,12 @@ def read_inputs_to_pagelist(args_input_file, args_page_range):
         if args_page_range and len(args_page_range) > i:
             range_string = args_page_range[i]
         start_page, end_page = parse_page_range(range_string, reader.pages)
-        if end_page > len(reader.pages):  # no need to test start_page cause start_page > end_page is checked above
-            raise HandledException(f"-p: page range goes beyond pages of input file: {range_string}")
         for old_page_num in range(start_page, end_page):
             new_page_num += 1
-            page = reader.pages[old_page_num]
+            if old_page_num >= len(reader.pages):
+                page = pypdf.PageObject.create_blank_page(width=A4_WIDTH, height=A4_HEIGHT)
+            else:
+                page = reader.pages[old_page_num]
             pages_to_add += [page]
             print(f"-i, -p: read in {input_file} page number {old_page_num+1} as new page {new_page_num}")
     return pages_to_add, opened_files
-- 
2.30.2