13

I'm not finding in the documentation where to take 2 PDFs and combine them into 1 file where image1.pdf will be page 1 and image2.pdf will be page2.

Is this even possible?

I see documentation where you can pull images and do a lot of processing FROM a multi-page PDF but not on how to combine them.

**This seems to have worked but with major loss of quality. I'm sure there is a way to combine without touching PDF quality, density, degradation, etc.

exec("convert image1.pdf image2.pdf combined.pdf");

LITguy
  • 623
  • 4
  • 13
  • 39
  • 1
    You can use [pdftk](http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/) to do this much simpler - are you limited to imagemagick? – PinnyM Feb 06 '13 at 21:15
  • Unfortunately the entire website is programmed in Imagemagick and on GoDaddy servers so i'm limited yes. It needs to be where a monkey can do it which is why I have it so a user manipulates 2 images and it combines them automatically. I hoped Imagemagick could do it somehow. – LITguy Feb 06 '13 at 21:16
  • 1
    If the imagemagick on your server is able to manipulate the pdfs at all it must be using the ghostscript delegate under the hood. Try [this answer](http://superuser.com/questions/54041/how-to-merge-pdfs-using-imagemagick-resolution-problem) first using ghostscript directly for best results. – PinnyM Feb 06 '13 at 21:21
  • I believe you are right. I tried a very simple line of code which i'll paste above in my question but the resolution is awful. I'll keep looking for how to make it go image1.PDF, image2.PDF, -> combined.PDF without loss of quality. i'd like to mark your answer as correct but it's not in an answer box. i'll give you a upvote though. – LITguy Feb 06 '13 at 21:33

2 Answers2

21

If the imagemagick on your server is able to manipulate the pdfs at all it must be using the ghostscript delegate under the hood. Try the answer used here first using ghostscript directly for best results.

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=temp.pdf pdf1.pdf pdf2.pdf
Community
  • 1
  • 1
PinnyM
  • 35,165
  • 3
  • 73
  • 81
  • 1
    Worked with me (old version for Debian, Dec 2008), and preserved resolution and color (as far as I can tell). – SignalToNoise Mar 25 '14 at 15:01
  • Unlike imagemagick, gs doesn't touch the data. I combined PDFs with images, extracted with `pdfimages` and tested with `md5sum`. Also IDKW imm add some white pages in between. – Pablo Bianchi Dec 27 '22 at 21:20
10

Try:

convert page1 page2 output.pdf

You posted just before me :(

I think you will have to add a density.

Bonzo
  • 5,169
  • 1
  • 19
  • 27
  • That works but it's so blocky it must be somehow changing the quality, resolution, density or something. Searching for how to combine PDFs without loss of quality. – LITguy Feb 06 '13 at 21:41
  • 2
    Imagemagick uses raster and I suppose it converts from pdf to raster and back to pdf. – Bonzo Feb 06 '13 at 21:43
  • density worked. i kept it at 300 but it did make a 9mb and 7mb file become 35mb... weird! – LITguy Feb 06 '13 at 22:08
  • 2
    The reason for the bloat is likely because ImageMagick is rendering the pdf as a raster image. Using ghostscript directly avoids this problem... – PinnyM Mar 25 '14 at 18:24