home · contact · privacy
Bookmaker: improve error handling.
[misc] / bookmaker.py
index 48d4af2c6bd4e940ba4d9e01f61dbbfd097351d2..8d0f0c4e7a0a7e467ae41f3c63c81b00d9d2e04f 100755 (executable)
@@ -1,16 +1,25 @@
 #!/usr/bin/env python3
-import pypdf
 import argparse
 import io
 import os
-from reportlab.lib.pagesizes import A4
+def fail_with_msg(msg):
+    print("ERROR:", msg)
+    exit(1)
+try:
+    import pypdf
+except ImportError:
+    fail_with_msg("Can't run without pypdf installed.")
+try:
+    from reportlab.lib.pagesizes import A4
+except ImportError:
+    fail_with_msg("Can't run without reportlab installed.")
+
 a4_width, a4_height = A4
 points_per_cm = 10 * 72 / 25.4
 cut_depth = 1.95 * points_per_cm
 cut_width = 1.05 * points_per_cm
 middle_point_depth = 0.4 * points_per_cm
 spine_limit = 1 * points_per_cm
-
 desc = """bookmaker.py is a helper for optimizing PDFs of books for the production of small self-printed, self-bound physical books  To this goal it offers various PDF manipulation options potentially that can also be used indepéndently and for other purposes.
 """
 epilogue = """
@@ -51,6 +60,8 @@ Same as -n, but draw lines marking printable-region margins, page quarts, spine
 
 NOTES:
 
+For arguments like -p, page numbers are assumed to start with 1 (not 0, which is treated as an invalid page number value).
+
 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).
 
 The --nup4 quartering puts pages into a specific order optimized for no-tumble duplex print-outs that can easily be folded and cut into pages of a small A6 book.  Each unit of 8 pages from the source PDF is mapped thus onto two subsequent pages (i.e. front and back of a printed A4 paper):
@@ -67,12 +78,11 @@ To facilitate this layout, --nup4 also pads the input PDF pages to a total numbe
 (To turn this page into a tiny 8-page book, cut the paper in two on its horizontal middle line.  Fold the two halves by their vertical middle lines, with pages 3-2 and 7-6 on the folds' insides.  This creates two 4-page books of pages 1-4 and pages 5-8.  Fold them both closed and (counter-intuitively) put the book of pages 5-8 on top of the other one (creating a temporary page order of 5,6,7,8,1,2,3,4).  A binding cut stencil should be visible on the top left of this stack – cut it out (with all pages folded together) to add the same inner-margin upper cut to each page.  Turn around your 8-pages stack to find the mirror image of aforementioned stencil on the stack's back's bottom, and cut it out too.  Each page now has binding cuts on top and bottom of its inner margins.  Swap the order of both books (back to the final page order of 1,2,3,4,5,6,7,8), and you now have an 8-pages book that can be "bound" in its binding cuts through a rubber band or the like.  Repeat with the next 8-pages double-page, et cetera.  (Actually, with just 8 pages, the paper may curl under the pressure of a rubber band – but go up to 32 pages or so, and the result will become quite stable.)
 """
 
-# parser = argparse.ArgumentParser(description="build print-ready book PDF")
 parser = argparse.ArgumentParser(description=desc, epilog=epilogue, formatter_class=argparse.RawDescriptionHelpFormatter)
 parser._optionals.title = "OPTIONS"
 parser.add_argument("-i", "--input_file", action="append", required=True, help="input PDF file")
 parser.add_argument("-o", "--output_file", required=True, help="output PDF file")
