- if i == 0:
- eight_pack = []
- eight_pack += [page]
- i += 1
- if i == 8:
- i = 0
- new_i_order += [8 * n_eights + 3,
- 8 * n_eights + 0,
- 8 * n_eights + 7,
- 8 * n_eights + 4,
- 8 * n_eights + 1,
- 8 * n_eights + 2,
- 8 * n_eights + 5,
- 8 * n_eights + 6]
- n_eights += 1
- new_page_order += [eight_pack[3]] # page front, upper left
- new_page_order += [eight_pack[0]] # page front, upper right
- new_page_order += [eight_pack[7]] # page front, lower left
- new_page_order += [eight_pack[4]] # page front, lower right
- new_page_order += [eight_pack[1]] # page back, upper left
- new_page_order += [eight_pack[2]] # page back, upper right
- new_page_order += [eight_pack[5]] # page back, lower left
- new_page_order += [eight_pack[6]] # page back, lower right
- i = 0
- page_count = 0
- front_page = True
- for j, page in enumerate(new_page_order):
- if i == 0:
- new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
-
- # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
- new_i = new_i_order[j]
- crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i]
- 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:
- page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
- elif i == 1 or i == 3:
- 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))
-
- # outer section transformations
- page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*a4_height))
- if i == 0 or i == 1:
- y_section = a4_height
- page.mediabox.bottom = half_height
- page.mediabox.top = a4_height
- if i == 2 or i == 3:
- y_section = 0
- page.mediabox.bottom = 0
- page.mediabox.top = half_height
- if i == 0 or i == 2:
- x_section = 0
- page.mediabox.left = 0
- page.mediabox.right = half_width
- if i == 1 or i == 3:
- page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*a4_width))
- x_section = a4_width
- page.mediabox.left = half_width
- page.mediabox.right = a4_width
- page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
- page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
- new_page.merge_page(page)
- page_count += 1
- print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
- i += 1
- if i > 3:
- from reportlab.pdfgen import canvas
- if args.analyze:
- # borders
- packet = io.BytesIO()
- c = canvas.Canvas(packet, pagesize=A4)
- c.setLineWidth(0.1)
- c.line(0, a4_height, a4_width, a4_height)
- c.line(0, half_height, a4_width, half_height)
- c.line(0, 0, a4_width, 0)
- c.line(0, a4_height, 0, 0)
- c.line(half_width, a4_height, half_width, 0)
- c.line(a4_width, a4_height, a4_width, 0)
- c.save()
- new_pdf = pypdf.PdfReader(packet)
- new_page.merge_page(new_pdf.pages[0])
- printable_offset_x = printable_margin
- printable_offset_y = printable_margin * a4_height / a4_width
- new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
- new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
- x_left_spine_limit = half_width * bonus_shrink_factor
- x_right_spine_limit = a4_width - x_left_spine_limit
- if args.analyze or front_page:
- packet = io.BytesIO()
- c = canvas.Canvas(packet, pagesize=A4)
- if args.analyze:
- # # spine lines
- c.setLineWidth(0.1)
- c.line(x_left_spine_limit, a4_height, x_left_spine_limit, 0)
- c.line(x_right_spine_limit, a4_height, x_right_spine_limit, 0)
- if front_page:
- c.setLineWidth(0.2)
-
- 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
- middle_point_up_left_y = half_height + middle_point_depth
- end_point_up_left_y = half_height + cut_depth
- c.line(start_up_left_right_x, half_height, x_left_spine_limit, end_point_up_left_y)
- c.line(x_left_spine_limit, end_point_up_left_y, x_left_spine_limit, middle_point_up_left_y)
- c.line(x_left_spine_limit, middle_point_up_left_y, start_up_left_left_x, half_height)
-
- start_down_right_left_x = x_right_spine_limit - 0.5 * cut_width
- start_down_right_right_x = x_right_spine_limit + 0.5 * cut_width
- middle_point_down_right_y = half_height - middle_point_depth
- end_point_down_right_y = half_height - cut_depth
- c.line(start_down_right_left_x, half_height, x_right_spine_limit, end_point_down_right_y)
- c.line(x_right_spine_limit, end_point_down_right_y, x_right_spine_limit, middle_point_down_right_y)
- c.line(x_right_spine_limit, middle_point_down_right_y, start_down_right_right_x, half_height)
-
- if args.analyze or front_page:
- c.save()
- new_pdf = pypdf.PdfReader(packet)
- new_page.merge_page(new_pdf.pages[0])
- 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)
+ if "/Rotate" in page:
+ page.rotate(360 - page["/Rotate"])
+ page.mediabox.left = 0
+ page.mediabox.bottom = 0
+ page.mediabox.top = A4_HEIGHT
+ page.mediabox.right = A4_WIDTH
+ page.cropbox = page.mediabox
+
+ # 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.crops:
+ for c_string in args.crops:
+ page_range, crops = split_crops_string(c_string)
+ start_page, end_page = parse_page_range(page_range, pages_to_add)
+ 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 %.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 %.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
+ 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")
+ elif zoom_horizontal + zoom_vertical > 2:
+ zoom = min(zoom_horizontal, zoom_vertical)
+ else:
+ zoom = max(zoom_horizontal, zoom_vertical)
+ for page_num in range(start_page, end_page):
+ 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()
+ 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]
+ zoom = zoom_at_page[i]
+ 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
+ page.mediabox.right = cropped_width * zoom
+ 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.print_margin)
+ if args.analyze:
+ print("-a: drawing page borders, spine limits")
+ n_pages_per_axis = 2
+ printable_margin = args.print_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
+ section_scale_factor = 1 / n_pages_per_axis
+ spine_part_of_page = (SPINE_LIMIT / half_width) / printable_scale
+ bonus_shrink_factor = 1 - spine_part_of_page
+ new_page_order = []
+ new_i_order = []
+ eight_pack = []
+ i = 0
+ n_eights = 0
+ for page in pages_to_add:
+ if i == 0:
+ eight_pack = []
+ eight_pack += [page]
+ i += 1
+ if i == 8:
+ i = 0
+ new_i_order += [8 * n_eights + 3,
+ 8 * n_eights + 0,
+ 8 * n_eights + 7,
+ 8 * n_eights + 4,
+ 8 * n_eights + 1,
+ 8 * n_eights + 2,
+ 8 * n_eights + 5,
+ 8 * n_eights + 6]
+ n_eights += 1
+ new_page_order += [eight_pack[3]] # page front, upper left
+ new_page_order += [eight_pack[0]] # page front, upper right
+ new_page_order += [eight_pack[7]] # page front, lower left
+ new_page_order += [eight_pack[4]] # page front, lower right
+ new_page_order += [eight_pack[1]] # page back, upper left
+ new_page_order += [eight_pack[2]] # page back, upper right
+ new_page_order += [eight_pack[5]] # page back, lower left
+ new_page_order += [eight_pack[6]] # page back, lower right
+ i = 0
+ page_count = 0
+ front_page = True
+ for j, page in enumerate(new_page_order):
+ if i == 0:
+ new_page = pypdf.PageObject.create_blank_page(width=A4_WIDTH, height=A4_HEIGHT)
+
+ # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
+ new_i = new_i_order[j]
+ crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i]
+ 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:
+ page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
+ elif i == 1 or i == 3:
+ 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))
+
+ # outer section transformations
+ page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*A4_HEIGHT))
+ if i == 0 or i == 1:
+ y_section = A4_HEIGHT
+ page.mediabox.bottom = half_height
+ page.mediabox.top = A4_HEIGHT
+ if i == 2 or i == 3:
+ y_section = 0
+ page.mediabox.bottom = 0
+ page.mediabox.top = half_height
+ if i == 0 or i == 2:
+ x_section = 0
+ page.mediabox.left = 0
+ page.mediabox.right = half_width
+ if i == 1 or i == 3:
+ page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*A4_WIDTH))
+ x_section = A4_WIDTH
+ page.mediabox.left = half_width
+ page.mediabox.right = A4_WIDTH
+ page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
+ page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
+ new_page.merge_page(page)
+ page_count += 1
+ print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
+ i += 1
+ if i > 3:
+ from reportlab.pdfgen import canvas
+ if args.analyze:
+ # borders
+ packet = io.BytesIO()
+ c = canvas.Canvas(packet, pagesize=A4)
+ c.setLineWidth(0.1)
+ c.line(0, A4_HEIGHT, A4_WIDTH, A4_HEIGHT)
+ c.line(0, half_height, A4_WIDTH, half_height)
+ c.line(0, 0, A4_WIDTH, 0)
+ c.line(0, A4_HEIGHT, 0, 0)
+ c.line(half_width, A4_HEIGHT, half_width, 0)
+ c.line(A4_WIDTH, A4_HEIGHT, A4_WIDTH, 0)
+ c.save()
+ new_pdf = pypdf.PdfReader(packet)
+ new_page.merge_page(new_pdf.pages[0])
+ printable_offset_x = printable_margin
+ printable_offset_y = printable_margin * A4_HEIGHT / A4_WIDTH
+ new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
+ new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
+ x_left_SPINE_LIMIT = half_width * bonus_shrink_factor
+ x_right_SPINE_LIMIT = A4_WIDTH - x_left_SPINE_LIMIT
+ if args.analyze or front_page:
+ packet = io.BytesIO()
+ c = canvas.Canvas(packet, pagesize=A4)
+ if args.analyze:
+ # # spine lines
+ c.setLineWidth(0.1)
+ c.line(x_left_SPINE_LIMIT, A4_HEIGHT, x_left_SPINE_LIMIT, 0)
+ c.line(x_right_SPINE_LIMIT, A4_HEIGHT, x_right_SPINE_LIMIT, 0)
+ if front_page:
+ c.setLineWidth(0.2)
+ # cut upper left
+ 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
+ middle_point_up_left_y = half_height + MIDDLE_POINT_DEPTH
+ end_point_up_left_y = half_height + CUT_DEPTH
+ c.line(start_up_left_right_x, half_height, x_left_SPINE_LIMIT, end_point_up_left_y)
+ c.line(x_left_SPINE_LIMIT, end_point_up_left_y, x_left_SPINE_LIMIT, middle_point_up_left_y)
+ c.line(x_left_SPINE_LIMIT, middle_point_up_left_y, start_up_left_left_x, half_height)
+ # cut lower right
+ start_down_right_left_x = x_right_SPINE_LIMIT - 0.5 * CUT_WIDTH
+ start_down_right_right_x = x_right_SPINE_LIMIT + 0.5 * CUT_WIDTH
+ middle_point_down_right_y = half_height - MIDDLE_POINT_DEPTH
+ end_point_down_right_y = half_height - CUT_DEPTH
+ c.line(start_down_right_left_x, half_height, x_right_SPINE_LIMIT, end_point_down_right_y)
+ c.line(x_right_SPINE_LIMIT, end_point_down_right_y, x_right_SPINE_LIMIT, middle_point_down_right_y)
+ c.line(x_right_SPINE_LIMIT, middle_point_down_right_y, start_down_right_right_x, half_height)
+ if args.analyze or front_page:
+ c.save()
+ new_pdf = pypdf.PdfReader(packet)
+ new_page.merge_page(new_pdf.pages[0])
+ 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)
+
+
+if __name__ == "__main__":
+ try:
+ main()
+ except ValueError as e:
+ fail_with_msg(e)