cut_depth = 1.95 * points_per_cm
cut_width = 1.05 * points_per_cm
middle_point_depth = 0.4 * points_per_cm
cut_depth = 1.95 * points_per_cm
cut_width = 1.05 * points_per_cm
middle_point_depth = 0.4 * 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 = 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("-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)")
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))
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]
+
crops_at_page = [(0,0,0,0)]*len(pages_to_add)
zoom_at_page = [1]*len(pages_to_add)
if args.crop_range:
crops_at_page = [(0,0,0,0)]*len(pages_to_add)
zoom_at_page = [1]*len(pages_to_add)
if args.crop_range:
- 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
- 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))
- 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
cropped_width = a4_width - crop_left - crop_right
cropped_height = a4_height - crop_bottom - crop_top
zoom = 1
odd_page = True
for i, page in enumerate(pages_to_add):
crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
odd_page = True
for i, page in enumerate(pages_to_add):
crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
printable_margin = args.margin * points_per_cm
printable_scale = (a4_width - 2*printable_margin)/a4_width
half_width = a4_width / n_pages_per_axis
printable_margin = args.margin * points_per_cm
printable_scale = (a4_width - 2*printable_margin)/a4_width
half_width = a4_width / n_pages_per_axis
- 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]
page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
new_page.merge_page(page)
page_count += 1
page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
new_page.merge_page(page)
page_count += 1