I have a project that involves converting a large batch of multi-page PDFs into single-page PDFs.
Each PDF file has anywhere from 1 to 16 pages, and the pages have varying heights. For example, I have a two-page PDF that looks like this – my goal is to stitch or impose the two pages together vertically to form a single PDF page:
What I want to happen:
Before After
┌─────┐ ┌─────┐
│ A │ │ A │
│ A │ │ A │
│ A │ -----> │ A │
└─────┘ │ │
┌─────┐ │ B │
│ B │ └─────┘
└─────┘
I set up a Bash script that loops through all of the PDF files in a directory, and for each one, it runs a pdflatex command:
pdflatex "\def\pdfpath{Example.pdf} \input{stitch_pages.tex}"
Which runs this LaTeX script:
\documentclass{article}
\usepackage{pdfpages}
\usepackage[paperwidth=12in, paperheight=200in]{geometry} % Bigger than the combined width/height of the PDF pages (will be cropped after)
\pagestyle{plain}
\begin{document}
\includepdfmerge[nup=1x16, % Assuming there are no PDFs with more than 16 pages
noautoscale=true,
delta=0 0,
frame=true]
{\pdfpath, -} % Path is passed in when running from the command line
\end{document}
As you can see above, I set a large paper width and height, to avoid the PDF pages being resized. I have excess margins around the edges being trimmed off in an additional step, using the Python tool pdfCropMargins.
Unfortunately, after the LaTeX script runs, I end up with a single-page PDF that looks like this – with a large margin between the two pages that can't be cropped away:
What actually happens:
Before After
┌─────┐ ┌─────┐
│ A │ │ A │
│ A │ │ A │
│ A │ -----> │ A │
└─────┘ │ │
┌─────┐ │ │
│ B │ │ │
└─────┘ │ B │
└─────┘
I found a similar question here – Stitch differently-sized pages together – but the proposed solution, using delta, seems to require the pages to be known, fixed sizes. In my case, I don't know how tall any of the pages are for a given PDF in advance. Is it possible to prevent margins from being added between the pages in the first place, or to dynamically calculate what the margin would be and subtract it, or to vertically align the pages to the top of their "box" when combining them (which would cause the large margin above page B to move below)?
This is my first time using pdflatex, pdfpages, and LaTeX in general, so it's possible I'm missing something simple. Non-LaTeX suggestions for how to do this are also welcome, if anyone has any expertise (LaTeX is the most promising command-line option I've found so far).
pdfpagesis stitching each one page doc on its own 1x16 grid. Try to join all the PDF in a multipage before (you can usepdftkfor that) and then just call oneincludepdfon the resulting file. – Rmano Feb 02 '21 at 07:18