65

I have a range of PDF files 1.pdf, 2.pdf, etc. that I would like to merge into one file, with all the PDFs tiled on one page.

Currently, I have tried pdftk to merge these files, but they are put on separate pages:

pdftk 1.pdf 2.pdf ... cat output merged.pdf

Is there a way to, instead, tile the individual PDF files into one master page on merged.pdf?

10 Answers10

64

I do not know if pdftk can do this, but pdfjam can. It can be installed on debian or derivates with sudo apt install texlive-extra-utils. Then you can do:

pdfjam Page1.pdf Page2.pdf --nup 2x1 --landscape --outfile Page1+2.pdf

It puts 2 pages on one page side by side. With --nup 1x2 --no-landscapeyou can put two pages on one page on top of each other.

Ole Tange
  • 5,099
10

Not sure what you mean by tiled on one page. I was looking for a way to merge multiple PDFs on one page - on top of another. This can be done with pdftk like this:

pdftk foreground.pdf background background.pdf output merged.pdf

Nik
  • 216
6

The pdfLaTeX based pdfnup might work for you. If you have lots of pdf-files you may need to make a long pipe of pdfjam or run it several times.

There is also pdfnup in python.

micke
  • 3,485
  • 19
  • 29
4

you can use montage from ImageMagick

$ montage *.pdf merged.pdf

see also http://www.imagemagick.org/script/montage.php

Cesco
  • 98
3

This script will tile the pdf pages for you. Change the slice to what you need per page.

#!/usr/bin/ruby

latexhead = <<'EOF'
\documentclass{article}
\usepackage[pdftex]{graphicx}
\usepackage[margin=0.1in]{geometry}
\usepackage{pdfpages}
\begin{document}
EOF
latextail = <<'EOF'
\end{document}
EOF

pages = %x[pdfinfo #{ARGV[0]}].split(/\n/).select{|x| x=~ /Pages:/}[0].split(/\s+/)[1].to_i
puts latexhead
s = (1..pages).each_slice(4).to_a
s.each do |a|
  puts "\\begin{figure}"
  a.each do |p|
    puts "\\includegraphics[page=#{p},scale=0.4,width=.5\\textwidth]{#{ARGV[0]}}"
  end
  puts "\\end{figure}"
end
puts latextail
anonymous
  • 31
  • 1
1

If filenames are in "system specific" order, then pdftk *.pdf cat output merged.pdf should work just fine.

Here is what I mean by "system specific" order.

Example:
I've got 3 files on my Ubuntu 11.04: 1.pdf, 2.pdf, 10.pdf
Files are merged in order: 10.pdf 1.pdf 2.pdf (ls -l returned the same order as in merged file)

Safest naming convention: 0001.pdf, 0002.pdf, etc.

szemek
  • 37
0

This link: http://www.verypdf.com/wordpress/201302/how-to-combine-4-page-pdf-into-1-page-pdf-file-to-save-ink-and-papers-34505.html shows VeryPDF PDF Stitcher can do the job vertically or sideways. Download at: http://www.verypdf.com/app/pdf-stitch/index.html

Zimba
  • 1,291
0

If you've got a large number of PDFs in one folderstructure, and you've got a TeX-Installation, this script puts all the PDFs recursivly into one large file:

    #!/bin/bash
#
# pdfdir OUTPUT_FILE
#
# produces one big PDF file of all PDF files in .
#
if [ $# -ne 1 ] || [ -z "$1" ]; then
  echo "Syntax: pdfdir OUTPUT_FILE"
  exit 1
fi
FILE="$(echo "$1"|sed -e 's/\.\(pdf\|tex\)$//')"
for F in "$FILE" "$FILE.tex" "$FILE.pdf" "$FILE.aux" "$FILE.log" ; do
  if [ -e "$F" ]; then
    echo "$F exists already."
    exit 2
  fi
done
cat >"$FILE.tex" <<EOF
\documentclass{article}%
\usepackage{pdfpages}%
\usepackage{grffile}%
\listfiles%
\begin{document}%
%\tableofcontents%
EOF
# helper functions
exist_pdf_files () {
  [ $(find -L "$1" -name \*.pdf -o -name \*.PDF -type f 2>/dev/null|wc -l) -eq 0 ] && return 1
  return 0
}
list_directories () {
  find -L "$1" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | sort
}
list_pdf_files () {
  # version with " around filenames:
  #find -L "$1" -maxdepth 1 -mindepth 1 -name \*.pdf -o -name \*.PDF -type f 2>/dev/null | sort | \
  #  sed -e 's/^/\\includepdf[pages=-]{"/; s/$/"}%/'
  # version without " around filenames:
  find -L "$1" -maxdepth 1 -mindepth 1 -name \*.pdf -o -name \*.PDF -type f 2>/dev/null | sort | \
    sed -e 's/^/\\includepdf[pages=-]{/; s/$/}%/'
}
tex_headline () {
    echo "$1" | sed -e 's/_/\\_/g'
}
# current folder (lefel 0):
list_pdf_files . >>"$FILE.tex"
# Bearbeite Ebene 1:
list_directories . | while read -r DIR1; do
  # Are there PDFs in folders below that level?
  exist_pdf_files "$DIR1" || continue
  # Yes ...
  tex_headline "\section{${DIR1##*/}}%"
  # those:
  list_pdf_files "$DIR1"
  # Level 2:
  list_directories "$DIR1" | while read -r DIR2; do
    exist_pdf_files "$DIR2" || continue
    tex_headline "\subsection{${DIR2##*/}}%"
    list_pdf_files "$DIR2"
    # Level 3:
    list_directories "$DIR2" | while read -r DIR3; do
      exist_pdf_files "$DIR3" || continue
      tex_headline "\subsubsection{${DIR3##*/}}%"
      list_pdf_files "$DIR3"
      # Level 4:
      list_directories "$DIR3" | while read -r DIR4; do
        exist_pdf_files "$DIR4" || continue
        tex_headline "\paragraph{${DIR4##*/}}%"
        list_pdf_files "$DIR4"
        # Level 5:
        list_directories "$DIR4" | while read -r DIR5; do
          exist_pdf_files "$DIR5" || continue
          tex_headline "\subparagraph{${DIR5##*/}}%"
          list_pdf_files "$DIR5"
        done
      done
    done
  done
done >>"$FILE.tex"
echo "\end{document}%" >>"$FILE.tex"
echo "Sourcecode to PDF directly [J/n]"
read -r ANSWER
case "$ANSWER" in
[JjYy]) ;;
*) exit 0 ;;
esac
pdflatex "$FILE"
[ $? -eq 0 ] && rm -f "$FILE.aux" "$FILE.log" "$FILE.tex"

I have not written that code, I've got it from a discussion here: http://www.listserv.dfn.de/cgi-bin/wa?A2=ind1201&L=tex-d-l&T=0&P=10771

It is very usefull. I've translated some German comments into English.

Regards, Alexander

Keks Dose
  • 263
  • 3
  • 12
-2
  1. Save pages wanted into a pdf doc with something like Acrobat Pro.

  2. Print document using the multiple pages to a page feature, back to a pdf document.

Multiple pages to a single page. :-)

-2

Sorry for not answering your specific question about pdftk, but since other answers exist show alternatives, I thought I'd chime in and present yet another alternative.

With a system that has poppler installed, you should have the pdfunite program. Using it is simple:

pdfunite foo.pdf bar.pdf   /tmp/output.pdf