PDF Manipulations And Conversions From Linux Command Prompt


If PDF is electronic paper, then pdftk is an electronic staple-remover, hole-punch, binder, secret-decoder-ring, and X-Ray-glasses. Pdftk is a simple tool for doing everyday things with PDF documents. Pdftk allows you to manipulate PDF easily and freely. It does not require Acrobat, and it runs on Linux, Windows, Mac OS X, FreeBSD and Solaris.

In Debian/Ubuntu you can install pdftk via apt:

$ sudo aptitude install pdftk

Merge Two or More PDFs into a New Document

$ pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdf

or (Using Handles):

$ pdftk A=1.pdf B=2.pdf cat A B output 12.pdf

or (Using Wildcards):

$ pdftk *.pdf cat output combined.pdf

Split Select Pages from Multiple PDFs into a New Document

$ pdftk A=one.pdf B=two.pdf cat A1-7 B1-5 A8 output combined.pdf

Rotate the First Page of a PDF to 90 Degrees Clockwise

$ pdftk in.pdf cat 1E 2-end output out.pdf

Rotate an Entire PDF Document’s Pages to 180 Degrees

$ pdftk in.pdf cat 1-endS output out.pdf

Encrypt a PDF using 128-Bit Strength (the Default) and Withhold All Permissions (the Default)

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foopass

Same as Above, Except a Password is Required to Open the PDF

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz

Same as Above, Except Printing is Allowed (after the PDF is Open)

$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz allow printing

Decrypt a PDF

$ pdftk secured.pdf input_pw foopass output unsecured.pdf

Join Two Files, One of Which is Encrypted (the Output is Not Encrypted)

$ pdftk A=secured.pdf mydoc.pdf input_pw A=foopass cat output combined.pdf

Uncompress PDF Page Streams for Editing the PDF Code in a Text Editor

$ pdftk mydoc.pdf output mydoc.clear.pdf uncompress

Repair a PDF’s Corrupted XREF Table and Stream Lengths (If Possible)

$ pdftk broken.pdf output fixed.pdf

Burst a Single PDF Document into Single Pages and Report its Data to doc_data.txt

$ pdftk mydoc.pdf burst

Report on PDF Document Metadata, Bookmarks and Page Labels

$ pdftk mydoc.pdf dump_data output report.txt

Poppler is a PDF rendering library based on the xpdf-3.0 code base. The poppler-utils package contains pdftops (PDF to PostScript converter), pdfinfo (PDF document information extractor), pdfimages (PDF image extractor), pdftohtml (PDF to HTML converter), pdftotext (PDF to text converter), and pdffonts (PDF font analyzer).

Debian/Ubuntu users can install pdftk via apt:

$ sudo aptitude install poppler-utils

Convert PDF to TEXT

Pdftotext converts Portable Document Format (PDF) files to plain text.

$ pdftotext example.pdf example.txt

If textfile is not specified, pdftotext converts file.pdf to file.txt.  If text-file is ´-’, the text is sent to stdout.

To convert  page  from 3 to 7 (including 3 and 7) use:

$ pdftotext -f 3 -l 7 example.pdf example.txt

To extract only 3rd page

$ pdftotext -f 3 -l 3 example.pdf example.txt
$ pdftotext -layout example.pdf example.txt

Maintain the original physical layout of the text and output the text in reading order.

Set the  -nopgbrk option if you don’t want insert page breaks.

Uset -opw (owner password) or -upw (user password) options if the PDF file is password protected.

Extract Images From PDF

Pdfimages saves images from a Portable Document Format (PDF) file as Portable Pixmap (PPM), Portable Bitmap (PBM), or JPEG files.

Pdfimages reads the PDF file, scans one or more pages, and writes one PPM, PBM, or JPEG file for each image, image-root-nnn.xxx, where nnn is the image number and xxx is the image type (.ppm, .pbm, .jpg).

Pdfimages extracts the raw image data from the PDF file, without performing any additional transforms. Any rotation, clipping, color inversion, etc. done by the PDF content stream is ignored.

$ pdfimages example.pdf exampleimage

The above command will extract all images from example.pdf. The images will be saved in PPM format.

Use -j option to save images as JPG format

$ pdfimages -j example.pdf exampleimage

Use the -f and -l options to specify the startpage and lastpage to scan. To scan pages 3 to 7 (including 3 and 7) use:

$ pdfimages -f 3 -l 7 example.pdf exampleimage

To scan only one specific page use:

$ pdfimages -f 3 -l 3 example.pdf exampleimage

If the PDF file is password protected use -opw or -upw option:

-opw Owner password
-upw User password

Convert PDF to HTML

pdftohtml is a program that converts pdf documents into html. It generates its output in the current working directory.


$ pdftohtml file.pdf file.html

If you want to see graphics, you’ll need to use the -c (as in “complex”) option:

$ pdftohtml -c file.pdf file.html

Convert PDF to Image

First you need to have ImageMagick installed in your machine.
To install ImageMagick in Debian/Ubuntu run the following command:

$ sudo aptitude install imagemagick

To convert pdf file to image use the ‘convert‘ command:

$ convert doc.pdf doc.jpeg

convert to tiff

$ convert doc.pdf doc.tiff

for more information look here.

# #


Guy September 23, 2010

Thanks for the article. Each of the examples for pdfimages use the command “pfdimages” where it should read “pdfimages” (ie 2nd and 3rd letters transposed).

segfault September 23, 2010


Thanks for pointing that out.