Extracting Tables from PDF
Your requirement to extract tables from PDFs is not easy to meet.
Standard PDFs do not provide any hints about the semantics of what they draw on a page: the only distinction that the syntax provides is the distinctions between vector elements (lines, fills,...), images and text.
Whether any character is part of a table or part of a line or just a lonely, single character within an otherwise empty area is not easy to recognize programmatically by parsing the PDF source code.
For a background about why the PDF file format should never, ever be thought of as suitable for hosting extractable, structured data, see this article:
Why Updating Dollars for Docs Was So Difficult (ProPublica-Website)
Having said the above now let me add this:
Tabula is written in Ruby. Its source code is open on GitHub. Its license is Free and non-GPL.
Extracting Images from a PDF
Here I'd recommend to look into Poppler. Poppler is a PDF rendering library forked from the xpdf-3.0 code base.
It ships with a command line utility,
pdfimages which can list and extract images embedded in PDF files. Here is an example run which lists all images contained in page range 3-5:
$ pdfimages -list -f 3 -l 5 some.pdf
page num type width height color comp bpc enc interp objectID x-ppi y-ppi size ratio
3 0 image 577 645 rgb 3 8 image yes 593 0 152 152 106K 9.7%
3 1 smask 577 645 gray 1 8 image no 593 0 152 152 383B 0.1%
3 2 image 382 419 rgb 3 8 image yes 594 0 110 110 56.7K 12%
3 3 smask 382 419 gray 1 8 image no 594 0 110 110 178B 0.1%
4 4 image 1569 344 rgb 3 8 image yes 534 0 162 162 36.4K 2.3%
5 5 image 719 930 index 1 8 image yes 535 0 146 146 42.6K 6.5%
As you can see, the output is a table containing all kinds of useful information about the embedded images:
- the page number they appear on,
- the image type,
- their width/height dimensions,
- their color space,
- the number of base colors,
- color depth,
- their encoding,
- their PDF object ID,
- their respective resolutions in X or Y directions and
- their sizes and their compression ratio.
To extract all images from page 9 as PNG files and naming them with a
images-from-page9--- prefix, use this command:
$ pdfimages -png -f 9 -l 9 some.pdf images-from-page9---
To see more options, run
Of course, if you do not want the command line tool, but instead use the Poppler API/library with your own code, you can achieve the same functions...
Extracting Text from PDF
The same Poppler library also ships with a command line program to extract text:
Here is its short help output:
$ pdftotext -h
pdftotext version 0.32.0
Copyright 2005-2015 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011 Glyph & Cog, LLC
Usage: pdftotext [options] <PDF-file> [<text-file>]
-f <int> : first page to convert
-l <int> : last page to convert
-r <fp> : resolution, in DPI (default is 72)
-x <int> : x-coordinate of the crop area top left corner
-y <int> : y-coordinate of the crop area top left corner
-W <int> : width of crop area in pixels (default is 0)
-H <int> : height of crop area in pixels (default is 0)
-layout : maintain original physical layout
-fixed <fp> : assume fixed-pitch (or tabular) text
-raw : keep strings in content stream order
-htmlmeta : generate a simple HTML file, including the meta information
-enc <string> : output text encoding name
-listenc : list available encodings
-eol <string> : output end-of-line convention (unix, dos, or mac)
-nopgbrk : don't insert page breaks between pages
-bbox : output bounding box for each word and page size to html. Sets -htmlmeta
-opw <string> : owner password (for encrypted files)
-upw <string> : user password (for encrypted files)
As you can see, there are various command line switches to address or retrieve coordinates for text locations.
It is not always possible to extract text via
pdftotext from PDFs. The PDF files may look perfectly good, but their internal structure may be faulty when it comes to text extraction.
See for example these recent StackOverflow answers:
Poppler's license is GPL however....