PDFminer: PDFTextExtractionNotAllowed Error
<p>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:</p>

<pre><code>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 &lt;cStringIO.StringO object at 0x7f79137a1ab0&gt;
</code></pre>

<p>I've checked stackoverflow and <a href="https://stackoverflow.com/questions/28192977/how-to-unlock-a-secured-read-protected-pdf-in-python">someone else who had this error</a> found their pdfs to be secured with a password.  However, I'm able to access the pdfs through preview on my mac.  </p>

<p>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.</p>

<p>Here's an example from the site I'm downloading pdfs from:
<a href="http://www.sophia-project.org/uploads/1/3/9/5/13955288/aristotle_firstprinciples.pdf" rel="noreferrer">http://www.sophia-project.org/uploads/1/3/9/5/13955288/aristotle_firstprinciples.pdf</a></p>

<p>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:</p>

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

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

<pre><code>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)
</code></pre>

<p>Lastly, here is the function I'm using to extract the text.</p>

<pre><code>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
</code></pre>

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