PDF - the Portable Document Format


How to concatenate several JPEG files into a single PDF file ?

convert image_1.jpg image_2.jpg image_3.jpg output.pdf
If this outputs the error (details : 1, 2) :
convert-im6.q16: attempt to perform an operation not allowed by the security policy `PDF' @ error/constitute.c/IsCoderAuthorized/408.
As root :
mv /etc/ImageMagick-6/policy.xml{,_DISABLED}

as a one-liner :

mogrify -resize 40% *jpg; convert image_{1,2,3}.jpg output.pdf

How to rotate PDF pages ?

pdftk myDocument.pdf cat 1-endE output output.pdf

How to edit the contents of a PDF file ?

Even though not everything can be done (depends on how the PDF was made), you may by opening a PDF file with Inkscape.

How to extract pages from a PDF document ?

The quick and easy solution

Open the PDF file with any viewer and print to file the selected page(s).

A feature-full solution with pdftk

pdftk can do MUCH MORE than this to PDF documents (join, rotate pages, ...). For more : man pdftk

A basic example

pdftk A=input.pdf cat A2 output output.pdf
  • A serves as a handle on an input file
  • A2 means "page 2 of file with handle A"

A not-so-basic example

Let's imagine you received a scanned document of more than 100 pages having randomly-placed blank pages. You'd like to generate a PDF without those blank pages, which means you want to keep pages (pages listed below are included) :
  • odd pages between page 1 and until page 51
  • then all pages from 53 to 77
  • then all pages from 79 to 101
  • then the page 103
The command would then be :
pdftk A='documentWithBlankPages.pdf' cat 1-51odd 53-77 79-101 103 output cleanDocument.pdf
Don't forget the output keyword

How to concatenate several PDF files into a single PDF file ?

The Ghostscript method (source)

  • gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=concatenated.pdf page_1.pdf page_2.pdf page_n.pdf
  • gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=concatenated.pdf page_{1,2,,n}.pdf

Or as an alias :

pdfcat () {
	gs -q -sPAPERSIZE=a4 -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=concatenated.pdf "$@"

With PDFtk (source)

  • pdftk page_1.pdf page_2.pdf page_n.pdf cat output concatenated.pdf
  • pdftk page_{1,2,,n}.pdf cat output concatenated.pdf


There is also pdfjoin, but I've not tried it yet.