A script that combines multiple pages of a PDF file into one page without margins

Motivation

As a method (2up, 4up, ... n-up) to combine multiple pages of PDF into one page, for example, "Preview" of macOS, etc. ** "File"-> "Print ..."-> Select the number of pages / sheet on the "Layout" tab-> "Save as PDF" ** The method can be mentioned. However, I couldn't find a way to avoid the margins between the pages that were put together with this method.

The first and second pages are, respectively. 元ファイル1ページ目 元ファイル2ページ目 If you try to 2up a PDF file that contains elements up to the edge of the page as shown above, macOSのプリント機能でまーじした場合 It can be seen that the margin is set as shown in, and there is a gap between pages. l This is a bit disappointing for output for purposes such as ** splitting large sheets of paper and scanning PDF files back together on a single page **. I think there is a way to convert to the Postscript format once and use the old psnup, but I think that it can be troublesome due to multibyte characters, fonts, and format conversion, so handle PDF. I decided to write a conversion script in a recent scripting language where a library is likely to exist.

specification

After a little research, it seems that there is a library called PyPDF2 for Python, so I decided to implement it with Python. As a specification, I decided to make it possible to select the main things that can be changed on the layout tab of the print menu of macOS as command line options.

--Allows you to select the number of pages to be combined into one page. --Allows you to choose several directions for arranging pages. --As for page size, simply keep the original page size. (For example, if you upload an A4 size file by 2up, an A3 size PDF file will be output.) --For page rotation, + -90 degrees and 180 degrees can be selected. Also, when the vertical page and the horizontal page are mixed, the operation to align them can be selected. --When pages of different sizes are mixed, you can now select how to align the sizes and page positions. --Allows the meta information of PDF files to be inherited from the input file.

Implementation

The file storage location is below.

Execution example

An example of grouping pages in 2 rows and 3 columns.

% ./pdf_merge_multipages.py -o merge_test.pdf -c 3 -l 2 test_samples/input_landsc
ape_1.pdf test_samples/input_portleit_1.pdf

Detailed usage etc.

Operating requirements

--Python: Confirmed operation with 2.7

How to use

usage: pdf_merge_multipages.py [-h] [-output filename] [-columns n_h] [-lines n_v] [-page-order opt]
                               [-rotation opt] [-valign opt] [-align opt] [-metainfo opt] [-title text]
                               input-file [input-file ...]

Merge multiple mages in PDF files w/o gap.

positional arguments:
  input-file        Input PDF file(s)

optional arguments:
  -h, --help        show this help message and exit
  -output filename  Output file
  -columns n_h      # of columns of merged pages (default = 2)
  -lines n_v        # of lines of merged pages (default = 1)
  -page-order opt   Page order (choices=left2right[default], left2bottom, right2left, right2bottom)
  -rotation opt     Page orientation (choices=none[default], flip, right, left, auto, rauto)
  -valign opt       Page fitting (choices=resize, none[default], top, bottom, center, fit)
  -align opt        Page fitting (choices=resize, none[default], right, left, center, fit)
  -metainfo opt     Meta data for marged file (choices=full[default], none, partial, short)
  -title text       set title in meta data for marged file (Default: output file name)

Optional arguments

