home · contact · privacy
Bookmaker: more expressive output.
[misc] / bookmaker.py
1 #!/usr/bin/env python3
2 import pypdf
3 import argparse
4 import io
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
12
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()
24
25
26 # select pages from input files
27 def parse_page_range(range_string, pages):
28     start_page = 0
29     end_page = len(pages)
30     if range_string:
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"):
35             end_page = int(end)
36     return start_page, end_page
37 pages_to_add = []
38 opened_files = []
39 new_page_num = 0
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)
44     range_string = None
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):
49         new_page_num += 1
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))
53
54 # if necessary, pad pages to multiple of 8
55 if args.nup4:
56     mod_to_8 = len(pages_to_add) % 8
57     if mod_to_8 > 0:
58         print("-n: number of input pages %d not multiple of 8, padding to that" % len(pages_to_add))
59         for _ in range(8 - mod_to_8):
60             new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
61             pages_to_add += [new_page]
62
63 # rotate page canvas
64 if args.rotate:
65     for rotate in args.rotate:
66         page = pages_to_add[rotate - 1]
67         page.add_transformation(pypdf.Transformation().translate(tx=-a4_width/2, ty=-a4_height/2))
68         page.add_transformation(pypdf.Transformation().rotate(-90))
69         page.add_transformation(pypdf.Transformation().translate(tx=a4_width/2, ty=a4_height/2))
70         print("-r: rotating (by 90°) page", rotate)
71
72 # normalize all pages to portrait A4
73 for page in pages_to_add:
74     if "/Rotate" in page:
75         page.rotate(360 - page["/Rotate"])
76     page.mediabox.left = 0
77     page.mediabox.bottom = 0
78     page.mediabox.top = a4_height
79     page.mediabox.right = a4_width
80     page.cropbox = page.mediabox
81
82 # determine page crops, zooms, crop symmetry
83 crops_at_page = [(0,0,0,0)]*len(pages_to_add)
84 zoom_at_page = [1]*len(pages_to_add)
85 if args.crop_range:
86   for crop_range in args.crop_range:
87       initial_split = crop_range.split(':')
88       if len(initial_split) > 1:
89           page_range = initial_split[0]
90           crops = initial_split[1]
91       else:
92           page_range = None
93           crops = initial_split[0]
94       start_page, end_page = parse_page_range(page_range, pages_to_add)
95       crop_left_cm, crop_bottom_cm, crop_right_cm, crop_top_cm = [float(x) for x in  crops.split(',')]
96       crop_left = crop_left_cm * points_per_cm
97       crop_bottom = crop_bottom_cm * points_per_cm
98       crop_right = crop_right_cm * points_per_cm
99       crop_top = crop_top_cm * points_per_cm
100       if args.symmetry:
101           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))
102       else:
103           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))
104       cropped_width  = a4_width - crop_left - crop_right
105       cropped_height = a4_height - crop_bottom - crop_top
106       zoom = 1
107       zoom_horizontal = a4_width / (a4_width - crop_left - crop_right)
108       zoom_vertical = a4_height / (a4_height - crop_bottom - crop_top)
109       if (zoom_horizontal > 1 and zoom_vertical < 1) or (zoom_horizontal < 1 and zoom_vertical > 1):
110           print("Error: opposing zooms.")
111           exit(1)
112       elif zoom_horizontal + zoom_vertical > 2:
113           zoom = min(zoom_horizontal, zoom_vertical)
114       else:
115           zoom = max(zoom_horizontal, zoom_vertical)
116       for page_num in range(start_page, end_page):
117           if args.symmetry and page_num % 2:
118               crops_at_page[page_num] = (crop_right, crop_bottom, crop_left, crop_top)
119           else:
120               crops_at_page[page_num] = (crop_left, crop_bottom, crop_right, crop_top)
121           zoom_at_page[page_num] = zoom
122
123 writer = pypdf.PdfWriter()
124 if not args.nup4:
125     # single-page output
126     print("building 1-input-page-per-output-page book")
127     odd_page = True
128     for i, page in enumerate(pages_to_add):
129         crop_left, crop_bottom, crop_right, crop_top = crops_at_page[i]
130         zoom = zoom_at_page[i]
131         page.add_transformation(pypdf.Transformation().translate(tx=-crop_left, ty=-crop_bottom))
132         page.add_transformation(pypdf.Transformation().scale(zoom, zoom))
133         cropped_width  = a4_width - crop_left - crop_right
134         cropped_height = a4_height - crop_bottom - crop_top
135         page.mediabox.right = cropped_width * zoom
136         page.mediabox.top = cropped_height * zoom
137         writer.add_page(page)
138         odd_page = not odd_page
139         print("built page number %d (of %d)" % (i+1, len(pages_to_add)))
140
141 else:
142     print("-n: building 4-input-pages-per-output-page book")
143     print("-m: applying printable-area margin of %.2fcm" % args.margin)
144     if args.analyze:
145         print("-a: drawing page borders, spine limits")
146     n_pages_per_axis = 2
147     printable_margin = args.margin * points_per_cm
148     printable_scale = (a4_width - 2*printable_margin)/a4_width
149     half_width = a4_width / n_pages_per_axis
150     half_height = a4_height / n_pages_per_axis
151     section_scale_factor = 1 / n_pages_per_axis
152     spine_part_of_page = (spine_limit / half_width) / printable_scale
153     bonus_shrink_factor = 1 - spine_part_of_page
154     new_page_order = []
155     new_i_order = []
156     eight_pack = []
157     i = 0
158     n_eights = 0
159     for page in pages_to_add:
160         if i == 0:
161             eight_pack = []
162         eight_pack += [page]
163         i += 1
164         if i == 8:
165             i = 0
166             new_i_order += [8 * n_eights + 3,
167                             8 * n_eights + 0,
168                             8 * n_eights + 7,
169                             8 * n_eights + 4,
170                             8 * n_eights + 1,
171                             8 * n_eights + 2,
172                             8 * n_eights + 5,
173                             8 * n_eights + 6]
174             n_eights += 1
175             new_page_order += [eight_pack[3]]  # page front, upper left
176             new_page_order += [eight_pack[0]]  # page front, upper right
177             new_page_order += [eight_pack[7]]  # page front, lower left
178             new_page_order += [eight_pack[4]]  # page front, lower right
179             new_page_order += [eight_pack[1]]  # page back, upper left
180             new_page_order += [eight_pack[2]]  # page back, upper right
181             new_page_order += [eight_pack[5]]  # page back, lower left
182             new_page_order += [eight_pack[6]]  # page back, lower right
183     i = 0
184     page_count = 0
185     front_page = True
186     for j, page in enumerate(new_page_order):
187         if i == 0:
188             new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
189
190         # in-section transformations: align pages on top, left-hand pages to left, right-hand to right
191         new_i = new_i_order[j]
192         crop_left, crop_bottom, crop_right, crop_top = crops_at_page[new_i]
193         zoom = zoom_at_page[new_i]
194         page.add_transformation(pypdf.Transformation().translate(ty=(a4_height / zoom - (a4_height - crop_top))))
195         if i == 0 or i == 2:
196             page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
197         elif i == 1 or i == 3:
198             page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_right))))
199         page.add_transformation(pypdf.Transformation().scale(zoom * bonus_shrink_factor, zoom * bonus_shrink_factor))
200         if i == 2 or i == 3:
201             page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale))
202
203         # outer section transformations
204         page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*a4_height))
205         if i == 0 or i == 1:
206             y_section = a4_height
207             page.mediabox.bottom = half_height
208             page.mediabox.top    = a4_height
209         if i == 2 or i == 3:
210             y_section = 0
211             page.mediabox.bottom = 0
212             page.mediabox.top  =   half_height
213         if i == 0 or i == 2:
214             x_section = 0
215             page.mediabox.left   = 0
216             page.mediabox.right  = half_width
217         if i == 1 or i == 3:
218             page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*a4_width))
219             x_section = a4_width
220             page.mediabox.left   = half_width
221             page.mediabox.right  = a4_width
222         page.add_transformation(pypdf.Transformation().translate(tx=x_section, ty=y_section))
223         page.add_transformation(pypdf.Transformation().scale(section_scale_factor, section_scale_factor))
224         new_page.merge_page(page)
225         page_count += 1
226         print("merged page number %d (of %d)" % (page_count, len(pages_to_add)))
227         i += 1
228         if i > 3:
229             from reportlab.pdfgen import canvas
230             if args.analyze:
231                 # borders
232                 packet = io.BytesIO()
233                 c = canvas.Canvas(packet, pagesize=A4)
234                 c.setLineWidth(0.1)
235                 c.line(0, a4_height, a4_width, a4_height)
236                 c.line(0, half_height, a4_width, half_height)
237                 c.line(0, 0, a4_width, 0)
238                 c.line(0, a4_height, 0, 0)
239                 c.line(half_width, a4_height, half_width, 0)
240                 c.line(a4_width, a4_height, a4_width, 0)
241                 c.save()
242                 new_pdf = pypdf.PdfReader(packet)
243                 new_page.merge_page(new_pdf.pages[0])
244             printable_offset_x = printable_margin
245             printable_offset_y = printable_margin * a4_height / a4_width
246             new_page.add_transformation(pypdf.Transformation().scale(printable_scale, printable_scale))
247             new_page.add_transformation(pypdf.Transformation().translate(tx=printable_offset_x, ty=printable_offset_y))
248             x_left_spine_limit = half_width * bonus_shrink_factor
249             x_right_spine_limit = a4_width - x_left_spine_limit
250             if args.analyze or front_page:
251                 packet = io.BytesIO()
252                 c = canvas.Canvas(packet, pagesize=A4)
253             if args.analyze:
254                 # # spine lines
255                 c.setLineWidth(0.1)
256                 c.line(x_left_spine_limit, a4_height, x_left_spine_limit, 0)
257                 c.line(x_right_spine_limit, a4_height, x_right_spine_limit, 0)
258             if front_page:
259                 c.setLineWidth(0.2)
260
261                 start_up_left_left_x = x_left_spine_limit - 0.5 * cut_width
262                 start_up_left_right_x = x_left_spine_limit + 0.5 * cut_width
263                 middle_point_up_left_y = half_height + middle_point_depth
264                 end_point_up_left_y = half_height + cut_depth
265                 c.line(start_up_left_right_x, half_height, x_left_spine_limit, end_point_up_left_y)
266                 c.line(x_left_spine_limit, end_point_up_left_y, x_left_spine_limit, middle_point_up_left_y)
267                 c.line(x_left_spine_limit, middle_point_up_left_y, start_up_left_left_x, half_height)
268
269                 start_down_right_left_x = x_right_spine_limit - 0.5 * cut_width
270                 start_down_right_right_x = x_right_spine_limit + 0.5 * cut_width
271                 middle_point_down_right_y = half_height - middle_point_depth
272                 end_point_down_right_y = half_height - cut_depth
273                 c.line(start_down_right_left_x, half_height, x_right_spine_limit, end_point_down_right_y)
274                 c.line(x_right_spine_limit, end_point_down_right_y, x_right_spine_limit, middle_point_down_right_y)
275                 c.line(x_right_spine_limit, middle_point_down_right_y, start_down_right_right_x, half_height)
276
277             if args.analyze or front_page:
278                 c.save()
279                 new_pdf = pypdf.PdfReader(packet)
280                 new_page.merge_page(new_pdf.pages[0])
281             writer.add_page(new_page)
282             i = 0
283             front_page = not front_page
284
285 # write and close
286 for file in opened_files:
287     file.close()
288 with open(args.output_file, 'wb') as output_file:
289     writer.write(output_file)