Last active
August 22, 2018 09:44
-
-
Save nazavode/b32d9f1cfe67b8724f5afc7919c0c1f3 to your computer and use it in GitHub Desktop.
Produce correctly ordered page numbers for smooth booklet printing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python | |
| # Copyright: (c) 2018, Federico Ficarelli <federico.ficarelli@gmail.com> | |
| # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) | |
| import argparse | |
| parser = argparse.ArgumentParser(description="""Produces page numbers in the correct order for smooth booklet printing. | |
| The output from the booklet command can then be used as input to a printing | |
| application (e.g.: Acrobat Reader) or pdftk to directly rearrange pages: | |
| $ pdftk in.pdf cat $(booklet 10) output out.pdf | |
| Please note that in case of an odd page count, an extra page is inserted as | |
| the first one: | |
| $ booklet 3 | |
| 4 1 2 3 | |
| This workaround ensures that the first actual page is printed on the | |
| right side of the first face. The safest way to handle an odd page count | |
| is to manually add a blank page to the original document: | |
| $ convert xc:none -page A4 blank.pdf | |
| $ pdftk A=in.pdf B=blank.pdf cat A B output padded.pdf | |
| """, formatter_class=argparse.RawTextHelpFormatter) | |
| parser.add_argument('npages', metavar='N', type=int, help='total number of pages of source document') | |
| parser.add_argument('--separator', metavar='STR', type=str, default=' ', | |
| help='string separator to be used for numbers output (default: " ")') | |
| def booklet_pages(n_pages): | |
| n_p = n_pages if n_pages % 2 == 0 else n_pages + 1 | |
| n_faces = int(n_p / 2) | |
| for i_face in range(n_faces): | |
| if i_face % 2 == 0: | |
| i_page_right = i_face | |
| i_page_left = (n_p - 1) - i_page_right | |
| else: | |
| i_page_left = i_face | |
| i_page_right = (n_p - 1) - i_page_left | |
| yield i_page_left | |
| yield i_page_right | |
| if __name__ == '__main__': | |
| args = parser.parse_args() | |
| numbers = booklet_pages(args.npages) | |
| print(args.separator.join(str(i + 1) for i in numbers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment