Skip to content

Instantly share code, notes, and snippets.

@naholyr
Last active September 19, 2023 11:45
Show Gist options
  • Save naholyr/aa0a15b3991e1e7ffc4f2ada20d39333 to your computer and use it in GitHub Desktop.
Save naholyr/aa0a15b3991e1e7ffc4f2ada20d39333 to your computer and use it in GitHub Desktop.
How to print a booklet, the copy-pasta tables

Printing a booklet

Printing instructions:

  • Duplex
    • short edge for A5 booklet
    • long edge for A6 booklet
  • X pages per side, so if you're printing on A4 sheets:
    • 2 pages per side for A5 booklet (~21×15 mm)
    • 4 pages per side for A6 booklet (~10×15 mm)

Depending on the target size, the trickiest part is to print pages in right order so you can just cut and fold and get a booklet. See table below.

The big table

Printing PDF of the exact correct number of pages (2/page requires a multiple of 4, 4/page requires multiple of 8), you can simply follow this table for pdf up to 60 pages so you don't have to compute anything yourself (you'll find the Node.js scripts that generated those lists and could generate print order for any number of pages):

2/page (A5)

PDF pages Print order
4 4,1,2,3
8 8,1,2,7,6,3,4,5
12 12,1,2,11,10,3,4,9,8,5,6,7
16 16,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9
20 20,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11
24 24,1,2,23,22,3,4,21,20,5,6,19,18,7,8,17,16,9,10,15,14,11,12,13
28 28,1,2,27,26,3,4,25,24,5,6,23,22,7,8,21,20,9,10,19,18,11,12,17,16,13,14,15
32 32,1,2,31,30,3,4,29,28,5,6,27,26,7,8,25,24,9,10,23,22,11,12,21,20,13,14,19,18,15,16,17
36 36,1,2,35,34,3,4,33,32,5,6,31,30,7,8,29,28,9,10,27,26,11,12,25,24,13,14,23,22,15,16,21,20,17,18,19
40 40,1,2,39,38,3,4,37,36,5,6,35,34,7,8,33,32,9,10,31,30,11,12,29,28,13,14,27,26,15,16,25,24,17,18,23,22,19,20,21
44 44,1,2,43,42,3,4,41,40,5,6,39,38,7,8,37,36,9,10,35,34,11,12,33,32,13,14,31,30,15,16,29,28,17,18,27,26,19,20,25,24,21,22,23
48 48,1,2,47,46,3,4,45,44,5,6,43,42,7,8,41,40,9,10,39,38,11,12,37,36,13,14,35,34,15,16,33,32,17,18,31,30,19,20,29,28,21,22,27,26,23,24,25
52 52,1,2,51,50,3,4,49,48,5,6,47,46,7,8,45,44,9,10,43,42,11,12,41,40,13,14,39,38,15,16,37,36,17,18,35,34,19,20,33,32,21,22,31,30,23,24,29,28,25,26,27
56 56,1,2,55,54,3,4,53,52,5,6,51,50,7,8,49,48,9,10,47,46,11,12,45,44,13,14,43,42,15,16,41,40,17,18,39,38,19,20,37,36,21,22,35,34,23,24,33,32,25,26,31,30,27,28,29
60 60,1,2,59,58,3,4,57,56,5,6,55,54,7,8,53,52,9,10,51,50,11,12,49,48,13,14,47,46,15,16,45,44,17,18,43,42,19,20,41,40,21,22,39,38,23,24,37,36,25,26,35,34,27,28,33,32,29,30,31

4/page (A6)

PDF pages Print order
8 2,7,4,5,8,1,6,3
16 2,15,6,11,16,1,12,5,4,13,8,9,14,3,10,7
24 2,23,8,17,24,1,18,7,4,21,10,15,22,3,16,9,6,19,12,13,20,5,14,11
32 2,31,10,23,32,1,24,9,4,29,12,21,30,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
40 2,39,12,29,40,1,30,11,4,37,14,27,38,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
48 2,47,14,35,48,1,36,13,4,45,16,33,46,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
56 2,55,16,41,56,1,42,15,4,53,18,39,54,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
64 2,63,18,47,64,1,48,17,4,61,20,45,62,3,46,19,6,59,22,43,60,5,44,21,8,57,24,41,58,7,42,23,10,55,26,39,56,9,40,25,12,53,28,37,54,11,38,27,14,51,30,35,52,13,36,29,16,49,32,33,50,15,34,31

