While making handouts for my lecture, I found myself needing to convert a PDF file to an n-up layout, specifically an arrangement of 2×2 original pages per output page. There is in fact a nice shell script pdfnup/pdfjam
that comes with TeX-Live
, but I also made my own script to do the job, based on the LaTeX
package pdfpages
. My version (pdfnup2
) is more limited, but it has some complementary features. In particular, if you have several PDF
files that each have their own different dimensions, my script maintains these dimensions individually for each file. On the other hand, pdfjam
allows you to specify the option --fitpaper true
after giving a list of files, but this just chooses one single page size to best fit all dimensions, instead of allowing each page to have its own size.
The latest version of the script was motivated by this thread on the Mac-TeX mailing liet.
The script shown below takes any PDF file and creates a new file with these properties:
n
rows and m
columns
pdfnup2 -n 2x2 -d -30x-30 file.pdf
The output file (default is ~/Desktop/converted.pdf
) will have a 2×2 page layout. Here the rows and columns are moved closer together by making the offset -30x-30
negative in the x and y directions.
pdfnup2 -o new.pdf file1.pdf file2.pdf
The output file new.pdf
is the concatenation of both input files. However, the paper size of each file is unchanged, so that pages of different size can be contained in the output file.
#!/bin/tcsh # # Save this file under any name (e.g., pdfnup2) and make it executable in your PATH. # Usage instructions are displayed when invoking the command without arguments. # # Created Oct 11, 2007 by J.U.Noeckel (originally called pdfbgcolor, only used to change background color) # Modified March 30, 2009 by J.U.Noeckel (added n-up functionality because no such tool seemed to exist for Leopard) # Modified January,14 2012 by J.U.Noeckel (added ability to provide multiple input files and preserve their page dimensions) alias printHelp 'printf "Usage: "`basename $0`" [-o outputfile] [-c HTML-color] [-n ROWSxCOLUMNS] [-d DELTA_XxDELTA_Y] filename(s)\n\n";\\ printf "outputfile:\n"; \\ printf "name of the output PDF file (default: converted.pdf)\n"; \\ printf "HTML-color:\n"; \\ printf "hexadecimal RGB number in capitals, RRGGBB\n"; \\ printf "The background of the file filename is changed to color HTML-color\n"; \\ printf "Default color: white\n\n";\\ printf "ROWSxCOLUMNS:\n"; \\ printf "two integers separated by x specifying the number of rows and columns to\nprint per physical page (e.g., 2x2)\n\n"; \\ printf "DELTA_XxDELTA_Y:\n"; \\ printf "two floats separated by x sepcifying the offset in points between rows and\ncolumns (may be negative)\n"' alias cleanup 'rm -Rf $TMPNAME*' if ("$#" == 0) then echo "**** Input file name required" printHelp exit 1 endif set TMPNAME = `mktemp -dt pdfnup` echo $TMPNAME # Default background color is white: set COLOR = "FFFFFF" set NUP = "," set DELTA = "" set OUTFILE = ~/Desktop/converted.pdf # Define the permissible command-line options: set ARGS = `getopt hd:n:c:o: $*` # Arguments preceding the special "--" are command-line options: while ("$ARGS[1]" != "--") switch ("$ARGS[1]") # Check if a color option is given: case "-c": set COLOR = $ARGS[2] shift ARGS breaksw # Output file name: case "-o": set OUTFILE = "$ARGS[2]" shift ARGS breaksw # Check if an n-up option is given: case "-n": set NUP = ",nup="$ARGS[2] shift ARGS breaksw # Check if an offset option is given: case "-d": set DELTA = ",delta="$ARGS[2]:s/x/ / shift ARGS breaksw default: printHelp exit 0 breaksw endsw shift ARGS end if (-f $OUTFILE) then echo -n "Output file "$OUTFILE" exists. Overwrite ? [yes/no]" set overwrite = "$<" if ( $overwrite != "yes" ) then echo "NOT OVERWRITING" cleanup exit 1 endif endif echo '\documentclass{minimal}\usepackage{xcolor}\pagecolor[HTML]{'$COLOR'}\usepackage{pdfpages}\begin{document}' > $TMPNAME/converted.tex shift ARGS set FILECOUNTER = 0 foreach i (`echo "$ARGS"`) if (-f $i) then @ FILECOUNTER++ cp "$i" "$TMPNAME"/$FILECOUNTER.pdf else echo "**** Invalid filename $i" printHelp cleanup exit 1 endif echo '\includepdf[fitpaper,pages=-'$NUP$DELTA',frame=false]{'$FILECOUNTER.pdf'}' >> $TMPNAME/converted.tex end echo '\end{document}' >> $TMPNAME/converted.tex pushd $TMPNAME pdflatex -interaction nonstopmode converted.tex # Return to original working directory: popd if ( -f $TMPNAME/converted.pdf) then mv $TMPNAME/converted.pdf $OUTFILE echo "Output written to "$OUTFILE else echo "**** Couldn't convert" endif cleanup