pdfminer/poppler - how to set encoding
Asked Answered
C

0

6

I have a file, i.e. http://www.agfl.cs.ru.nl/papers/manual28.pdf (it's english)

Pdfminer and poppler shows the same result in most parsed pages, like: ¾º¿  ÒÙ Öݸ ¾¼¼ Ⱥ ¾º ÂÙÒ ¸ ¾¼¼ ź Ë ÙØØ Ö¸ Ǻ Ë

It seems it can't read font custom encodings. How to specify it?

Here's code samples:

# poppler
input_filename = '/tmp/manual28.pdf'
document = poppler.document_new_from_file('file://%s' %  urllib.pathname2url(os.path.abspath(input_filename)), None)
n_pages = document.get_n_pages()
for i in range(n_pages):
    page = document.get_page(i)
    print page.get_text()
    # chardet.detect(page.get_text()) # utf8 all time

# pdfminer
def pdf_to_html(in_fp, out_fp, codec='utf-8', maxpages=0, pagenos=None, html=True):
    rsrcmgr = PDFResourceManager()
    laparams = LAParams()

    if isinstance(in_fp, basestring):
        in_fp = open(in_fp, 'rb')

    if isinstance(out_fp, basestring):
        out_fp = open(out_fp, 'wb')

    if html:
        device = HTMLConverter(rsrcmgr, out_fp, codec=codec, laparams=laparams)
    else:
        device = TextConverter(rsrcmgr, out_fp, codec=codec, laparams=laparams)

    interpreter = PDFPageInterpreter(rsrcmgr, device)
    for page in PDFPage.get_pages(in_fp, pagenos, maxpages=maxpages):
        interpreter.process_page(page)

    in_fp.close()
    device.close()
    out_fp.close()
Celadon answered 6/2, 2014 at 7:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.