#!/usr/bin/python
# coding: utf-8

import os
import re
import sys
from binascii import b2a_hex
from operator import itemgetter
import lxml.etree
from xml.sax.saxutils import escape
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfdocument import PDFDocument, PDFNoOutlines
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import PDFPageAggregator
from pdfminer.pdfpage import PDFPage
from pdfminer.layout import LAParams, LTTextBox, LTTextLine, LTFigure, LTImage, LTTextLineHorizontal

__all__ = ['pdf2xml','pdf2xml_pages', 'parse_page_xml']

def parse_page_xml(fileobj):

    pdfxml = fileobj.read()
    root = lxml.etree.fromstring(pdfxml)

    fontspecs = {}
    rows = []

    pages = []
    for pagenum, page in enumerate(root):
        assert page.tag == 'page'
        pagelines = {}
        for v in page:
            if v.tag == 'text':
                # there has to be a better way here to get the contents
                text = re.match('(?s)<text.*?>(.*?)</text>', lxml.etree.tostring(v)).group(1)
                #print >> sys.stderr, text
                if not text.strip():
                    continue
                left = int(v.attrib.get('left'))
                top  = int(v.attrib.get('top'))

                # fix some off-by-one placement issues, which make some text span over two lines where it should be in one
                if pagelines.has_key(top-1):
                    top = top - 1
                elif pagelines.has_key(top+1):
                    top = top + 1
                line = pagelines.setdefault(top, [])
                line.append((left, text))
        ordered = list(sorted([(k, sorted(v)) for k,v in pagelines.iteritems()]))
        rows.extend(ordered)
        pages.append((pagenum, ordered))
    return pages

def with_pdf (pdf_doc, pdf_pwd, fn, *args):
    """Open the pdf document, and apply the function, returning the results"""
    result = None
    try:
        # open the pdf file
        if hasattr(pdf_doc, 'read'):
            fp = pdf_doc
        else:
            fp = open(pdf_doc, 'rb')
        # create a parser object associated with the file object
        parser = PDFParser(fp)
        # create a PDFDocument object that stores the document structure
        doc = PDFDocument(parser)
        # connect the parser and document objects
        parser.set_document(doc)

        if doc.is_extractable:
            # apply the function and return the result
            result = fn(doc, *args)

        # close the pdf file
        fp.close()
    except IOError:
        # the file doesn't exist or similar problem
        pass
        raise
    return result

def _parse_toc (doc):
    """With an open PDFDocument object, get the table of contents (toc) data
    [this is a higher-order function to be passed to with_pdf()]"""
    toc = []
    try:
        outlines = doc.get_outlines()
        for (level,title,dest,a,se) in outlines:
            toc.append( (level, title) )
    except PDFNoOutlines:
        pass
    return toc

def get_toc (pdf_doc, pdf_pwd=''):
    """Return the table of contents (toc), if any, for this pdf file"""
    return with_pdf(pdf_doc, pdf_pwd, _parse_toc)

def _parse_pages (doc, image_handler):
    """With an open PDFDocument object, get the pages, parse each one, and return the entire text
    [this is a higher-order function to be passed to with_pdf()]"""
    rsrcmgr = PDFResourceManager()
    laparams = LAParams()
    device = PDFPageAggregator(rsrcmgr, laparams=laparams)
    interpreter = PDFPageInterpreter(rsrcmgr, device)

    text_content = [] # a list of strings, each representing text collected from each page of the doc
    text_content.append('<pdf2xml>')
    for i, page in enumerate(PDFPage.create_pages(doc)):
        text_content.append('<page number="%s" width="%s" height="%s">' % (i+1, page.mediabox[2], page.mediabox[3]))
        interpreter.process_page(page)
        # receive the LTPage object for this page
        layout = device.get_result()
        page_height = int(layout.bbox[3])
        # layout is an LTPage object which may contain child objects like LTTextBox, LTFigure, LTImage, etc.
        ret = parse_lt_objs(layout._objs, (i+1), image_handler, page_height)
        text_content.append(ret)
        text_content.append('</page>')

    text_content.append('</pdf2xml>')
    return text_content

def get_pages (pdf_doc, pdf_pwd='', image_handler=None):
    """Process each of the pages in this pdf file and print the entire text to stdout"""
    return with_pdf(pdf_doc, pdf_pwd, _parse_pages, *tuple([image_handler]))

