Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multi-page PDFs into one PDF with ImageMagick

I am trying to use ImageMagick (6.8.0) to combine several multi-page PDFs into a single PDF. This command:

$ convert multi-page-1.pdf multi-page-2.pdf merged.pdf

Returns merged.pdf, which contains the first page of multi-page-1.pdf and the first page of multi-page-2.pdf.

This command:

$ convert multi-page-1.pdf[2] multi-page-2.pdf[2] merged.pdf

Returns merged.pdf, which contains the third page of multi-page-1.pdf and the third page of multi-page--2.pdf.

I would like to merged.pdf to contain all of the pages of each multi-page pdf. I have so far not found a way of telling the convert command to use a range of pages, although I have tried adding [0-1] and [0,1] at the end of the filenames.

Interestingly, this ghostscript command (which I found via StackOverflow but cannot re-find) does work as I would like it to:

$ gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=merged.pdf multi-page-1.pdf multi-page-2.pdf 

The problem is, the ImageMagick 'convert' command takes urls as inputs and ghostscript does not, and I need my program to take url input rather than file paths.

Is it possible to get the result of the above ghostscript command using ImageMagick?

like image 313
Jessie A. Young Avatar asked Jun 19 '13 23:06

Jessie A. Young


1 Answers

Why don't you use pdfunite?

Example:

$ pdfunite 1.pdf 2.pdf 3.pdf merged.pdf
like image 140
Rosamunda Avatar answered Sep 23 '22 17:09

Rosamunda