Other numbers of pages

Obviously not all PDF come with the exact correct number of pages to be printed the way you want, then add a blank page to your PDF and replace extraneous pages with this blank one.

Example, if you want to print a 12 pages PDF into A6 booklet:

  • Add a blank page: you now have a 13 pages PDF
  • Print as a 16 pages PDF, by replacing any pages greater than 13 with 13
    • 2,13,6,11,13,1,12,5,4,13,8,9,13,3,10,7
Click here to see the whole list, for copy-pasta convenience
PDF pages Booklet size Print order
5 A5 6,1,2,6,6,3,4,5
6 A5 7,1,2,7,6,3,4,5
7 A5 8,1,2,7,6,3,4,5
9 A5 10,1,2,10,10,3,4,9,8,5,6,7
10 A5 11,1,2,11,10,3,4,9,8,5,6,7
11 A5 12,1,2,11,10,3,4,9,8,5,6,7
13 A5 14,1,2,14,14,3,4,13,12,5,6,11,10,7,8,9
14 A5 15,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9
15 A5 16,1,2,15,14,3,4,13,12,5,6,11,10,7,8,9
17 A5 18,1,2,18,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11
18 A5 19,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11
19 A5 20,1,2,19,18,3,4,17,16,5,6,15,14,7,8,13,12,9,10,11
21 A5 22,1,2,22,22,3,4,21,20,5,6,19,18,7,8,17,16,9,10,15,14,11,12,13
22 A5 23,1,2,23,22,3,4,21,20,5,6,19,18,7,8,17,16,9,10,15,14,11,12,13
23 A5 24,1,2,23,22,3,4,21,20,5,6,19,18,7,8,17,16,9,10,15,14,11,12,13
25 A5 26,1,2,26,26,3,4,25,24,5,6,23,22,7,8,21,20,9,10,19,18,11,12,17,16,13,14,15
26 A5 27,1,2,27,26,3,4,25,24,5,6,23,22,7,8,21,20,9,10,19,18,11,12,17,16,13,14,15
27 A5 28,1,2,27,26,3,4,25,24,5,6,23,22,7,8,21,20,9,10,19,18,11,12,17,16,13,14,15
29 A5 30,1,2,30,30,3,4,29,28,5,6,27,26,7,8,25,24,9,10,23,22,11,12,21,20,13,14,19,18,15,16,17
30 A5 31,1,2,31,30,3,4,29,28,5,6,27,26,7,8,25,24,9,10,23,22,11,12,21,20,13,14,19,18,15,16,17
31 A5 32,1,2,31,30,3,4,29,28,5,6,27,26,7,8,25,24,9,10,23,22,11,12,21,20,13,14,19,18,15,16,17
33 A5 34,1,2,34,34,3,4,33,32,5,6,31,30,7,8,29,28,9,10,27,26,11,12,25,24,13,14,23,22,15,16,21,20,17,18,19
34 A5 35,1,2,35,34,3,4,33,32,5,6,31,30,7,8,29,28,9,10,27,26,11,12,25,24,13,14,23,22,15,16,21,20,17,18,19
35 A5 36,1,2,35,34,3,4,33,32,5,6,31,30,7,8,29,28,9,10,27,26,11,12,25,24,13,14,23,22,15,16,21,20,17,18,19
37 A5 38,1,2,38,38,3,4,37,36,5,6,35,34,7,8,33,32,9,10,31,30,11,12,29,28,13,14,27,26,15,16,25,24,17,18,23,22,19,20,21
38 A5 39,1,2,39,38,3,4,37,36,5,6,35,34,7,8,33,32,9,10,31,30,11,12,29,28,13,14,27,26,15,16,25,24,17,18,23,22,19,20,21
39 A5 40,1,2,39,38,3,4,37,36,5,6,35,34,7,8,33,32,9,10,31,30,11,12,29,28,13,14,27,26,15,16,25,24,17,18,23,22,19,20,21
41 A5 42,1,2,42,42,3,4,41,40,5,6,39,38,7,8,37,36,9,10,35,34,11,12,33,32,13,14,31,30,15,16,29,28,17,18,27,26,19,20,25,24,21,22,23
42 A5 43,1,2,43,42,3,4,41,40,5,6,39,38,7,8,37,36,9,10,35,34,11,12,33,32,13,14,31,30,15,16,29,28,17,18,27,26,19,20,25,24,21,22,23
43 A5 44,1,2,43,42,3,4,41,40,5,6,39,38,7,8,37,36,9,10,35,34,11,12,33,32,13,14,31,30,15,16,29,28,17,18,27,26,19,20,25,24,21,22,23
45 A5 46,1,2,46,46,3,4,45,44,5,6,43,42,7,8,41,40,9,10,39,38,11,12,37,36,13,14,35,34,15,16,33,32,17,18,31,30,19,20,29,28,21,22,27,26,23,24,25
46 A5 47,1,2,47,46,3,4,45,44,5,6,43,42,7,8,41,40,9,10,39,38,11,12,37,36,13,14,35,34,15,16,33,32,17,18,31,30,19,20,29,28,21,22,27,26,23,24,25
47 A5 48,1,2,47,46,3,4,45,44,5,6,43,42,7,8,41,40,9,10,39,38,11,12,37,36,13,14,35,34,15,16,33,32,17,18,31,30,19,20,29,28,21,22,27,26,23,24,25
49 A5 50,1,2,50,50,3,4,49,48,5,6,47,46,7,8,45,44,9,10,43,42,11,12,41,40,13,14,39,38,15,16,37,36,17,18,35,34,19,20,33,32,21,22,31,30,23,24,29,28,25,26,27
50 A5 51,1,2,51,50,3,4,49,48,5,6,47,46,7,8,45,44,9,10,43,42,11,12,41,40,13,14,39,38,15,16,37,36,17,18,35,34,19,20,33,32,21,22,31,30,23,24,29,28,25,26,27
51 A5 52,1,2,51,50,3,4,49,48,5,6,47,46,7,8,45,44,9,10,43,42,11,12,41,40,13,14,39,38,15,16,37,36,17,18,35,34,19,20,33,32,21,22,31,30,23,24,29,28,25,26,27
53 A5 54,1,2,54,54,3,4,53,52,5,6,51,50,7,8,49,48,9,10,47,46,11,12,45,44,13,14,43,42,15,16,41,40,17,18,39,38,19,20,37,36,21,22,35,34,23,24,33,32,25,26,31,30,27,28,29
54 A5 55,1,2,55,54,3,4,53,52,5,6,51,50,7,8,49,48,9,10,47,46,11,12,45,44,13,14,43,42,15,16,41,40,17,18,39,38,19,20,37,36,21,22,35,34,23,24,33,32,25,26,31,30,27,28,29
55 A5 56,1,2,55,54,3,4,53,52,5,6,51,50,7,8,49,48,9,10,47,46,11,12,45,44,13,14,43,42,15,16,41,40,17,18,39,38,19,20,37,36,21,22,35,34,23,24,33,32,25,26,31,30,27,28,29
57 A5 58,1,2,58,58,3,4,57,56,5,6,55,54,7,8,53,52,9,10,51,50,11,12,49,48,13,14,47,46,15,16,45,44,17,18,43,42,19,20,41,40,21,22,39,38,23,24,37,36,25,26,35,34,27,28,33,32,29,30,31
58 A5 59,1,2,59,58,3,4,57,56,5,6,55,54,7,8,53,52,9,10,51,50,11,12,49,48,13,14,47,46,15,16,45,44,17,18,43,42,19,20,41,40,21,22,39,38,23,24,37,36,25,26,35,34,27,28,33,32,29,30,31
59 A5 60,1,2,59,58,3,4,57,56,5,6,55,54,7,8,53,52,9,10,51,50,11,12,49,48,13,14,47,46,15,16,45,44,17,18,43,42,19,20,41,40,21,22,39,38,23,24,37,36,25,26,35,34,27,28,33,32,29,30,31
4 A6 2,5,4,5,5,1,5,3
5 A6 2,6,4,5,6,1,6,3
6 A6 2,7,4,5,7,1,6,3
7 A6 2,7,4,5,8,1,6,3
9 A6 2,10,6,10,10,1,10,5,4,10,8,9,10,3,10,7
10 A6 2,11,6,11,11,1,11,5,4,11,8,9,11,3,10,7
11 A6 2,12,6,11,12,1,12,5,4,12,8,9,12,3,10,7
12 A6 2,13,6,11,13,1,12,5,4,13,8,9,13,3,10,7
13 A6 2,14,6,11,14,1,12,5,4,13,8,9,14,3,10,7
14 A6 2,15,6,11,15,1,12,5,4,13,8,9,14,3,10,7
15 A6 2,15,6,11,16,1,12,5,4,13,8,9,14,3,10,7
17 A6 2,18,8,17,18,1,18,7,4,18,10,15,18,3,16,9,6,18,12,13,18,5,14,11
18 A6 2,19,8,17,19,1,18,7,4,19,10,15,19,3,16,9,6,19,12,13,19,5,14,11
19 A6 2,20,8,17,20,1,18,7,4,20,10,15,20,3,16,9,6,19,12,13,20,5,14,11
20 A6 2,21,8,17,21,1,18,7,4,21,10,15,21,3,16,9,6,19,12,13,20,5,14,11
21 A6 2,22,8,17,22,1,18,7,4,21,10,15,22,3,16,9,6,19,12,13,20,5,14,11
22 A6 2,23,8,17,23,1,18,7,4,21,10,15,22,3,16,9,6,19,12,13,20,5,14,11
23 A6 2,23,8,17,24,1,18,7,4,21,10,15,22,3,16,9,6,19,12,13,20,5,14,11
25 A6 2,26,10,23,26,1,24,9,4,26,12,21,26,3,22,11,6,26,14,19,26,5,20,13,8,25,16,17,26,7,18,15
26 A6 2,27,10,23,27,1,24,9,4,27,12,21,27,3,22,11,6,27,14,19,27,5,20,13,8,25,16,17,26,7,18,15
27 A6 2,28,10,23,28,1,24,9,4,28,12,21,28,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
28 A6 2,29,10,23,29,1,24,9,4,29,12,21,29,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
29 A6 2,30,10,23,30,1,24,9,4,29,12,21,30,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
30 A6 2,31,10,23,31,1,24,9,4,29,12,21,30,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
31 A6 2,31,10,23,32,1,24,9,4,29,12,21,30,3,22,11,6,27,14,19,28,5,20,13,8,25,16,17,26,7,18,15
33 A6 2,34,12,29,34,1,30,11,4,34,14,27,34,3,28,13,6,34,16,25,34,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
34 A6 2,35,12,29,35,1,30,11,4,35,14,27,35,3,28,13,6,35,16,25,35,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
35 A6 2,36,12,29,36,1,30,11,4,36,14,27,36,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
36 A6 2,37,12,29,37,1,30,11,4,37,14,27,37,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
37 A6 2,38,12,29,38,1,30,11,4,37,14,27,38,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
38 A6 2,39,12,29,39,1,30,11,4,37,14,27,38,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
39 A6 2,39,12,29,40,1,30,11,4,37,14,27,38,3,28,13,6,35,16,25,36,5,26,15,8,33,18,23,34,7,24,17,10,31,20,21,32,9,22,19
41 A6 2,42,14,35,42,1,36,13,4,42,16,33,42,3,34,15,6,42,18,31,42,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
42 A6 2,43,14,35,43,1,36,13,4,43,16,33,43,3,34,15,6,43,18,31,43,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
43 A6 2,44,14,35,44,1,36,13,4,44,16,33,44,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
44 A6 2,45,14,35,45,1,36,13,4,45,16,33,45,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
45 A6 2,46,14,35,46,1,36,13,4,45,16,33,46,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
46 A6 2,47,14,35,47,1,36,13,4,45,16,33,46,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
47 A6 2,47,14,35,48,1,36,13,4,45,16,33,46,3,34,15,6,43,18,31,44,5,32,17,8,41,20,29,42,7,30,19,10,39,22,27,40,9,28,21,12,37,24,25,38,11,26,23
49 A6 2,50,16,41,50,1,42,15,4,50,18,39,50,3,40,17,6,50,20,37,50,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
50 A6 2,51,16,41,51,1,42,15,4,51,18,39,51,3,40,17,6,51,20,37,51,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
51 A6 2,52,16,41,52,1,42,15,4,52,18,39,52,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
52 A6 2,53,16,41,53,1,42,15,4,53,18,39,53,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
53 A6 2,54,16,41,54,1,42,15,4,53,18,39,54,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
54 A6 2,55,16,41,55,1,42,15,4,53,18,39,54,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
55 A6 2,55,16,41,56,1,42,15,4,53,18,39,54,3,40,17,6,51,20,37,52,5,38,19,8,49,22,35,50,7,36,21,10,47,24,33,48,9,34,23,12,45,26,31,46,11,32,25,14,43,28,29,44,13,30,27
57 A6 2,58,18,47,58,1,48,17,4,58,20,45,58,3,46,19,6,58,22,43,58,5,44,21,8,57,24,41,58,7,42,23,10,55,26,39,56,9,40,25,12,53,28,37,54,11,38,27,14,51,30,35,52,13,36,29,16,49,32,33,50,15,34,31
58 A6 2,59,18,47,59,1,48,17,4,59,20,45,59,3,46,19,6,59,22,43,59,5,44,21,8,57,24,41,58,7,42,23,10,55,26,39,56,9,40,25,12,53,28,37,54,11,38,27,14,51,30,35,52,13,36,29,16,49,32,33,50,15,34,31
59 A6 2,60,18,47,60,1,48,17,4,60,20,45,60,3,46,19,6,59,22,43,60,5,44,21,8,57,24,41,58,7,42,23,10,55,26,39,56,9,40,25,12,53,28,37,54,11,38,27,14,51,30,35,52,13,36,29,16,49,32,33,50,15,34,31

