Merge / convert multiple PDF files into one PDF [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

Closed 3 years ago . The community reviewed whether to reopen this question last year and left it closed:
Original close reason(s) were not resolved

How could I merge / convert multiple PDF files into one large PDF file? I tried the following, but the content of the target file was not as expected:

convert file1.pdf file2.pdf merged.pdf 

I need a very simple/basic command line (CLI) solution. Best would be if I could pipe the output of the merge / convert straight into pdf2ps ( as originally attempted in my previously asked question here: Linux piping ( convert -> pdf2ps -> lp) ).

1 1 1 silver badge asked Mar 24, 2010 at 12:56 23.6k 4 4 gold badges 24 24 silver badges 21 21 bronze badges

ymmv, but this doesn't seem to have as good of a resolution in the output file as pdfunite and it also results in a file size larger than the output from pdfunite

Commented Nov 17, 2015 at 23:02 Commented Feb 11, 2017 at 22:06

Whenever links are preserved or not by those solutions is discussed in this post. If you want to preserve the links (probably along with other annotations), use pdftk if want a command-line interface, pdfsam if you want graphical user interface, sejda if you want a web interface.

Commented Mar 5, 2020 at 3:02

The convert command line is from ImageMagick and it converts the PDF to an image before doing whatever else it will be doing.

Commented Dec 23, 2021 at 1:15 pdftk PDF1.pdf PDF2.pdf cat output PDF3.pdf works fine Commented Sep 16, 2022 at 21:14

23 Answers 23

Considering that pdfunite is part of poppler it has a higher chance to be installed, usage is also simpler than pdftk :

⚠ IMPORTANT: Just make sure you remember to provide out.pdf , or else it will overwrite the last input file in your command ⚠

pdfunite in-1.pdf in-2.pdf in-n.pdf out.pdf 

A safer solution may include a test of non-existence
targeting the output file

export output_file=out.pdf && \ ! test -e $output_file && \ pdfunite in-1.pdf in-2.pdf in-n.pdf $output_file 
17.1k 11 11 gold badges 70 70 silver badges 94 94 bronze badges answered Jul 1, 2012 at 7:11 Hubert Kario Hubert Kario 22.4k 4 4 gold badges 27 27 silver badges 45 45 bronze badges It is fast, but it seems to break hyperlinks. See blog.dbrgn.ch/2013/8/14/merge-multiple-pdfs Commented Aug 14, 2013 at 9:46

Just make sure you remember to provide out.pdf, or else it will overwrite the last file in your command, sigh.

Commented Oct 19, 2013 at 22:20 package for pdfunite is poppler-utils in debian but may not be present in old debian releases. Commented Nov 10, 2013 at 12:16

Cannot recommend this. The size of the the resulting PDF is far too big. For example: Pdfunite gives me a 75MB file while Ghostscript packs everything into 1MB.

Commented Dec 6, 2013 at 11:58

You can use: pdfunite *.pdf out.pdf assuming no other pdf exists in that directory and their order is preserved by "*". If its not preserved, using ranges: filename_<0..9>.pdf solves it.

Commented Jan 5, 2015 at 5:48
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf mine1.pdf mine2.pdf 

or even this way for an improved version for low resolution PDFs (thanks to Adriano for pointing this out):

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dPDFSETTINGS=/prepress -sOutputFile=merged.pdf mine1.pdf mine2.pdf 

In both cases the ouput resolution is much higher and better than this way using convert:

convert -density 300x300 -quality 100 mine1.pdf mine2.pdf merged.pdf 

In this way you wouldn't need to install anything else, just work with what you already have installed in your system (at least both come by default in my box).

UPDATE #1: first of all thanks for all your nice comments!! just a tip that may work for you guys, after googleing, I found a superb trick to shrink the size of PDFs, I reduced with it one PDF of 300 MB to just 15 MB with an acceptable resolution! and all of this with the good ghostscript, here it is:

gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/default -dNOPAUSE -dQUIET -dBATCH -dDetectDuplicateImages -dCompressFonts=true -r150 -sOutputFile=output.pdf input.pdf 

UPDATE #2: In case you need to "burn" edits and compress a PDF made with Acrobat, this would help:

gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=compressed.pdf withedits.pdf