---output filename: The name of the output file. If not specified, the default is ʻa.out.pdf ---columns nh, -lines n_v: Layout specification: Place the page of the input PDF file in horizontal n_h columns x vertical n_v rows. If not specified, the default is 2x1 ("2up") ---page-order option: Specify the order in which the pages are laid out. --left2right: Place the first page of the input file in the upper left. The pages following the input file are arranged sequentially to the right, and when they reach the right end, they are arranged sequentially from the left end of the column directly below or from the upper left to the right of the next page. (Default) --left2bottom: Place the first page of the input file in the upper left. Arrange the pages following the input file in order from the bottom, and when you reach the bottom, place them from the top of the column to the right or from the top left to the bottom of the next page. --left2top: Place the first page of the input file at the bottom left. Arrange the pages following the input file in order, and when you reach the top, place them from the bottom of the column to the right or from the bottom left to the top of the next page. --right2left: Place the first page of the input file in the upper right. The pages following the input file are arranged sequentially on the left, and when they reach the left end, they are arranged sequentially from the right end of the column directly below or from the upper right to the left of the next page. --right2bottom: Place the first page of the input file in the upper right. Arrange the pages following the input file in order from the bottom, and when you reach the bottom, place them from the top of the column to the left or from the top right to the bottom of the next page. --right2top: Place the first page of the input file at the bottom right. Arrange the pages following the input file in order, and when you reach the top, place them from the bottom of the column to the left or from the bottom right to the top of the next page. ---rotation option: Page rotation --none: Do not rotate each page of the input file to be placed. (Default) --flip: Flip each page of the input file to be placed upside down. --right: Rotate each page of the placed input file 90 degrees to the right. --left: Rotate each page of the placed input file 90 degrees to the left. --ʻAuto: If each page of the input file placed at the top of the output page is placed vertically (horizontally), each page of the other input file placed on the same page of the output file is placed horizontally (vertically). ), Rotate it 90 degrees to the left, otherwise it will not rotate. -- rauto: If the page of the input file placed at the top of the output page is placed vertically (horizontally), each page of the other input file placed on the same page of the output file is placed horizontally (vertically). In the case of, rotate it 90 degrees to the right, and if not, place it without rotating it. ---valign opttion: Specify vertical position adjustment of the page to be placed --none, bottom: Bottom alignment (default) --center: Vertical center alignment --top: Top alignment --resize: Reduces to fit the vertical size of the page of the input file placed at the top of the output page. If specified at the same time as -algin resize or -algin fit, the reduction ratio may be smaller, but the layout interval is still the input file placed at the top of the output page. The vertical size of the page. --fit: Shrinks to fit the vertical size of the page of the input file placed at the top of the output page. If specified at the same time as -algin resize or -algin fit, the reduction ratio will be smaller and the layout interval will be smaller than the vertical size of the page of the input file placed at the top of the output page. Sometimes.

---align opttion: Specifying the horizontal position adjustment of the page to be placed --none, left: Right justified (default) --center: Left and right center alignment -- right: Right justified --resize: Reduces to fit the horizontal size of the page of the input file placed at the top of the output page. If specified at the same time as -valgin resize or -valgin fit, the reduction ratio may be smaller, but even then the layout interval is for the input file placed at the top of the output page. It is the horizontal size of the page. --fit: Shrink to fit the horizontal size of the page of the input file placed at the top of the output page. When specified at the same time as -valgin resize or -valgin fit, the reduction ratio is smaller and the layout interval is smaller than the horizontal size of the input file page placed at the top of the output page. Sometimes. ---metainfo options: Specifying meta information in the output file --full: Generated by adding the meta information of the input file to the combined one. (Default) --partial: Generated by adding the meta information of the input file to a part of the combined one. --Only short:'/ Title','/ Creater','/ Producer' are generated --none: Do not generate meta information for the output file. (PyPDF2 default value is specified.) ---title text: Specify the title of the meta information of the input file. (Default is output file name)

Recommended Posts

A script that combines multiple pages of a PDF file into one page without margins
Python that merges a lot of excel into one excel
A script that combines margins when pasting a number of graphs on tiles with gnuplot
A script that combines your favorite python modules and binaries into one Lambda Layer
Combine multiple python files into one python file
[Python, PyPDF2] A script that divides a spread PDF into two left and right
[Python & Unix] Combine multiple PDF files into one.
Create a PDF file with a random page size
Convert multiple jpg files to one PDF file
A memo that implements the job of loading a GCS file into BigQuery in Python
Combine multiple csv files into one csv file with python (assuming only one line of header)
A script that takes a snapshot of an EBS volume
A shell script that puts Webmin into Alpine Linux
A script that outputs a list of SoftLayer portal users