Adding blank page

  • Online tools like PDF-UX have this option
  • Acrobat can do it too
  • Otherwise you can find a blank PDF and use pdftk:
    • pdftk input.pdf blank.pdf cat output output.pdf
// 1/page = 20×30
// 2/page = 15×20
// algo général:
// booklet 2/page = front A: n,1 ; back A: 2,n-1 ; front B: n-2,3 ; back B: 4,n-3 ; …
// côté front on décrémente à gauche, incrémente à droite, et on inverse pour le back
// exemple: 8,1,2,7,6,3,4,5
const nPages = Number(process.argv[2] || 4); // multiple of 8
const print = [];
let decr = nPages;
let incr = 1;
for (let i = 0; i < nPages; i++) {
const r = i % 4;
if (r === 0 || r === 3) {
print.push(decr--);
} else {
print.push(incr++);
}
}
console.log(print.join(','));
// 1/page = 20×30
// 2/page = 15×20
// 4/page = 10×15
// TODO 6/page = 10×10 → pour avoir le ~7×10 il faudrait choisir une taille de papier ~15×30, il faut aussi choisir le mode paysage
// TODO 9/page = 7×10
// algo général:
// booklet 2/page = front A: n,1 ; back A: 2,n-1 ; front B: n-2,3 ; back B: 4,n-3 ; …
// côté front on décrémente à gauche, incrémente à droite, et on inverse pour le back
// exemple: 8,1,2,7,6,3,4,5
// booklet 4/page = 1 feuille = 2 feuilles de 2/page = n,1,2,n-1;n-2,3,4,n-3 ; …
// on doit intercaler les fronts et les backs (les 4 front sur la première page, les 4 backs sur la 2e page)
// = front A: n,1,n-2,3 ; back A: 2,n-1,4,n-3 ; …
// exemple: 8,1,6,3,2,7,4,5
// booklet 6/page = 1 feuille = 3 feuilles de 2/page = n,1,2,n-1;n-2,3,4,n-3;n-4,5,6,n-5
// on doit intercaler les front et les back comme pour 4/p (les 6 fronts sur la première page, les 6 back sur la 2e page)
// = front A: n,1,n-2,3,n-4,5 ; back A = 2,n-1,4,n-3,6,n-5
// exemple 12,1,10,3,8,5,2,11,4,9,6,7
// booklet 9/page = idem 6/page mais on supprime la dernière colonne du front / première colonne du back
//
// = front A: n,1,x,n-2,3,x,n-4,5,x ; back A = x,2,n-1,x,4,n-3,x,6,n-5
// exemple 12,1,x,10,3,x,8,5,x,x,2,11,x,4,9,x,6,7
// booklet 12/page = ...
// booklet 16/page = ...
// 4/page = 8 per sheet
const nPages = Number(process.argv[2] || 8); // multiple of 8
const nSheets = nPages / 8;
// create physical A4 sheets and number the physical A6 pages on them
// number all A4 sheets
const sheetsA4 = []; // nSheets
for (let n = 0; n < nSheets; n++) {
const curPage = n * 8 + 1;
sheetsA4.push({
frontPhysical: [curPage, curPage + 1, curPage + 2, curPage + 3],
backPhysical: [curPage + 4, curPage + 5, curPage + 6, curPage + 7],
});
}
console.log('A4', sheetsA4);
// virtually rip in half horizontally
const sheetsA5 = []; // nSheets * 2
// top half
for (let n = 0; n < nSheets; n++) {
sheetsA5.push({
frontPhysical: [sheetsA4[n].frontPhysical[0], sheetsA4[n].frontPhysical[1]],
backPhysical: [sheetsA4[n].backPhysical[0], sheetsA4[n].backPhysical[1]],
});
}
// bottom half
for (let n = 0; n < nSheets; n++) {
sheetsA5.push({
frontPhysical: [sheetsA4[n].frontPhysical[2], sheetsA4[n].frontPhysical[3]],
backPhysical: [sheetsA4[n].backPhysical[2], sheetsA4[n].backPhysical[3]],
});
}
console.log('A5', sheetsA5);
console.log("virtual horizontal A5 sheets after cutting horizontally:")
for (let n = 0; n < nSheets * 2; n++) {
console.log("front %s back %s", sheetsA5[n].frontPhysical, sheetsA5[n].backPhysical);
}
// number all physical pages in book order
let bookOrder = []; // nPages (this is a translation table, i.e. book page 1 belongs to nth position)
// left pages of the stack going up = first half book pages
for (let n = 0; n < sheetsA5.length; n++) { // nSheets * 2
// back right
bookOrder.push(sheetsA5[n].backPhysical[1]);
// front left
bookOrder.push(sheetsA5[n].frontPhysical[0]);
}
// right pages of the stack going down = second half book pages
for (let n = sheetsA5.length - 1; n >= 0; n--) {
// front right
bookOrder.push(sheetsA5[n].frontPhysical[1]);
// back left
bookOrder.push(sheetsA5[n].backPhysical[0]);
}
console.log('Book order', bookOrder);
// apply translation
let printOrder = [];
for (let index = 0; index < bookOrder.length; index++) {
printOrder[bookOrder[index] - 1] = index + 1;
}
console.log('Print order:', printOrder.join(','));
/*
// print pdftk command-line
fmt.Printf("pdftk A=%s cat", inpath)
for _, pgNumber := range printOrder {
fmt.Printf(" A%d", pgNumber)
}
fmt.Printf(" output %s\n", outpath)
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment