cardinal_pythonlib.tools.pdf_to_booklet


Original code copyright (C) 2009-2022 Rudolf Cardinal (rudolf@pobox.com).

This file is part of cardinal_pythonlib.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


Command-line tool to make booklets from PDFs.

RNC, 18 Nov 2017.

PURPOSE:

Take a PDF created with pdfnup or similar, with A4 sheets and two pages per sheet, like this:

PDF page 1      +-----+-----+
                |     |     |
                |  1  |  2  |
                |     |     |
                +-----+-----+

PDF page 2      +-----+-----+
                |     |     |
                |  3  |  4  |
                |     |     |
                +-----+-----+

PDF page 3      +-----+-----+
                |     |     |
                |  5  |  6  |
                |     |     |
                +-----+-----+

and create a similar PDF but like this:

PDF page 1      +-----+-----+
                |     |     |
                |  6  |  1  |
                |     |     |
                +-----+-----+

PDF page 2      +-----+-----+
                |     |     |
                |  1  |  2  |
                |     |     |
                +-----+-----+

PDF page 3      +-----+-----+
                |     |     |
                |  1  |  2  |
                |     |     |
                +-----+-----+

so it can be printed double-sided and folded into an A5 booklet.

DEFINITIONS

  • page = one side of a piece of paper BUT HERE, IN A BOOK CONTEXT, half that, i.e. what ends up as a book “page”

  • pair = two pages, making up one side of a sheet/leaf

  • sheet = one piece of paper (= leaf) (= 4 pages, here)

PRINTING

It’s our job here to make pairs from pages, and to create a PDF where each PDF page is a pair.

It’s the printer’s job to make sheets from pages. When printing in duplex, you will need to use SHORT-EDGE BINDING (if you use long-edge binding, the reverse sides will be inverted).

FURTHER THOUGHT 19 Nov 2017

We can, of course, support LONG-EDGE binding as well; that just requires an extra step of rotating all the even-numbered pages from the preceding step. Supported, as below.

cardinal_pythonlib.tools.pdf_to_booklet.booklet_nup_pdf(input_filename: str, output_filename: str, latex_paper_size: str = 'a4paper') str[source]

Takes a PDF (e.g. A4) and makes a 2x1 booklet (e.g. 2xA5 per A4). The booklet can be folded like a book and the final pages will be in order. Returns the output filename.

cardinal_pythonlib.tools.pdf_to_booklet.calc_n_sheets(n_pages: int) int[source]

How many sheets does this number of pages need, on the basis of 2 pages per sheet?

cardinal_pythonlib.tools.pdf_to_booklet.calc_n_virtual_pages(n_sheets: int) int[source]

Converts #sheets to #pages, but rounding up to a multiple of 4.

cardinal_pythonlib.tools.pdf_to_booklet.convert_to_foldable(input_filename: str, output_filename: str, slice_horiz: int, slice_vert: int, overwrite: bool = False, longedge: bool = False, latex_paper_size: str = 'a4paper') bool[source]

Runs a chain of tasks to convert a PDF to a useful booklet PDF.

cardinal_pythonlib.tools.pdf_to_booklet.get_page_count(filename: str) int[source]

How many pages are in a PDF?

cardinal_pythonlib.tools.pdf_to_booklet.main() NoReturn[source]

Command-line processor. See --help for details.

cardinal_pythonlib.tools.pdf_to_booklet.make_blank_pdf(filename: str, paper: str = 'A4') None[source]

NOT USED. Makes a blank single-page PDF, using ImageMagick’s convert.

cardinal_pythonlib.tools.pdf_to_booklet.page_sequence(n_sheets: int, one_based: bool = True) List[int][source]

Generates the final page sequence from the starting number of sheets.

cardinal_pythonlib.tools.pdf_to_booklet.require(executable: str, explanation: str = '') None[source]

Ensures that the external tool is available. Asserts upon failure.

cardinal_pythonlib.tools.pdf_to_booklet.rotate_even_pages_180(input_filename: str, output_filename: str) str[source]

Rotates even-numbered pages 180 degrees. Returns the output filename.

cardinal_pythonlib.tools.pdf_to_booklet.run(args: List[str], get_output: bool = False, encoding: str = 'utf-8') Tuple[str, str][source]

Run an external command +/- return the results. Returns a (stdout, stderr) tuple (both are blank strings if the output wasn’t wanted).

cardinal_pythonlib.tools.pdf_to_booklet.slice_pdf(input_filename: str, output_filename: str, slice_horiz: int, slice_vert: int) str[source]

Slice each page of the original, to convert to “one real page per PDF page”. Return the output filename.