Skip to content

Instantly share code, notes, and snippets.

@josue
Last active December 10, 2023 15:11
Show Gist options
  • Save josue/dc6a60d9a8054f94b7f6 to your computer and use it in GitHub Desktop.
Save josue/dc6a60d9a8054f94b7f6 to your computer and use it in GitHub Desktop.
Using ImageMagick to easily: Split, Merge, Remove a page from PDF.

Install Required libraries:

sudo apt-get update && sudo apt-get install imagemagick gs

create directory to test commands

mkdir -p pdf_conversion/{merged,split}
cd pdf_conversion

download sample multi-page pdf to test with

curl -o LargeSample.pdf "http://cl.ly/0o1T453E153m/download/LargeSample.pdf"

Split a pdf into separate images

convert LargeSample.pdf split/page-%0d.jpg

Merge separate images into one full pdf.

convert split/*.jpg merged/full.pdf

Remove a page from a full pdf.

convert LargeSample.pdf split/page-%0d.jpg
rm split/page-2.jpg
convert split/*.jpg merged/full.pdf

With PHP: You can use PHP exec function to run these commands, ie:

<?php
exec('convert LargeSample.pdf split/page-%0d.jpg');

Links to read:

http://linuxcommando.blogspot.com/2015/03/how-to-merge-or-split-pdf-files-using.html http://linuxcommando.blogspot.com/2013/02/splitting-up-is-easy-for-pdf-file.html http://stackoverflow.com/questions/17025515/converting-a-multi-page-pdf-to-multiple-pages-using-a-single-command http://codetheory.in/convert-split-pdf-files-into-images-with-imagemagick-and-ghostscript/