#!/usr/bin/python
# Simple Python text and image extractor based on PDFlib TET
#
# $Id: extractor.py,v 1.13 2010/07/22 10:07:03 rjs Exp $
#

from sys import *
from tetlib_py import *

# global option list */
globaloptlist = "searchpath={{../data} {../../../resource/cmap}}"

# document-specific option list */
docoptlist = ""

# page-specific option list */
pageoptlist = "granularity=page"

# separator to emit after each chunk of text. This depends on the
# application's needs; for granularity=word a space character may be useful.
#
separator = "\n"

if len(argv) != 3:
    raise Exception("usage: extractor <infilename> <outfilename>\n")

try:
    try:

        tet = TET_new()

        if (version_info[0] < 3):
            fp = open(argv[2], 'w')
        else:
            fp = open(argv[2], 'w', 2, 'utf-8')

        TET_set_option(tet, globaloptlist)

        doc = TET_open_document(tet, argv[1], docoptlist)

        if (doc == -1):
            raise Exception("Error " + TET_get_errnum(tet) + "in " \
                + TET_get_apiname(tet) + "(): " + TET_get_errmsg(tet))

        # get number of pages in the document */
        n_pages = TET_pcos_get_number(tet, doc, "length:pages")

        # loop over pages in the document */
        for pageno in range(1, int(n_pages)+1):
            imageno = -1

            page = TET_open_page(tet, doc, pageno, pageoptlist)

            if (page == -1):
                print("Error " + TET_get_errnum(tet) + "in " \
                    + TET_get_apiname(tet) + "(): " + TET_get_errmsg(tet))
                continue                        # try next page */

            # Retrieve all text fragments; This is actually not required
            # for granularity=page, but must be used for other granularities.
            #
            text = TET_get_text(tet, page)
            while (text):
                fp.write(text)  # print the retrieved text */

                # print a separator between chunks of text */
                fp.write(separator)
                text = TET_get_text(tet, page)

            if (TET_get_errnum(tet) != 0):
                raise Exception ("\nError " + repr(TET_get_errnum(tet)) \
                    + "in " + TET_get_apiname(tet) + "() on page " + \
                    repr(pageno) + ": " + TET_get_errmsg(tet) + "\n")

            TET_close_page(tet, page)

        TET_close_document(tet, doc)

    except TETlibException:
        print("TET exception occurred:\n[%d] %s: %s" %
            ((TET_get_errnum(tet)), TET_get_apiname(tet),  TET_get_errmsg(tet)))

    except Exception, e:
        print e;

finally:
    TET_delete(tet)
