(Work in progress, but usable and tested on Linux)
I was having fun when merging around 5000 PDFs (4957 to 2018-01-18, to be exact; the UVA ARENA collection, https://github.com/dipu-bd/UVA-Arena). This is my first attempt (tested on Linux). I was thinking to add a simple Table of Contents (let's say with the name of the files), for now it's just a plain and fast merge with sort -g
added.
Let's say we have those files in the pdfs
folder, then we run bash mal-combine-pdfs.sh pdfs
. It generates a TeX file (taking advantage of the pdfpages
package) and processes it with lualatex/pdflatex
.
I was wondering if we could process the TeX code directly without intermediate TeX file, we can, taken from lualatex --help
:
or: luatex --lua=FILE [OPTION]... \FIRST-LINE
At the moment, I'm not 100% sure if I could publicly show you the result of my efforts (I am writing an email to the author to be sure), for now, this is it.
I am removing spaces and special (TeX) characters in the filenames manually, see How to include graphics with spaces in their path? for more details how to handle that. For non-TeX way, please see Making one PDF file from multiple PDFs or Tex?.
#!/bin/bash
# mal-combine-pdfs.sh
# mal, version 1, 2018-01-18
# Concatenate PDFs into one PDF file
# Usage:
# chmod +x mal-combine-pdfs.sh
# ./mal-combine.pdfs.sh pdfs
# or the actual directory:
# ./mal-combine.pdfs
# The disadvantage:
# It is not working when filenames contain spaces and some special characters.
# Untested when filenames contain diacritical letters.
IFS=$'\n' # more lines to be added to the variable, bash thing
ulimit -n 10000 # increase the limit in the linux environment
tempcore=malarticle # temporary file, the core name
temp=$tempcore.tex # dtto , the TeX file
rm -f $tempcore.pdf # delete our previous attempt, if any, just in case
# rm -f texput.log # delete a log file, if previous attempt failed
malcode="\\documentclass[a4paper]{article}
\\usepackage{pdfpages}
\\begin{document}
"
malcounter=0 # number of PDF files to be concatenated
maldirectory=$1 # e.g. "pdfs", by default it is the actual directory, "."
echo "Processing PDFs from the \"$1\" directory..." # a message to the terminal
# The core, find all the PDFs
for thepdf in `find $maldirectory -type f -iname \*.pdf | sort -g`; do
let malcounter=malcounter+1
echo "Adding $thepdf..."
malcode+="\\includepdf[pages={-}]{$thepdf}
"
# A portion of files, if needed for testing
# To be uncommented (1 line)
#if [ $malcounter -eq 10 ]; then break; fi
done
malcode+="\\end{document}"
echo "No. of PDFs: $malcounter..." # a message to the the terminal
sleep 2 # Relax for a while
echo "The TeX code to be processed is:"
echo $malcode # show the TeX code to the terminal
echo $malcode >$temp # save variable to the TeX file
echo "Generating the $tempcore.pdf file..."
lualatex $temp # generating PDF from within the TeX file
# pdflatex $temp # It usually works as well, but lualatex has less restrictions.
rm -f $tempcore.{aux,log} # Kid, clean the kitchen!... ;-)
#rm -f $tempcore.tex # Kid, clean the garage!... ;-)
# A chunk for TeXists, we do not need the TeX file, a direct processing
# To be uncommented (1 line)
#lualatex $malcode # generating PDF without TeX file
echo "The work is done! Have a nice day..." # A final message to the terminal.
pdfpages
is what you want.pdfpages
is great for including already exisiting PDFs into your.tex
file.