PDFMiner

Last Modified: Wed Apr 30 19:15:12 JST 2008

What's It?

PDFMiner is a suite of programs that aims to help extracting or analyzing text data from PDF documents. Unlike other PDF-related tools, it allows to obtain the exact location of texts in a page, as well as other layout information such as font size or font name, which could be useful for analyzing the document. It can be also used as a basis for a full-fledged PDF interpreter.

Features:

Homepage:
http://www.unixuser.org/~euske/python/pdfminer/index.html

Download (source):
http://www.unixuser.org/~euske/python/pdfminer/pdfminer-dist-20080429.tar.gz (1.8Mbytes)

Svn repository:
http://pdfminerr.googlecode.com/svn/


How to Install

  1. Install Python 2.4 or newer.
  2. Download the PDFMiner source.
  3. Extract it.
  4. Go to the pdfminer directory.
  5. Do the following test:
    $ python pdf2txt.py samples/simple1.pdf
    <document>
    <page id="0" bbox="0,0,612,792" rotate="0">
    <text x="100.000" y="100.000" font="Helvetica" size="24.000" width="137.352"> Hello World </text>
    </page>
    </document>
    
  6. Done!

For non-ASCII languages

In order to handle non-ASCII languages (e.g. Japanese), you need to install an additional data called CMap.

Here is how:

  1. Get http://www.unixuser.org/~euske/pub/CMap.tar.bz2
  2. Do the follwoing:
    $ tar jxf CMap.tar.bz2
    
  3. Put the CMap directory into the pdfminer directory.
  4. Go to the pdfminer directory.
  5. Do the follwoing: (this is optional but highly recommended)
    $ make cdbcmap
    

How to Use

PDFMiner comes with two programs: pdf2txt.py and dumppdf.py.

pdf2txt.py

pdf2txt.py extracts text contents from a PDF file. It extracts all the texts that are to be rendered programatically. It also extracts the corresponding locations, font names, and font sizes for each text portion in an SGML-like format. It cannot extract texts embedded within images (i.e. it does not do optical character recognition). You can provide a password for protected PDF documents whose access is limited.

For non-ASCII languages, you can speficy the output encoding (such as UTF-8). Note that not all characters in a PDF can be converted safely to Unicode, as some of them are not included in the current Unicode Standard.

Examples:

$ ./pdf2txt.py samples/naacl06-shinyama.pdf

$ ./pdf2txt.py -c euc-jp samples/jo.pdf
(extract Japanese texts in vertical writing, CMap is required)

$ ./pdf2txt.py -P mypassword secret.pdf
(extract texts from an encrypted PDF file with a password)

Options:

-o filename
Speficies the output file name. By default, it prints the extracted contents to stdout.

-p pageno
Speficies the page number to be extracted. Note that page numbers start from zero. Multiple -p options are allowed. By default, it extracts texts from all the pages.

-c codec
Speficies the output codec for non-ASCII texts.

-P password
Provides the user password to open the PDF file.

-d
Increases the debug level.

dumppdf.py

dumppdf.py dumps the internal contents of a PDF file in pseudo-XML format. This program is primarily for debugging purpose, but it's also possible to extract some meaningful contents (such as images).

Examples:

$ ./dumppdf.py -a foo.pdf
(dump all the headers and contents, except stream objects)

$ ./dumppdf.py -r -i6 foo.pdf > pic.jpeg
(extract a JPEG image)

Options:

-a
Instructs to dump all the objects. By default, it only prints the document trailer (like a header).

-p pageno
Speficies the page number to be extracted. Multiple -p options are allowed. Note that page numbers start from zero.

-r (raw)
-b (binary)
-t (text)
Speficies the output format of stream contents. Because the contents of stream objects can be very large, they are omitted when none of the options above is specified.

With -r option, all the stream contents are dumped without decoding. With -b option, the contents are dumped as a binary blob. With -t option, the contents are dumped in a text format, similar to repr() manner. When -r or -b option is given, no stream header is displayed for the ease of saving it to a file.

-P password
Provides the user password to open the PDF file.

-d
Increases the debug level.

Changes


Related Projects


Terms and Conditions

Copyright (c) 2004-2008 Yusuke Shinyama <yusuke at cs dot nyu dot edu>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Yusuke Shinyama