66

When I try to merge two .pdf files using Imagemagick

convert pdf1.pdf pdf2.pdf temp.pdf

the resulting temp.pdf file seems to have very low resolution. How can I keep the resolution same as in the source files?

6 Answers 6

72

Barns's right, but if pdftk didn't work try ghostscript.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=temp.pdf pdf1.pdf pdf2.pdf
5
  • Worked perfectly, and speedily too - much faster than using ImageMagick with the -density flag.
    – Brionius
    Nov 9, 2014 at 22:59
  • 1
    This should be the correct answer. Not that ImageMagick doesn't work; it works too. But as @Brionius noted, gs is much faster and the quality of the resulting pdf is top notch. Jun 15, 2015 at 22:57
  • 1
    Note that you could use *.pdf instead of the list of pdf1.pdf and pdf2.pdf at the end of this command to convert all pdf in a folder to a single pdf.
    – Colin D
    Nov 29, 2017 at 18:28
  • I had some problems with the font, some pages lose their font. Is there a way I can choose it? Feb 26, 2019 at 14:02
  • Note that this command can be written in an easier way (since the -o option is made for exactly this): gs -q -sDEVICE=pdfwrite -o temp.pdf pdf1.pdf pdf2.pdf
    – fynsta
    Jan 31 at 10:14
31

If every file you want to merge is in the same folder:

convert -density 150 $(ls -rt *pdf) output.pdf

the argument -density 150 keeps the quality of the merged PDFs.

3
  • 9
    A side note on your all PDF syntax, you can omit the sub-ls command: convert -density 150 *.pdf output.pdf
    – Josh
    Jul 11, 2014 at 19:47
  • '*.pdf' and '$(ls -rt *pdf)' may result in different output.
    – shantanoo
    Aug 31, 2017 at 14:48
  • 2
    Please don't ever use ls like that. (1) If some file names have spaces or special characters, the command might fail or, worse, cause data corruption. (2) ls doesn't have a POSIX standard, and is therefore not guaranteed to be portable. (2) You need to quote the results, but you can't do so because of the subprocess. Instead, to find files, use the find command or, better, use the shell as @Josh explained. Mar 20, 2021 at 17:13
26

Imagemagick's convert command is normally used for converting image files from one format to another, and in this case, it is possible that it is actually performing an internal conversion of sorts before outputting the two "images" (PDFs) into a single file.

I would suggest you consider using the PDF Toolkit (pdftk) instead http://www.accesspdf.com/pdftk/

From the examples on the website, this should be as simple as:

pdftk pdf1.pdf pdf2.pdf cat output temp.pdf
6
  • I'm running on OS X 10.6.1 and I tried to install pdftk via Macports. It seems that pdftk is deprecated on Mac, the installation would not finish. So, I tried to do this using Imagemagick.
    – jraja
    Sep 27, 2009 at 13:41
  • On Ubuntu pdftk was no problem to install from apt, and it did the trick gloriously. I've used ImageMagick to stitch together PDFs before with great success, but because it rasterizes PDFs first (understandably--it doesn't work with vectors) it doesn't look so good for most text documents. But pdftk worked great--thanks for the suggestion.
    – Iguananaut
    Mar 3, 2013 at 0:40
  • Great ... thank you
    – Dan Ortega
    Jun 11, 2019 at 20:26
  • Let's upvote this one, pdftk works way better than ghostcript doing this.
    – Dan Ortega
    Jun 11, 2019 at 20:32
  • had to do a snap install, and the quality was very good. Nov 16, 2020 at 15:33
11

I always forget how to do this and find this question first when I search.

convert -density 600 file1.pdf file2.pdf -resize 50% new.pdf

The linked example has the density at 144, however, that has never been high enough to not appear pixelated.

http://web.archive.org/web/20130311071316/http://studio.imagemagick.org/pipermail/magick-users/2009-September/022958.html

1
  • Sorry, the link is dead now.
    – Sablefoste
    Apr 24, 2015 at 16:00
9

If running linux you can also try poppler which provides pdfunite which concatenates without manipulating the resolution.

1
1

I couldn't find any way of joining two pdf files together while keeping the resolution good and the text intact, but I figured out a way to convert it into a high resolution png file.

pdftoppm -f 1 -l 1 -aa yes -aaVector yes -png -r 300 page.pdf > tmp1.png
pdftoppm -f 2 -l 2 -aa yes -aaVector yes -png -r 300 page.pdf > tmp2.png
convert tmp1.png tmp2.png +append -quality 100 page.png

This takes to first two pages of page.pdf and joins them into a side by side high resolution png file.

Changing the last line to

convert tmp1.png tmp2.png +append -quality 100 page.pdf

will result in a pdf document output as I later figured out after messing around with pngtopnm, pnmtops, ps2pdf.

1
  • 2
    -1 for suggesting to rasterize a (possible) vector image. That's not the way to go.
    – Marco
    May 10, 2012 at 11:59

You must log in to answer this question.