home · contact · privacy
In Bookmaker, allow multiple (optiopnally page-ranged) crop definitions.
[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
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()
19
20
21 # select pages from input files
22 def parse_page_range(range_string, pages):
23     start_page = 0
24     end_page = len(pages)
25     if range_string:
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"):
30             end_page = int(end)
31     return start_page, end_page
32 pages_to_add = []
33 opened_files = []
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)
38     range_string = None
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))
46
47 # rotate page canvas
48 if args.rotate:
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))
54
55 # normalize all pages to portrait A4
56 for page in pages_to_add:
57     if "/Rotate" in page:
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
64
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)
68 if args.crop_range:
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]
74       else:
75           page_range = None
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
81       zoom = 1
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.")
86           exit(1)
87       elif zoom_horizontal + zoom_vertical > 2:
88           zoom = min(zoom_horizontal, zoom_vertical)
89       else:
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
94
95 writer = pypdf.PdfWriter()
96 if not args.nup4:
97     odd_page = True
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))
103         else:
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
112
113 else:
114     points_per_mm = 2.83465
115     n_pages_per_axis = 2
116     spine_limit = 10
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
124     new_page_order = []
125     new_i_order = []
126     eight_pack = []
127     mod_to_8 = len(pages_to_add) % 8
128     if mod_to_8 > 0:
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]
132     i = 0
133     n_eights = 0
134     for page in pages_to_add:
135         if i == 0:
136             eight_pack = []
137         eight_pack += [page]
138         i += 1
139         if i == 8:
140             i = 0
141             new_i_order += [8 * n_eights + 3,
142                             8 * n_eights + 0,
143                             8 * n_eights + 7,
144                             8 * n_eights + 4,
145                             8 * n_eights + 1,
146                             8 * n_eights + 2,
147                             8 * n_eights + 5,
148                             8 * n_eights + 6]
149             n_eights += 1
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
158     i = 0
159     page_count = 0
160     front_page = True
161     for j, page in enumerate(new_page_order):
162         if i == 0:
163             new_page = pypdf.PageObject.create_blank_page(width=a4_width, height=a4_height)
164
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))))
170         if i == 0 or i == 2:
171             if args.symmetry:
172                 page.add_transformation(pypdf.Transformation().translate(tx=-crop_left))
173             else:
174                 page.add_transformation(pypdf.Transformation().translate(tx=-crop_right))
175         elif i == 1 or i == 3:
176             if args.symmetry:
177                 page.add_transformation(pypdf.Transformation().translate(tx=(a4_width / zoom - (a4_width - crop_left))))
178             else:
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))
181         if i == 2 or i == 3:
182             page.add_transformation(pypdf.Transformation().translate(ty=-2*printable_margin/printable_scale))
183
184         # outer section transformations
185         page.add_transformation(pypdf.Transformation().translate(ty=(1-bonus_shrink_factor)*a4_height))
186         if i == 0 or i == 1:
187             y_section = a4_height
188             page.mediabox.bottom = half_height
189             page.mediabox.top    = a4_height
190         if i == 2 or i == 3:
191             y_section = 0
192             page.mediabox.bottom = 0
193             page.mediabox.top  =   half_height
194         if i == 0 or i == 2:
195             x_section = 0
196             page.mediabox.left   = 0
197             page.mediabox.right  = half_width
198         if i == 1 or i == 3:
199             page.add_transformation(pypdf.Transformation().translate(tx=(1-bonus_shrink_factor)*a4_width))
200             x_section = 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)
206         page_count += 1
207         print("merged page number", page_count)
208         i += 1
209         if i > 3:
210             from reportlab.pdfgen import canvas
211             if args.analyze:
212                 # borders
213                 packet = io.BytesIO()
214                 c = canvas.Canvas(packet, pagesize=A4)
215                 c.setLineWidth(0.1)
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)
222                 c.save()
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)
234             if args.analyze:
235                 # # spine lines
236                 c.setLineWidth(0.1)
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)
239             if front_page:
240                 c.setLineWidth(0.2)
241                 cut_depth = 19.5 * points_per_mm
242                 cut_width = 10.5 * points_per_mm
243                 middle_point_depth = 4 * points_per_mm
244
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)
252
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)
260
261             if args.analyze or front_page:
262                 c.save()
263                 new_pdf = pypdf.PdfReader(packet)
264                 new_page.merge_page(new_pdf.pages[0])
265             writer.add_page(new_page)
266             i = 0
267             front_page = not front_page
268 for file in opened_files:
269     file.close()
270
271 with open(args.output_file, 'wb') as output_file:
272     writer.write(output_file)