Imagemagick combine 2 generated PDFs into 1 multi-page file
Asked Answered
M

2

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");

Meletius answered 6/2, 2013 at 21:14 Comment(4)
You can use pdftk to do this much simpler - are you limited to imagemagick?Monostylous
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.Meletius
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 first using ghostscript directly for best results.Monostylous
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.Meletius
H
10

Try:

convert page1 page2 output.pdf

You posted just before me :(

I think you will have to add a density.

Hamburg answered 6/2, 2013 at 21:38 Comment(4)
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.Meletius
Imagemagick uses raster and I suppose it converts from pdf to raster and back to pdf.Hamburg
density worked. i kept it at 300 but it did make a 9mb and 7mb file become 35mb... weird!Meletius
The reason for the bloat is likely because ImageMagick is rendering the pdf as a raster image. Using ghostscript directly avoids this problem...Monostylous
M
22

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
Monostylous answered 6/2, 2013 at 21:42 Comment(2)
Worked with me (old version for Debian, Dec 2008), and preserved resolution and color (as far as I can tell).Scheller
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.Antipus
H
10

Try:

convert page1 page2 output.pdf

You posted just before me :(

I think you will have to add a density.

Hamburg answered 6/2, 2013 at 21:38 Comment(4)
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.Meletius
Imagemagick uses raster and I suppose it converts from pdf to raster and back to pdf.Hamburg
density worked. i kept it at 300 but it did make a 9mb and 7mb file become 35mb... weird!Meletius
The reason for the bloat is likely because ImageMagick is rendering the pdf as a raster image. Using ghostscript directly avoids this problem...Monostylous

© 2022 - 2024 — McMap. All rights reserved.