9

I am using Dompdf to generate A5 pdf documents from a html template and Pdfnup (Part of Pdfjam) to combine them into a nice single A4 sheet, which helps saving some paper when printing :)

# Generate an a5 pdf 
php dompdf.php mytemplate.html -p 'A5' -f a5doc.pdf

# combine two copies of the generated A5 into a single A4 page
pdfnup a5doc.pdf a5doc.pdf --nup '2x1' 

This works just fine; though the second step forces me to install a huge amount of dependencies (i.e. Tex-Latex, pdftex, ecc.) and would clutter my production server. I am wondering if is there any way to combine the generated documents without actually using Pdfnup. For example, is there any way of doing this with pdftk?

Thank you in advance!

mirabilos
  • 5,123
  • 2
  • 46
  • 72
Andrea Fiore
  • 1,628
  • 2
  • 14
  • 18
  • `-nup` option of `pdfnup` should be read as `--nup`. – Laurent Grégoire Jan 07 '13 at 11:20
  • Did you realized this job? If yes, did you use the hint of Kurt Pfeifle? I am not really sure how to change the given parameters to fit your problem. If you was successfull, could you maybe share (a part of) your solution? – Johnson_145 Jun 27 '13 at 15:23

4 Answers4

10

On Debian/Ubuntu, I managed to merge 2xA5 to 1xA4 for printing, using simple commands, by:

# apt-get install ghostscript pdftk psutils 
pdftk A=A5-1.pdf B=A5-2.pdf cat A1 B1 output - \
| pdf2ps -dLanguageLevel=3 - - \
| psnup -2 -Pa5 -pa4 \
| ps2pdf -dCompatibility=1.4 - A4.pdf
Cédric Dufour
  • 414
  • 4
  • 7
  • Thanks, since Ubuntu 18.? removed pdftk i had to install it manual refer to this fine install script https://askubuntu.com/a/1046476/617935 – Stefan Höltker Apr 26 '19 at 14:07
  • 1
    It could be helpful to add east/west to rotate the input pdfs, because a5 fits perfectly twice in a4. Here is a solution one could put in .bashrc e.g.: `function pdfa5toa4() {pdftk A=$1 B=$2 cat A1east B1east output - | pdf2ps -dLanguageLevel=3 - - | psnup -2 -Pa5 -pa4 | ps2pdf -dCompatibility=1.4 - $3}` and use with pdfa5toa4 A5-1.pdf A5-2.pdf output.pdf – colidyre Jul 11 '19 at 09:39
  • 1
    Some people need to add ps2pdf (as pdf2pdf) to @colidyre’s answer to fix issues with the PDFs, so ` – mirabilos Nov 02 '21 at 23:06
3

You can do it with a combination of Ghostscript and pdftk.

Here's how: https://superuser.com/questions/191373/linux-based-tool-to-chop-pdfs-into-multiple-pages/192293#192293 .

The above linked example shows how to split pages into half. Just modify the steps accordingly, using different parameters to...

  • ...first move "left" pages to a double-sized canvas, left half;
  • ...then move "right" pages to a double-sized canvas, right half;
  • ...last, combine the pages with pdftk.

Update:

Hint: You'd want to use either of pdftk's multistamp or multibackground operations (NOT: its shuffle operation!) to get the wanted final result.

Community
  • 1
  • 1
Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • The outputs of your first and second steps are different files, correct? Which pdftk command will combine corresponding pages from separate files to the same page of the output, per step 3? – Jeff G May 31 '16 at 18:14
  • @JeffG: Please comment under the original question. Over there, the instructions are very clear IMHO, if taken together with the illustrations I made. OTOH, your current questions are not clear to me at all. Please try to ask them again, but in a clear way. – Kurt Pfeifle Jun 01 '16 at 10:55
  • 1
    My question is not about how to chop a single page into multiple pages (the other question, which yes is very clear!). Instead, I'm asking for clarification about your answer here -- i.e. how to adapt your other answer to combine multiple pages into a single page. In my question here about the "steps", I'm referring to your 3 bullets just above: (1) move left pages to double-sized canvas (2) move right pages similarly, and (3) combine pages with pdftk. In short: would you use `pdftk shuffle` even for the _merging_ the left- and right- halves? How? – Jeff G Jun 01 '16 at 15:43
0

Based on Kurt-Pfeifle's answer the code using unix like shell (I also kept the line for libreoffice):

FileBaseName="ExampleDoc_A5_Landscape"

# required packages: gs, pdftk, coreutils:mktemp

libreoffice --headless --nodefault --convert-to pdf "${FileBaseName}.odt"

temp_pdf_dir=$(mktemp -d)
a4_page1="${temp_pdf_dir}/1.pdf"
a4_page2="${temp_pdf_dir}/2.pdf"

pdftk "${FileBaseName}.pdf" cat 1south output - | gs -o "${a4_page1}" -sDEVICE=pdfwrite -sPAPERSIZE=a4 -dFIXEDMEDIA -dPDFFitPage -
pdftk "${a4_page1}" cat 1north output "${a4_page2}"
pdftk "${a4_page1}" background "${a4_page2}" output "${FileBaseName}-A4.pdf"
rm -rf "${temp_pdf_dir}"

Please note that fonts embedded in the original document will be doubled in the final PDF.

This procedure generates a mirrored alignment therefore the printed A4 paper can be cut in the middle and both A5 pages will have this cut edge at their bottom.

Geck0
  • 61
  • 1
  • 5
0

Don't over engineer this problem. When printing A5, just scale the print to 141%, which make Your pages match A4. Then print two pages on one, and You are done. Pring 2xA5 on A4

Kjeld Flarup
  • 1,471
  • 10
  • 15