home · contact · privacy
Bookmaker: more expressive output.
[misc] / bookmaker.py
index 3a36b962f5014c1b121c93561699d2802f40fdde..d9bf685c0967a492d941b15e25bcd9af6a720751 100755 (executable)
@@ -8,18 +8,18 @@ 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
 
 parser = argparse.ArgumentParser(description="build print-ready book PDF")
 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("-c", "--crop_range", 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("-t", "--symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
+parser.add_argument("-s", "--symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
 parser.add_argument("-r", "--rotate", dest="rotate", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)")
 parser.add_argument("-n", "--nup4", action='store_true', help="puts 4 input pages onto 1 output page")
 parser.add_argument("-a", "--analyze", action="store_true", help="in --nup4, print lines identifying spine, page borders")
 parser.add_argument("-m", "--margin", type=float, default=0.43, help="print margin for --nup4 in cm (default 0.43)")
-parser.add_argument("-s", "--spine", type=float, default=1, help="on --nup4, cm width of margin hidden in spine (default 1)")
 args = parser.parse_args()
 
 
@@ -36,6 +36,7 @@ def parse_page_range(range_string, pages):
     return start_page, end_page
 pages_to_add = []
 opened_files = []
+new_page_num = 0
 for i, input_file in enumerate(args.input_file):
     file = open(input_file, 'rb')
     opened_files += [file]
@@ -44,12 +45,20 @@ 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)
-    new_page_num = 0
     for old_page_num in range(start_page, end_page):
         new_page_num += 1
-        page = reader.pages[new_page_num]
+        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, new_page_num+1, new_page_num))
+        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]
 
 # rotate page canvas
 if args.rotate:
@@ -70,7 +79,7 @@ for page in pages_to_add:
     page.mediabox.right = a4_width
     page.cropbox = page.mediabox
 
-# determine page crops, zooms
+# determine page crops, zooms, crop symmetry
 crops_at_page = [(0,0,0,0)]*len(pages_to_add)
 zoom_at_page = [1]*len(pages_to_add)
 if args.crop_range:
@@ -83,11 +92,15 @@ if args.crop_range:
           page_range = None
           crops = initial_split[0]
       start_page, end_page = parse_page_range(page_range, pages_to_add)
-      crop_left, crop_bottom, crop_right, crop_top = [float(x) * points_per_cm for x in  crops.split(',')]
+      crop_left_cm, crop_bottom_cm, crop_right_cm, crop_top_cm = [float(x) for x in  crops.split(',')]
+      crop_left = crop_left_cm * points_per_cm
+      crop_bottom = crop_bottom_cm * points_per_cm
+      crop_right = crop_right_cm * points_per_cm
+      crop_top = crop_top_cm * points_per_cm
       if args.symmetry:
-          print("-c, -t: to pages %d to %d applying crops: left %dcm, bottom %dcm, right %dcm, top %dcm (but alternating left and right crop between even and odd pages)" % (start_page + 1, end_page, crop_left, crop_bottom, crop_right, crop_top))
+          print("-c, -t: to pages %d to %d applying crops: left %.2fcm, bottom %.2fcm, right %.2fcm, top %.2fcm (but alternating left and right crop between even and odd pages)" % (start_page + 1, end_page, crop_left_cm, crop_bottom_cm, crop_right_cm, crop_top_cm))
       else:
-          print("-c: to pages %d to %d applying crops: left %dcm, bottom %dcm, right %dcm, top %dcm" % (start_page + 1, end_page, crop_left, crop_bottom, crop_right, crop_top))
+          print("-c: to pages %d to %d applying crops: left %.2fcm, bottom %.2fcm, right %.2fcm, top %.2fcm" % (start_page + 1, end_page, crop_left_cm, crop_bottom_cm, crop_right_cm, crop_top_cm))
       cropped_width  = a4_width - crop_left - crop_right
       cropped_height = a4_height - crop_bottom - crop_top
       zoom = 1
@@ -109,6 +122,8 @@ if args.crop_range:
 
 writer = pypdf.PdfWriter()
 if not args.nup4:
+    # single-page output
+    print("building 1-input-page-per-output-page book")
     odd_page = True
     for i, page in enumerate(pages_to_add):
         crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
@@ -121,10 +136,14 @@ if not args.nup4:
         page.mediabox.top = cropped_height * zoom
         writer.add_page(page)
         odd_page = not odd_page
+        print("built page number %d (of %d)" % (i+1, len(pages_to_add)))
 
 else:
+    print("-n: building 4-input-pages-per-output-page book")
+    print("-m: applying printable-area margin of %.2fcm" % args.margin)
+    if args.analyze:
+        print("-a: drawing page borders, spine limits")
     n_pages_per_axis = 2
-    spine_limit = args.spine * points_per_cm
     printable_margin = args.margin * points_per_cm
     printable_scale = (a4_width - 2*printable_margin)/a4_width
     half_width = a4_width / n_pages_per_axis
@@ -135,11 +154,6 @@ else:
     new_page_order = []
     new_i_order = []
     eight_pack = []
-    mod_to_8 = len(pages_to_add) % 8
-    if mod_to_8 > 0:
-        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]
     i = 0
     n_eights = 0
     for page in pages_to_add:
@@ -209,7 +223,7 @@ else:
         page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
         new_page.merge_page(page)
         page_count += 1
-        print("merged page number", page_count)
+        print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
         i += 1
         if i > 3:
             from reportlab.pdfgen import canvas