# Concatenate PDFs but extend pdf's to be even number of pages

I want to concatenate a bunch of PDFs but for printing purposes I would prefer that empty pages are added to each document that have an odd number of pages. Can I do this with PDFTK?

• Can you use LaTeX? – terdon Mar 29 '16 at 17:07
• Sure I can @terdon – adamse Mar 29 '16 at 17:38

Here's a simple little script that will iterate over all pdf files in the current directory and concatenate them into a single PDF, using LaTeX. PDFs with an odd number of pages will have an extra blank page added after them:

#!/bin/bash

cat<<EoF > all.tex
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
EoF

## rename the PDFs to something safe
c=0;
for f in *pdf
do
## Link the PDF with a safe name
ln -s "$f" "$c".pdf
## Include the PDF in the tex file
printf '\includepdf[pages=-]{%s.pdf}\n' "$c" >> all.tex; ## Get the number of pages pages=$(pdfinfo "$c".pdf | grep -oP '^Pages:\s*\K\d+') ## Add an empty page if they are odd [[$(expr "$pages" % 2) != 0 ]] && printf '%s\n' "\newpage\null" >> all.tex ((c++)); done printf '\\end{document}' >> all.tex; pdflatex all.tex  Since this is LaTeX, you can do all sorts of extra stuff. For example, you can have each PDF in its own section, with a clickable table of contents: #!/bin/bash cat<<EoF > all.tex \documentclass{article} \usepackage{pdfpages} \usepackage[colorlinks=true,linkcolor=blue,linktoc=page]{hyperref} \begin{document} \tableofcontents \newpage EoF ## rename the PDFs to something safe c=0; for f in *pdf do ## Link the PDF with a safe name ln -s "$f" "$c".pdf ## Include the PDF in the tex file cat<<EoF >> all.tex \section{${f//.pdf}}
\includepdf[pages=-]{$c.pdf} EoF ## Get the number of pages pages=$(pdfinfo "$c".pdf | grep -oP '^Pages:\s*\K\d+') ## This time, because each PDF has its own section title ## we want to add a blank page to the even numbered ones [[$(expr "$pages" % 2) = 0 ]] && printf '%s\n' "\newpage\null\newpage" >> all.tex ((c++)); done printf '\\end{document}' >> all.tex; ## Need to run it twice to build the ToC pdflatex all.tex; pdflatex all.tex;  • may be \cleardoublepage ? – JJoao Mar 31 '17 at 10:13 Certainly. Just create a blank page with (for example) echo "" | ps2pdf -sPAPERSIZE=a4 - blank.pdf  and add blank.pdf to every document that has an odd number of pages. E.g. pdftk \ BLANK=blank.pdf \ A=foo1.pdf \ B=foo2.pdf \ C=foo3.pdf \ cat A BLANK B BLANK C \ output bar.pdf  • Thanks, but I'm looking for a more automated way since I have many PDFs. – adamse Mar 29 '16 at 14:45 • Ah, An automated way should also be possible, you'd have to ask pdfinfo for the size of the PDFs and proceed accordingly. You should make clear in your question that you are looking for an automated method, and also perhaps include an example of what you have in mind. – Faheem Mitha Mar 29 '16 at 15:21 • Interesting! I'll try to make something. – adamse Mar 29 '16 at 15:22 For future wanderers like myself. I wrote a script combining the answer from @Faheem Mitha and the answer here #!/bin/sh # USAGE: ./concat-pdf.sh *.pdf output-file.pdf ALL_FILES="" BLANK_FILE="/tmp/blank.pdf" for OUTPUT_FILE; do true; done if test -f "$OUTPUT_FILE"; then
echo "$OUTPUT_FILE already exists" exit 1 fi echo "" | ps2pdf -sPAPERSIZE=a4 -$BLANK_FILE
for PDF_FILE in $* ;do if [$PDF_FILE != "$OUTPUT_FILE" ]; then pages=$(strings < $PDF_FILE | sed -n 's|.*Count -\{0,1\}$$[0-9]\{1,\}$$.*|\1|p' | sort -rn | head -n 1) ALL_FILES="$ALL_FILES $PDF_FILE" [$((pages%2)) -ne 0 ] && ALL_FILES="$ALL_FILES$BLANK_FILE"
fi
done

echo "pdftk $ALL_FILES cat output output.pdf" pdftk$ALL_FILES cat output output.pdf

rm -f \$BLANK_FILE