X-Git-Url: https://plomlompom.com/repos/berlin_corona.txt?a=blobdiff_plain;f=bookmaker.py;h=3a36b962f5014c1b121c93561699d2802f40fdde;hb=7377d4ebbb6916af2acce3d3dc8e44eb58e2674d;hp=4a7ed31a6b572999e9d7d7dd6a5450d998541614;hpb=8551551339083bbba32b241e4aa53399f5051d01;p=misc diff --git a/bookmaker.py b/bookmaker.py index 4a7ed31..3a36b96 100755 --- a/bookmaker.py +++ b/bookmaker.py @@ -4,17 +4,22 @@ import argparse import io from reportlab.lib.pagesizes import A4 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 parser = argparse.ArgumentParser(description="build print-ready book PDF") -parser.add_argument("-i", "--input", dest="input_file", action="append", required=True, help="input PDF file") -parser.add_argument("-o", "--output", dest="output_file", required=True, help="output PDF file") -parser.add_argument("-p", "--pages", dest="page_range", action="append", help="page range, e.g., '3-end'") -parser.add_argument("-c", "--crop", dest="crop_range", action="append", help="crops left, bottom, right, top – e.g., '10,10,10,10'; prefix with ':'-delimited page range to limit effect") -parser.add_argument("-n", "--nup4", dest="nup4", action='store_true', help="puts 4 input pages onto 1 output page") -parser.add_argument("-a", "--analyze", dest="analyze", action="store_true", help="print lines identifying spine, page borders") -parser.add_argument("-t", "--symmetry", dest="symmetry", action="store_true", help="alternate horizontal crops between odd and even pages") +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("-r", "--rotate", dest="rotate", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)") -parser.add_argument("-m", "--margin", type=float, default=4.3, help="print margin in mm (default 4.3)") +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() @@ -39,10 +44,12 @@ 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) - for page_num in range(start_page, end_page): - page = reader.pages[page_num] + new_page_num = 0 + for old_page_num in range(start_page, end_page): + new_page_num += 1 + page = reader.pages[new_page_num] pages_to_add += [page] - print("read in %s page number %d" % (input_file, page_num+1)) + print("-i, -p: read in %s page number %d as new page %d" % (input_file, new_page_num+1, new_page_num)) # rotate page canvas if args.rotate: @@ -51,6 +58,7 @@ if args.rotate: page.add_transformation(pypdf.Transformation().translate(tx=-a4_width/2, ty=-a4_height/2)) page.add_transformation(pypdf.Transformation().rotate(-90)) page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2)) + print("-r: rotating (by 90°) page", rotate) # normalize all pages to portrait A4 for page in pages_to_add: @@ -75,7 +83,11 @@ 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) for x in crops.split(',')] + crop_left, crop_bottom, crop_right, crop_top = [float(x) * points_per_cm for x in crops.split(',')] + 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)) + 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)) cropped_width = a4_width - crop_left - crop_right cropped_height = a4_height - crop_bottom - crop_top zoom = 1 @@ -89,7 +101,10 @@ if args.crop_range: else: zoom = max(zoom_horizontal, zoom_vertical) for page_num in range(start_page, end_page): - crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top) + if args.symmetry and page_num % 2: + crops_at_page[page_num] = (crop_right, crop_bottom, crop_left, crop_top) + else: + crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top) zoom_at_page[page_num] = zoom writer = pypdf.PdfWriter() @@ -98,10 +113,7 @@ if not args.nup4: for i, page in enumerate(pages_to_add): crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i] zoom = zoom_at_page[i] - if args.symmetry and odd_page: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_right, ty=-crop_bottom)) - else: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom)) + page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom)) page.add_transformation(pypdf.Transformation().scale(zoom, zoom)) cropped_width = a4_width - crop_left - crop_right cropped_height = a4_height - crop_bottom - crop_top @@ -111,10 +123,9 @@ if not args.nup4: odd_page = not odd_page else: - points_per_mm = 2.83465 n_pages_per_axis = 2 - spine_limit = 10 - printable_margin = args.margin * points_per_mm + 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 half_height = a4_height / n_pages_per_axis @@ -168,15 +179,9 @@ else: zoom = zoom_at_page[new_i] page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top)))) if i == 0 or i == 2: - if args.symmetry: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_left)) - else: - page.add_transformation(pypdf.Transformation().translate(tx=-crop_right)) + page.add_transformation(pypdf.Transformation().translate(tx=-crop_left)) elif i == 1 or i == 3: - if args.symmetry: - page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_left)))) - else: - page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right)))) + page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right)))) page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor)) if i == 2 or i == 3: page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale)) @@ -238,9 +243,6 @@ else: c.line(x_right_spine_limit, a4_height, x_right_spine_limit, 0) if front_page: c.setLineWidth(0.2) - cut_depth = 19.5 * points_per_mm - cut_width = 10.5 * points_per_mm - middle_point_depth = 4 * points_per_mm start_up_left_left_x = x_left_spine_limit - 0.5 * cut_width start_up_left_right_x = x_left_spine_limit + 0.5 * cut_width @@ -265,8 +267,9 @@ else: writer.add_page(new_page) i = 0 front_page = not front_page + +# write and close for file in opened_files: file.close() - with open(args.output_file, 'wb') as output_file: writer.write(output_file)