5 from reportlab.lib.pagesizes import A4
6 a4_width, a4_height = A4
7 points_per_cm = 10 * 72 / 25.4
8 cut_depth = 1.95 * points_per_cm
9 cut_width = 1.05 * points_per_cm
10 middle_point_depth = 0.4 * points_per_cm
11 spine_limit = 1 * points_per_cm
13 parser = argparse.ArgumentParser(description="build print-ready book PDF")
14 parser.add_argument("-i", "--input_file", action="append", required=True, help="input PDF file")
15 parser.add_argument("-o", "--output_file", required=True, help="output PDF file")
16 parser.add_argument("-p", "--page_range", action="append", help="page range, e.g., '3-end'")
17 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")
18 parser.add_argument("-s", "--symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
19 parser.add_argument("-r", "--rotate", dest="rotate", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)")
20 parser.add_argument("-n", "--nup4", action='store_true', help="puts 4 input pages onto 1 output page")
21 parser.add_argument("-a", "--analyze", action="store_true", help="in --nup4, print lines identifying spine, page borders")
22 parser.add_argument("-m", "--margin", type=float, default=0.43, help="print margin for --nup4 in cm (default 0.43)")
23 args = parser.parse_args()
26 # select pages from input files
27 def parse_page_range(range_string, pages):
31 start, end = range_string.split('-')
32 if not (len(start) == 0 or start == "start"):
33 start_page = int(start) - 1
34 if not (len(end) == 0 or end == "end"):
36 return start_page, end_page
40 for i, input_file in enumerate(args.input_file):
41 file = open(input_file, 'rb')
42 opened_files += [file]
43 reader = pypdf.PdfReader(file)
45 if args.page_range and len(args.page_range) > i:
46 range_string = args.page_range[i]
47 start_page, end_page = parse_page_range(range_string, reader.pages)
48 for old_page_num in range(start_page, end_page):
50 page = reader.pages[old_page_num]
51 pages_to_add += [page]
52 print("-i, -p: read in %s page number %d as new page %d" % (input_file, old_page_num+1, new_page_num))
56 for rotate in args.rotate:
57 page = pages_to_add[rotate - 1]
58 page.add_transformation(pypdf.Transformation().translate(tx=-a4_width/2, ty=-a4_height/2))
59 page.add_transformation(pypdf.Transformation().rotate(-90))
60 page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2))
61 print("-r: rotating (by 90°) page", rotate)
63 # normalize all pages to portrait A4
64 for page in pages_to_add:
66 page.rotate(360 - page["/Rotate"])
67 page.mediabox.left = 0
68 page.mediabox.bottom = 0
69 page.mediabox.top = a4_height
70 page.mediabox.right = a4_width
71 page.cropbox = page.mediabox
73 # determine page crops, zooms, crop symmetry
74 crops_at_page = [(0,0,0,0)]*len(pages_to_add)
75 zoom_at_page = [1]*len(pages_to_add)
77 for crop_range in args.crop_range:
78 initial_split = crop_range.split(':')
79 if len(initial_split) > 1:
80 page_range = initial_split[0]
81 crops = initial_split[1]
84 crops = initial_split[0]
85 start_page, end_page = parse_page_range(page_range, pages_to_add)
86 crop_left, crop_bottom, crop_right, crop_top = [float(x) * points_per_cm for x in crops.split(',')]
88 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))
90 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))
91 cropped_width = a4_width - crop_left - crop_right
92 cropped_height = a4_height - crop_bottom - crop_top
94 zoom_horizontal = a4_width / (a4_width - crop_left - crop_right)
95 zoom_vertical = a4_height / (a4_height - crop_bottom - crop_top)
96 if (zoom_horizontal > 1 and zoom_vertical < 1) or (zoom_horizontal < 1 and zoom_vertical > 1):
97 print("Error: opposing zooms.")
99 elif zoom_horizontal + zoom_vertical > 2:
100 zoom = min(zoom_horizontal, zoom_vertical)
102 zoom = max(zoom_horizontal, zoom_vertical)
103 for page_num in range(start_page, end_page):
104 if args.symmetry and page_num % 2:
105 crops_at_page[page_num] = (crop_right, crop_bottom, crop_left, crop_top)
107 crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top)
108 zoom_at_page[page_num] = zoom
110 writer = pypdf.PdfWriter()
113 print("building 1-input-page-per-output-page book")
115 for i, page in enumerate(pages_to_add):
116 crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
117 zoom = zoom_at_page[i]
118 page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
119 page.add_transformation(pypdf.Transformation().scale(zoom, zoom))
120 cropped_width = a4_width - crop_left - crop_right
121 cropped_height = a4_height - crop_bottom - crop_top
122 page.mediabox.right = cropped_width * zoom
123 page.mediabox.top = cropped_height * zoom
124 writer.add_page(page)
125 odd_page = not odd_page
128 print("-n: building 4-input-pages-per-output-page book")
130 printable_margin = args.margin * points_per_cm
131 printable_scale = (a4_width - 2*printable_margin)/a4_width
132 half_width = a4_width / n_pages_per_axis
133 half_height = a4_height / n_pages_per_axis
134 section_scale_factor = 1 / n_pages_per_axis
135 spine_part_of_page = (spine_limit / half_width) / printable_scale
136 bonus_shrink_factor = 1 - spine_part_of_page
140 mod_to_8 = len(pages_to_add) % 8
142 for _ in range(8 - mod_to_8):
143 new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
144 pages_to_add += [new_page]
147 for page in pages_to_add:
154 new_i_order += [8 * n_eights + 3,
163 new_page_order += [eight_pack[3]] # page front, upper left
164 new_page_order += [eight_pack[0]] # page front, upper right
165 new_page_order += [eight_pack[7]] # page front, lower left
166 new_page_order += [eight_pack[4]] # page front, lower right
167 new_page_order += [eight_pack[1]] # page back, upper left
168 new_page_order += [eight_pack[2]] # page back, upper right
169 new_page_order += [eight_pack[5]] # page back, lower left
170 new_page_order += [eight_pack[6]] # page back, lower right
174 for j, page in enumerate(new_page_order):
176 new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
178 # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
179 new_i = new_i_order[j]
180 crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i]
181 zoom = zoom_at_page[new_i]
182 page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top))))
184 page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
185 elif i == 1 or i == 3:
186 page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right))))
187 page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor))
189 page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale))
191 # outer section transformations
192 page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*a4_height))
194 y_section = a4_height
195 page.mediabox.bottom = half_height
196 page.mediabox.top = a4_height
199 page.mediabox.bottom = 0
200 page.mediabox.top = half_height
203 page.mediabox.left = 0
204 page.mediabox.right = half_width
206 page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*a4_width))
208 page.mediabox.left = half_width
209 page.mediabox.right = a4_width
210 page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
211 page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
212 new_page.merge_page(page)
214 print("merged page number", page_count)
217 from reportlab.pdfgen import canvas
220 packet = io.BytesIO()
221 c = canvas.Canvas(packet, pagesize=A4)
223 c.line(0, a4_height, a4_width, a4_height)
224 c.line(0, half_height, a4_width, half_height)
225 c.line(0, 0, a4_width, 0)
226 c.line(0, a4_height, 0, 0)
227 c.line(half_width, a4_height, half_width, 0)
228 c.line(a4_width, a4_height, a4_width, 0)
230 new_pdf = pypdf.PdfReader(packet)
231 new_page.merge_page(new_pdf.pages[0])
232 printable_offset_x = printable_margin
233 printable_offset_y = printable_margin * a4_height / a4_width
234 new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
235 new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
236 x_left_spine_limit = half_width * bonus_shrink_factor
237 x_right_spine_limit = a4_width - x_left_spine_limit
238 if args.analyze or front_page:
239 packet = io.BytesIO()
240 c = canvas.Canvas(packet, pagesize=A4)
244 c.line(x_left_spine_limit, a4_height, x_left_spine_limit, 0)
245 c.line(x_right_spine_limit, a4_height, x_right_spine_limit, 0)
249 start_up_left_left_x = x_left_spine_limit - 0.5 * cut_width
250 start_up_left_right_x = x_left_spine_limit + 0.5 * cut_width
251 middle_point_up_left_y = half_height + middle_point_depth
252 end_point_up_left_y = half_height + cut_depth
253 c.line(start_up_left_right_x, half_height, x_left_spine_limit, end_point_up_left_y)
254 c.line(x_left_spine_limit, end_point_up_left_y, x_left_spine_limit, middle_point_up_left_y)
255 c.line(x_left_spine_limit, middle_point_up_left_y, start_up_left_left_x, half_height)
257 start_down_right_left_x = x_right_spine_limit - 0.5 * cut_width
258 start_down_right_right_x = x_right_spine_limit + 0.5 * cut_width
259 middle_point_down_right_y = half_height - middle_point_depth
260 end_point_down_right_y = half_height - cut_depth
261 c.line(start_down_right_left_x, half_height, x_right_spine_limit, end_point_down_right_y)
262 c.line(x_right_spine_limit, end_point_down_right_y, x_right_spine_limit, middle_point_down_right_y)
263 c.line(x_right_spine_limit, middle_point_down_right_y, start_down_right_right_x, half_height)
265 if args.analyze or front_page:
267 new_pdf = pypdf.PdfReader(packet)
268 new_page.merge_page(new_pdf.pages[0])
269 writer.add_page(new_page)
271 front_page = not front_page
274 for file in opened_files:
276 with open(args.output_file, 'wb') as output_file:
277 writer.write(output_file)