def parse_lt_objs (lt_objs, page_number, image_handler, page_height, text=[]):
    """Iterate through the list of LT* objects and capture the text or image data contained in each"""
    text_content = []

    page_text = {} # k=(x0, x1) of the bbox, v=list of text strings within that bbox width (physical column)
    for lt_obj in lt_objs:
        if isinstance(lt_obj, LTTextBox) or isinstance(lt_obj, LTTextLine):
            # text, so arrange is logically based on its column width
            update_page_text_hash(page_text, lt_obj)
        elif isinstance(lt_obj, LTImage):
            # an image, so save it to the designated folder, and note it's place in the text
            saved_file = save_image(lt_obj, page_number, image_handler)
            if saved_file:
                # use html style <img /> tag to mark the position of the image within the text
                text_content.append('<img src="'+ saved_file +'" />')
        elif isinstance(lt_obj, LTFigure):
            # LTFigure objects are containers for other LT* objects, so recurse through the children
            text_content.append(parse_lt_objs(lt_obj._objs, page_number, image_handler, page_height, text_content))

    page_text_items = [(k[0], k[1], k, v) for k,v in page_text.items()]

    page_text_items = list(sorted(sorted(page_text_items, key=itemgetter(0)), key=itemgetter(1), reverse=True))
    sorted_text = [(c,d) for a,b,c,d in page_text_items]

    for k, v in sorted_text:
        # sort the page_text hash by the keys (x0,x1 values of the bbox),
        # which produces a top-down, left-to-right sequence of related columns
        a = k
        pos = 'top="%s" left="%s" width="%s" height="%s"' % (page_height-int(a[1]), int(a[0]), int(a[2]-a[0]), int(a[3]-a[1]))
        text_content.append('<text %s>%s</text>' % (pos, escape(v)))

    return '\n'.join(text_content)

def to_bytestring (s, enc='utf-8'):
    """Convert the given unicode string to a bytestring, using the standard encoding,
    unless it's already a bytestring"""
    if s:
        if isinstance(s, str):
            return s
        else:
            return s.encode(enc)

def update_page_text_hash (h, lt_obj, pct=0.2):
    """Use the bbox x0,x1 values within pct% to produce lists of associated text within the hash"""

    for obj in lt_obj._objs:
        if isinstance(obj, LTTextLineHorizontal):
            h[obj.bbox] = to_bytestring(obj.get_text()).rstrip('\n')
    return h

def save_image (lt_image, page_number, image_handler):
    """Try to save the image data from this LTImage object, and return the file name, if successful"""
    result = None
    if callable(image_handler):
        if lt_image.stream:
            file_stream = lt_image.stream.get_rawdata()
            file_ext = determine_image_type(file_stream[0:4])
            if file_ext:
                file_name = image_handler(lt_image.stream.get_rawdata(), file_ext)
                if file_name:
                    result = file_name
    return result

def determine_image_type (stream_first_4_bytes):
    """Find out the image file type based on the magic number comparison of the first 4 (or 2) bytes"""
    file_type = None
    bytes_as_hex = b2a_hex(stream_first_4_bytes)
    if bytes_as_hex.startswith('ffd8'):
        file_type = '.jpeg'
    elif bytes_as_hex == '89504e47':
        file_type = '.png'
    elif bytes_as_hex == '47494638':
        file_type = '.gif'
    elif bytes_as_hex.startswith('424d'):
        file_type = '.bmp'
    return file_type

def write_file (folder, filename, filedata, flags='w'):
    """Write the file data to the folder and filename combination
    (flags: 'w' for write text, 'wb' for write binary, use 'a' instead of 'w' for append)"""
    result = False
    if os.path.isdir(folder):
        try:
            file_obj = open(os.path.join(folder, filename), flags)
            file_obj.write(filedata)
            file_obj.close()
            result = True
        except IOError:
            pass
    return result

def pdf2xml_pages(fileobj, image_handler=None):
    return get_pages(fileobj, image_handler=image_handler)

def pdf2xml(fileobj, image_handler=None):
    return '\n'.join(pdf2xml_pages(fileobj, image_handler=image_handler))


if __name__ == "__main__":
    print('')
