Wednesday, March 4, 2015

How to merge or split pdf files using convert

convert is a member of the ImageMagick software suite for image manipulation. Two of my earlier posts dealt with using convert to slice and resize an image. It is a lesser-known fact that convert also works with pdf files. I'd previously explained how to merge and split up pdf files using tools such as pdftk and gs. In this post, I'll illustrate how to do the same using the convert program.

First, you need to install convert which is packaged in the ImageMagick suite.

$ sudo apt-get install imagemagick

Merging 2 pdf files (file1 and file2) into a new file (output) is as simple as executing:

$ convert file1.pdf file2.pdf output.pdf

You can merge a subset of pages instead of the entire input files. To accomplish that, use the angle brackets to specify the target subset of pages. For example, to merge page 1 of file1 with pages 1, 2 and 4 of file2, run the following command:

$ convert file1.pdf[0] file2.pdf[0-1,3] output.pdf

Note that page numbers are zero-based. Therefore, [0] is page 1, and [0-1] are the pages ranging from page 1 to page 2.

Finally, the following example splits up input into 2 files: first2output and next2output. The former output file contains pages 1 and 2 from the original file; the latter, pages 3 and 4.

$ convert input.pdf[0-1] first2output.pdf
$ convert input.pdf[2-3] next2output.pdf

As you can see, convert is a versatile utility program to manipulate both image and pdf files.

5 comments:

Unknown said...

Hi,
Thanks for sharing very useful information on Linux

Unknown said...

SystoTech PDF Split and Merge is another good solution available free for combining two or more PDF files or breaking a large PDF file into parts. Read more details: http://www.systotech.com/pdf-split-and-merge.aspx

Unknown said...

It worked fine but the quality of the PDF Image is AWFUL!

It might be suitable for PDF with loads of text!

Anonymous said...

I had the same problem.

I had much better results with pdfseparate, which is part of the poppler project http://poppler.freedesktop.org

From the docs:

pdfseparate sample.pdf sample-%d.pdf

extracts all pages from sample.pdf, if i.e. sample.pdf has 3 pages, it produces

sample-1.pdf, sample-2.pdf, sample-3.pdf

Or, to select a single page (in this case, the first page) from the file sample.pdf:

pdfseparate -f 1 -l 1 sample.pdf sample-1.pdf

Unknown said...

This is a terrible solution. All your text and nice vector data becomes raster pictures with pretty low resolution. And the document now has even more size than before splitting!