Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDFminer: PDFTextExtractionNotAllowed Error

I'm trying to extract text from pdfs I've scraped off the internet, but when I attempt to download them I get the error:

File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdfpage.py", line 124, in get_pages
    raise PDFTextExtractionNotAllowed('Text extraction is not allowed: %r' % fp)
PDFTextExtractionNotAllowed: Text extraction is not allowed <cStringIO.StringO object at 0x7f79137a1ab0>

I've checked stackoverflow and someone else who had this error found their pdfs to be secured with a password. However, I'm able to access the pdfs through preview on my mac.

Someone mentioned that preview may view secured pdfs anyway, so I opened the files in Adobe Acrobat Reader as well and was still able to access the pdf.

Here's an example from the site I'm downloading pdfs from: http://www.sophia-project.org/uploads/1/3/9/5/13955288/aristotle_firstprinciples.pdf

I discovered that if I open the pdf manually and re-export it as a pdf to the same filepath (basically replacing the original with a 'new' file), then I am able to extract text from it. I'm guessing it has something to do with downloading them from the site. I'm simply using urllib to download the pdfs as follows:

if not os.path.isfile(filepath):
    print '\nDownloading pdf'
    urllib.urlretrieve(link, filepath)
else:
    print '\nFile {} already exists!'.format(title)

I also tried rewriting the file to a new filepath, but it still resulted in the same error.

if not os.path.isfile(filepath):
    print '\nDownloading pdf'
    urllib.urlretrieve(link, filepath)

    with open(filepath) as f:
        new_filepath = re.split(r'\.', filepath)[0] + '_.pdf'
        new_f = file(new_filepath, 'w')
        new_f.write(f.read())
        new_f.close()

    os.remove(filepath)
    filepath = new_filepath
else:
    print '\nFile {} already exists!'.format(title)

Lastly, here is the function I'm using to extract the text.

def convert(fname, pages=None):
    '''
    Get text from pdf
    '''
    if not pages:
        pagenums = set()
    else:
        pagenums = set(pages)

    output = StringIO()
    manager = PDFResourceManager()
    converter = TextConverter(manager, output, laparams=LAParams())
    interpreter = PDFPageInterpreter(manager, converter)

    infile = file(fname, 'rb')
    try:
        for page in PDFPage.get_pages(infile, pagenums):
            interpreter.process_page(page)
    except PDFTextExtractionNotAllowed:
        print 'This pdf won\'t allow text extraction!'

    infile.close()
    converter.close()
    text = output.getvalue()
    output.close

    return text

Is there any way I can programmatically solve this rather than manually re-exporting the files in preview?

like image 894
Tyler Lazoen Avatar asked Oct 11 '16 16:10

Tyler Lazoen


1 Answers

Full disclosure, I am one of the maintainers of pdfminer.six. It is a community-maintained version of pdfminer for python 3.

This issue was fixed in 2020 by disabling the check_extractable by default. It now shows a warning instead of raising an error.

Similar question and answer here.

like image 178
Pieter Avatar answered Sep 17 '22 00:09

Pieter