PDFMiner

Python PDF parser and analyzer

Homepage Recent Changes

Last Modified: Sun May 17 23:10:42 JST 2009

What's It?

PDFMiner is a suite of programs that help extracting and analyzing text data of PDF documents. Unlike other PDF-related tools, it allows to obtain the exact location of texts in a page, as well as other extra information such as font information or ruled lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purpoes instead of text analysis.

Features:

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

Discussion: (for questions and comments, post here)
http://groups.google.com/group/pdfminer-users/

View the source:
http://code.google.com/p/pdfminerr/source/browse/trunk/pdfminer

Online Demonstration: (pdf -> html conversion webapp)
http://pdf2html.tabesugi.net:8080/


How to Install

  1. Install Python 2.4 or newer.
  2. Download the PDFMiner source.
  3. Extract it.
  4. Run setup.py to install:
    # python setup.py install
    
  5. Do the following test:
    $ pdf2txt.py samples/simple1.pdf
    <html><head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    </head><body>
    <span style="position:absolute; border: gray 1px solid; left:0px; top:50px; width:612px; height:792px;"></span>
    <div style="position:absolute; top:50px;"><a name="1">Page 1</a></div>
    <span style="position:absolute; writing-mode:lr-tb; left:158px; top:224px; font-size:22px;"> World </span>
    <span style="position:absolute; writing-mode:lr-tb; left:100px; top:124px; font-size:22px;"> </span>
    <span style="position:absolute; writing-mode:lr-tb; left:100px; top:224px; font-size:22px;"> Hello </span>
    <span style="position:absolute; writing-mode:lr-tb; left:368px; top:124px; font-size:22px;">World </span>
    <span style="position:absolute; writing-mode:lr-tb; left:206px; top:124px; font-size:22px;">Hello </span>
    <div style="position:absolute; top:0px;">Page: <a href="#1">1</a></div>
    </body></html>
    
  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, which is distributed from Adobe.

Here is how:

  1. Get a CMap archive file from http://www.unixuser.org/~euske/pub/CMap.tar.bz2
  2. Expand the archive and put the CMap directory under the directory where pdfminer is installed. (Normally this should be something like /usr/lib/python2.5/site-packages.) For example:
    $ cd /usr/lib/python2.5/site-packages
    $ tar jxf CMap.tar.bz2
    
  3. Do the follwoing. (this is optional, but highly recommended)
    $ python -m pdfminer.cmap
    
    This may take several minutes.

How to Use

PDFMiner comes with two handy tools: 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, i.e. it cannot extract texts drawn as images that require optical character recognition. It also extracts the corresponding locations, font names, font sizes, writing direction (horizontal or vertical) for each text portion. You need to provide a password for protected PDF documents when its access is restricted. You cannot extract any text from a PDF document which does not have extraction permission.

For non-ASCII languages, you can specify the output encoding (such as UTF-8).

Note: Not all characters in a PDF can be safely converted to Unicode.

Examples:

$ pdf2txt.py samples/naacl06-shinyama.pdf > output.html
(extract text as an HTML file whose filename is output.html)

$ pdf2txt.py -c euc-jp samples/jo.pdf > output.html
(extract a Japanese HTML file in vertical writing, CMap is required)

$ pdf2txt.py -P mypassword -t text secret.pdf > output.txt
(extract a text from an encrypted PDF file)

Options:

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

-p pageno[,pageno,...]
Specifies the comma-separated list of the page numbers to be extracted. Page numbers are starting from one. By default, it extracts texts from all the pages.

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

-t type
Specifies the output format. The following formats are currently supported.

-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 -T foo.pdf
(dump the table of contents)

$ 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
Specifies the page number to be extracted. Multiple -p options are allowed. Note that page numbers start from one.

-r (raw)
-b (binary)
-t (text)
Specifies 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-2009 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