5 from reportlab.lib.pagesizes import A4
6 a4_width, a4_height = A4
8 parser = argparse.ArgumentParser(description="build print-ready book PDF")
9 parser.add_argument("-i", "--input", dest="input_file", action="append", required=True, help="input PDF file")
10 parser.add_argument("-o", "--output", dest="output_file", required=True, help="output PDF file")
11 parser.add_argument("-p", "--pages", dest="page_range", action="append", help="page range, e.g., '3-end'")
12 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")
13 parser.add_argument("-n", "--nup4", dest="nup4", action='store_true', help="puts 4 input pages onto 1 output page")
14 parser.add_argument("-a", "--analyze", dest="analyze", action="store_true", help="print lines identifying spine, page borders")
15 parser.add_argument("-t", "--symmetry", dest="symmetry", action="store_true", help="alternate horizontal crops between odd and even pages")
16 parser.add_argument("-r", "--rotate", dest="rotate", type=int, action="append", help="rotate page of number by 90° (usable multiple times on same page!)")
17 parser.add_argument("-m", "--margin", type=float, default=4.3, help="print margin in mm (default 4.3)")
18 args = parser.parse_args()
21 # select pages from input files
22 def parse_page_range(range_string, pages):
26 start, end = range_string.split('-')
27 if not (len(start) == 0 or start == "start"):
28 start_page = int(start) - 1
29 if not (len(end) == 0 or end == "end"):
31 return start_page, end_page
34 for i, input_file in enumerate(args.input_file):
35 file = open(input_file, 'rb')
36 opened_files += [file]
37 reader = pypdf.PdfReader(file)
39 if args.page_range and len(args.page_range) > i:
40 range_string = args.page_range[i]
41 start_page, end_page = parse_page_range(range_string, reader.pages)
42 for page_num in range(start_page, end_page):
43 page = reader.pages[page_num]
44 pages_to_add += [page]
45 print("read in %s page number %d" % (input_file, page_num+1))
49 for rotate in args.rotate:
50 page = pages_to_add[rotate - 1]
51 page.add_transformation(pypdf.Transformation().translate(tx=-a4_width/2, ty=-a4_height/2))
52 page.add_transformation(pypdf.Transformation().rotate(-90))
53 page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2))
55 # normalize all pages to portrait A4
56 for page in pages_to_add:
58 page.rotate(360 - page["/Rotate"])
59 page.mediabox.left = 0
60 page.mediabox.bottom = 0
61 page.mediabox.top = a4_height
62 page.mediabox.right = a4_width
63 page.cropbox = page.mediabox
65 # determine page crops, zooms
66 crops_at_page = [(0,0,0,0)]*len(pages_to_add)
67 zoom_at_page = [1]*len(pages_to_add)
69 for crop_range in args.crop_range:
70 initial_split = crop_range.split(':')
71 if len(initial_split) > 1:
72 page_range = initial_split[0]
73 crops = initial_split[1]
76 crops = initial_split[0]
77 start_page, end_page = parse_page_range(page_range, pages_to_add)
78 crop_left, crop_bottom, crop_right, crop_top = [float(x) for x in crops.split(',')]
79 cropped_width = a4_width - crop_left - crop_right
80 cropped_height = a4_height - crop_bottom - crop_top
82 zoom_horizontal = a4_width / (a4_width - crop_left - crop_right)
83 zoom_vertical = a4_height / (a4_height - crop_bottom - crop_top)
84 if (zoom_horizontal > 1 and zoom_vertical < 1) or (zoom_horizontal < 1 and zoom_vertical > 1):
85 print("Error: opposing zooms.")
87 elif zoom_horizontal + zoom_vertical > 2:
88 zoom = min(zoom_horizontal, zoom_vertical)
90 zoom = max(zoom_horizontal, zoom_vertical)
91 for page_num in range(start_page, end_page):
92 crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top)
93 zoom_at_page[page_num] = zoom
95 writer = pypdf.PdfWriter()
98 for i, page in enumerate(pages_to_add):
99 crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
100 zoom = zoom_at_page[i]
101 if args.symmetry and odd_page:
102 page.add_transformation(pypdf.Transformation().translate(tx=-crop_right, ty=-crop_bottom))
104 page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
105 page.add_transformation(pypdf.Transformation().scale(zoom, zoom))
106 cropped_width = a4_width - crop_left - crop_right
107 cropped_height = a4_height - crop_bottom - crop_top
108 page.mediabox.right = cropped_width * zoom
109 page.mediabox.top = cropped_height * zoom
110 writer.add_page(page)
111 odd_page = not odd_page
114 points_per_mm = 2.83465
117 printable_margin = args.margin * points_per_mm
118 printable_scale = (a4_width - 2*printable_margin)/a4_width
119 half_width = a4_width / n_pages_per_axis
120 half_height = a4_height / n_pages_per_axis
121 section_scale_factor = 1 / n_pages_per_axis
122 spine_part_of_page = (spine_limit / half_width) / printable_scale
123 bonus_shrink_factor = 1 - spine_part_of_page
127 mod_to_8 = len(pages_to_add) % 8
129 for _ in range(8 - mod_to_8):
130 new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
131 pages_to_add += [new_page]
134 for page in pages_to_add:
141 new_i_order += [8 * n_eights + 3,
150 new_page_order += [eight_pack[3]] # page front, upper left
151 new_page_order += [eight_pack[0]] # page front, upper right
152 new_page_order += [eight_pack[7]] # page front, lower left
153 new_page_order += [eight_pack[4]] # page front, lower right
154 new_page_order += [eight_pack[1]] # page back, upper left
155 new_page_order += [eight_pack[2]] # page back, upper right
156 new_page_order += [eight_pack[5]] # page back, lower left
157 new_page_order += [eight_pack[6]] # page back, lower right
161 for j, page in enumerate(new_page_order):
163 new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
165 # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
166 new_i = new_i_order[j]
167 crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i]
168 zoom = zoom_at_page[new_i]
169 page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top))))
172 page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
174 page.add_transformation(pypdf.Transformation().translate(tx=-crop_right))
175 elif i == 1 or i == 3:
177 page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_left))))
179 page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right))))
180 page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor))
182 page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale))
184 # outer section transformations
185 page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*a4_height))
187 y_section = a4_height
188 page.mediabox.bottom = half_height
189 page.mediabox.top = a4_height
192 page.mediabox.bottom = 0
193 page.mediabox.top = half_height
196 page.mediabox.left = 0
197 page.mediabox.right = half_width
199 page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*a4_width))
201 page.mediabox.left = half_width
202 page.mediabox.right = a4_width
203 page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
204 page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
205 new_page.merge_page(page)
207 print("merged page number", page_count)
210 from reportlab.pdfgen import canvas
213 packet = io.BytesIO()
214 c = canvas.Canvas(packet, pagesize=A4)
216 c.line(0, a4_height, a4_width, a4_height)
217 c.line(0, half_height, a4_width, half_height)
218 c.line(0, 0, a4_width, 0)
219 c.line(0, a4_height, 0, 0)
220 c.line(half_width, a4_height, half_width, 0)
221 c.line(a4_width, a4_height, a4_width, 0)
223 new_pdf = pypdf.PdfReader(packet)
224 new_page.merge_page(new_pdf.pages[0])
225 printable_offset_x = printable_margin
226 printable_offset_y = printable_margin * a4_height / a4_width
227 new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
228 new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
229 x_left_spine_limit = half_width * bonus_shrink_factor
230 x_right_spine_limit = a4_width - x_left_spine_limit
231 if args.analyze or front_page:
232 packet = io.BytesIO()
233 c = canvas.Canvas(packet, pagesize=A4)
237 c.line(x_left_spine_limit, a4_height, x_left_spine_limit, 0)
238 c.line(x_right_spine_limit, a4_height, x_right_spine_limit, 0)
241 cut_depth = 19.5 * points_per_mm
242 cut_width = 10.5 * points_per_mm
243 middle_point_depth = 4 * points_per_mm
245 start_up_left_left_x = x_left_spine_limit - 0.5 * cut_width
246 start_up_left_right_x = x_left_spine_limit + 0.5 * cut_width
247 middle_point_up_left_y = half_height + middle_point_depth
248 end_point_up_left_y = half_height + cut_depth
249 c.line(start_up_left_right_x, half_height, x_left_spine_limit, end_point_up_left_y)
250 c.line(x_left_spine_limit, end_point_up_left_y, x_left_spine_limit, middle_point_up_left_y)
251 c.line(x_left_spine_limit, middle_point_up_left_y, start_up_left_left_x, half_height)
253 start_down_right_left_x = x_right_spine_limit - 0.5 * cut_width
254 start_down_right_right_x = x_right_spine_limit + 0.5 * cut_width
255 middle_point_down_right_y = half_height - middle_point_depth
256 end_point_down_right_y = half_height - cut_depth
257 c.line(start_down_right_left_x, half_height, x_right_spine_limit, end_point_down_right_y)
258 c.line(x_right_spine_limit, end_point_down_right_y, x_right_spine_limit, middle_point_down_right_y)
259 c.line(x_right_spine_limit, middle_point_down_right_y, start_down_right_right_x, half_height)
261 if args.analyze or front_page:
263 new_pdf = pypdf.PdfReader(packet)
264 new_page.merge_page(new_pdf.pages[0])
265 writer.add_page(new_page)
267 front_page = not front_page
268 for file in opened_files:
271 with open(args.output_file, 'wb') as output_file:
272 writer.write(output_file)