How to put multiple PDFs on one page (e.g., four on one / 4 on 1) with annotations preserved?

Description works for:

Ubuntu          21.04
pdfjam          3.03
GPL Ghostscript 9.53.3 (2020-10-01)

I created this question after having found the solution -- to make it easier for others (since some commands I found were outdated etc.)

Key to finding the solution is knowing that 'preserving' the annotations is called 'flattening', which essentially puts the annotation layer into the 'standard' one(s).

Thus, we have three steps:

  1. flattening: pdf2ps -q -sOutputFile=- input.pdf | ps2pdf - out-flat.pdf
  2. merging 4on1: pdfjam --nup 2x2 out-flat.pdf --outfile out-4on1.pdf --landscape

For me, step 1 produced a PDF that was significantly larger than the original, so an alternative third step might a compression:

  1. compressing: gs -sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -q -o out-4on1-compressed.pdf out-4on1.pdf

Note that for compression there are multiple compression rates that you could choose, the one chosen here (ebook) is the one best for my purposes. More details below.

Since I will have to do this on a regular basis, I created a convenient script for that. Hope it's helpful to others as well!

#!/bin/sh

# argument 1: input filename
# argument 2: compression rate (optional)

# remove file ending to be able chaning the filename
filename=$(basename -- "$1")
extension="${filename##*.}"
prefix="${filename%.*}"

# compile the annotations inte the main layer(s)
pdf2ps -q -sOutputFile=- $1 | ps2pdf - $prefix-flattened.pdf

# put 4 slides into one per page, landscape mode
pdfjam --nup 2x2 $prefix-flattened.pdf --outfile $prefix-4on1-largeFile.pdf --landscape

# compress
gs -sDEVICE=pdfwrite -dPDFSETTINGS=/${2:-ebook} -q -o $prefix-4on1.pdf $prefix-4on1-largeFile.pdf

# delete intermediate files
rm ./$prefix-flattened.pdf
rm ./$prefix-4on1-largeFile.pdf

Don't forget to make the script file executable (e.g. via chmod +x 4on1-script.sh). The script deletes all the intermediate files. Also it creates a useful filename, for which 'identifying' a file's prefix was required. Its first argument is the input filename. Its second argument is optional and determines compression quality. Default value is ebook, other values are:

  • screen: selects low-resolution output similar to the Acrobat Distiller "Screen Optimized" setting.
  • ebook: selects medium-resolution output similar to the Acrobat Distiller "eBook" setting. (chosen here)
  • printer selects output similar to the Acrobat Distiller "Print Optimized" setting.
  • prepress selects output similar to Acrobat Distiller "Prepress Optimized" setting.
  • default selects output intended to be useful across a wide variety of uses, possibly at the expense of a larger output file.

If you like this solution consider also liking the ones I based this one upon:

  • Specify optional argument in linux shell.
  • Identify file name's prefix.
  • How to compile annotations into PDF?
  • Where are compression qualities documented?