-parser.add_argument("-p", "--page_range", action="append", help="page range, e.g., '3-end'")
+parser.add_argument("-p", "--page_range", action="append", help="page range, e.g., '2-9' or '3-end' or 'start-14'")
 parser.add_argument("-c", "--crops", action="append", help="cm crops left, bottom, right, top – e.g., '10,10,10,10'; prefix with ':'-delimited page range to limit effect")
 parser.add_argument("-r", "--rotate_page", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)")
 parser.add_argument("-s", "--symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
@@ -82,21 +92,13 @@ parser.add_argument("-m", "--print_margin", type=float, default=0.43, help="prin
 args = parser.parse_args()
 
 # some basic input validation
-for filename in args.input_file:
-    if not os.path.isfile(filename):
-        raise ValueError("-i: %s is not a file" % filename)
-    try:
-        with open(filename, 'rb') as file:
-            pypdf.PdfReader(file)
-    except pypdf.errors.PdfStreamError:
-        raise ValueError("-i: cannot interpret %s as PDF file" % filename)
 def validate_page_range(p_string, err_msg_prefix):
     err_msg = "%s: invalid page range string: %s" % (err_msg_prefix, p_string)
     if '-' not in p_string:
-        raise ValueError(err_msg)
+        fail_with_msg("%s: page range string lacks '-': %s" % (err_msg_prefix, p_string))
     tokens = p_string.split("-")
     if len(tokens) > 2:
-        raise ValueError(err_msg)
+        fail_with_msg("%s: page range string has too many '-': %s" % (err_msg_prefix, p_string))
     for i, token in enumerate(tokens):
         if token == "":
             continue
@@ -107,17 +109,37 @@ def validate_page_range(p_string, err_msg_prefix):
         try:
             int(token)
         except:
-            raise ValueError(err_msg)
+            fail_with_msg("%s: page range string carries values that are neither integer, nor 'start', nor 'end': %s" % (err_msg_prefix, p_string))
+        if int(token) < 1:
+            fail_with_msg("%s: page range string may not carry page numbers <1: %s" % (err_msg_prefix, p_string))
+    start = -1
+    end = -1
+    try:
+        start = int(tokens[0])
+        end = int(tokens[1])
+    except:
+        pass
+    if start > 0 and end > 0 and start > end:
+        fail_with_msg("%s: page range starts higher than it ends: %s" % (err_msg_prefix, p_string))
+
+for filename in args.input_file:
+    if not os.path.isfile(filename):
+        fail_with_msg("-i: %s is not a file" % filename)
+    try:
+        with open(filename, 'rb') as file:
+            pypdf.PdfReader(file)
+    except pypdf.errors.PdfStreamError:
+        fail_with_msg("-i: cannot interpret %s as PDF file" % filename)
 if args.page_range:
     for p_string in args.page_range:
         validate_page_range(p_string, "-p")
     if len(args.page_range) > len(args.input_file):
-        raise ValueError("more -p arguments than -i arguments")
+        fail_with_msg("more -p arguments than -i arguments")
 if args.crops:
     for c_string in args.crops:
         initial_split = c_string.split(':')
         if len(initial_split) > 2:
-            raise ValueError("-c: cropping string has multiple ':': %s" % c_string)
+            fail_with_msg("-c: cropping string has multiple ':': %s" % c_string)
         if len(initial_split) > 1:
             validate_page_range(initial_split[0], "-c")
             crops = initial_split[1].split(",")
@@ -125,23 +147,24 @@ if args.crops:
         else:
             crops = initial_split[0].split(",")
         if len(crops) != 4:
-            raise ValueError("-c: cropping should contain three ',': %s" % c_string)
+            fail_with_msg("-c: cropping should contain three ',': %s" % c_string)
         for crop in crops:
             try:
                 float(crop)
             except:
-                raise ValueError("-c: non-number crop in %s" % c_string)
+                fail_with_msg("-c: non-number crop in %s" % c_string)
 if args.rotate_page:
-    for r in arg.rotate_page:
+    for r in args.rotate_page:
         try:
             int(r)
         except:
-            raise ValueError("-r: non-integer value: %s" % r)
+            fail_with_msg("-r: non-integer value: %s" % r)
+        if r < 1:
+            fail_with_msg("-r: value must not be <1: %s" % r)
 try:
     float(args.print_margin)
 except:
-    raise ValueError("-m: non-float value: %s" % arg.print_margin)
-
+    fail_with_msg("-m: non-float value: %s" % arg.print_margin)
 
 # select pages from input files
 def parse_page_range(range_string, pages):
@@ -154,6 +177,7 @@ def parse_page_range(range_string, pages):
         if not (len(end) == 0 or end == "end"):
             end_page = int(end)
     return start_page, end_page
+
 pages_to_add = []
 opened_files = []
 new_page_num = 0
@@ -165,20 +189,26 @@ for i, input_file in enumerate(args.input_file):
     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
+        fail_with_msg("-p: page range goes beyond pages of input file: %s" % range_string)
     for old_page_num in range(start_page, end_page):
         new_page_num += 1
         page = reader.pages[old_page_num]
         pages_to_add += [page]
         print("-i, -p: read in %s page number %d as new page %d" % (input_file, old_page_num+1, new_page_num))
 
-# if necessary, pad pages to multiple of 8
-if args.nup4:
-    mod_to_8 = len(pages_to_add) % 8
-    if mod_to_8 > 0:
-        print("-n: number of input pages %d not multiple of 8, padding to that" % len(pages_to_add))
-        for _ in range(8 - mod_to_8):
-            new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
-            pages_to_add += [new_page]
+# we can do some more input validations now that we know how many pages output should have
+if args.crops:
+    for c_string in args.crops:
+        initial_split = c_string.split(':')
+        if len(initial_split) > 1:
+            start, end = parse_page_range(initial_split[0], pages_to_add)
+            if end > len(pages_to_add):
+                 fail_with_msg("-c: page range goes beyond number of pages we're building: %s" % initial_split[0])
+if args.rotate_page:
+    for r in args.rotate_page:
+        if r > len(pages_to_add):
+             fail_with_msg("-r: page number beyond number of pages we're building: %d" % r)
 
 # rotate page canvas
 if args.rotate_page:
@@ -189,6 +219,15 @@ if args.rotate_page:
         page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2))
         print("-r: rotating (by 90°) page", rotate_page)
 
+# if necessary, pad pages to multiple of 8
+if args.nup4:
+    mod_to_8 = len(pages_to_add) % 8
+    if mod_to_8 > 0:
+        print("-n: number of input pages %d not multiple of 8, padding to that" % len(pages_to_add))
+        for _ in range(8 - mod_to_8):
+            new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
+            pages_to_add += [new_page]
+
 # normalize all pages to portrait A4
 for page in pages_to_add:
     if "/Rotate" in page:
@@ -227,7 +266,7 @@ if args.crops:
         zoom_horizontal = a4_width / (a4_width - crop_left - crop_right)
         zoom_vertical = a4_height / (a4_height - crop_bottom - crop_top)
         if (zoom_horizontal > 1 and zoom_vertical < 1) or (zoom_horizontal < 1 and zoom_vertical > 1):
-            raise ValueError("crops would create opposing zoom directions")
+            fail_with_msg("crops would create opposing zoom directions")
         elif zoom_horizontal + zoom_vertical > 2:
             zoom = min(zoom_horizontal, zoom_vertical)
         else: