Opening a pdf bytestring for reading
<p>I am working on a scrapy spider, trying to convert pdfs, using pdfminer (<a href="https://pypi.python.org/pypi/pdfminer2" rel="nofollow">https://pypi.python.org/pypi/pdfminer2</a>). I have no interest in saving the actual PDF to disk , and so I've been advised to look into the io.bytesIO subclass at <a href="https://docs.python.org/2/library/io.html#buffered-streams" rel="nofollow">https://docs.python.org/2/library/io.html#buffered-streams</a>. Based on <a href="http://stackoverflow.com/questions/39799009/creating-and-accessing-bytesio-object/39799090#39799090">Creating bytesIO object</a> , I have initialized the bytesIO class with the pdf body, but now I need to open the data and follow and example like the basic usage <a href="http://www.unixuser.org/~euske/python/pdfminer/programming.html" rel="nofollow">http://www.unixuser.org/~euske/python/pdfminer/programming.html</a> So far based on <a href="http://zevross.com/blog/2014/04/09/extracting-tabular-data-from-a-pdf-an-example-using-python-and-regular-expressions/" rel="nofollow">http://zevross.com/blog/2014/04/09/extracting-tabular-data-from-a-pdf-an-example-using-python-and-regular-expressions/</a> I have:</p>

<pre><code>    in_memory_pdf = BytesIO(bytes(response.body))
    in_memory_pdf.seek(0)

    rsrcmgr = PDFResourceManager()
    retstr = StringIO()

    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)

    fp = file(in_memory_pdf, 'rb')

    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos=set()
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
        interpreter.process_page(page)
    fp.close()
    device.close()
    st = retstr.getvalue()
    retstr.close()
    print st
</code></pre>

<p>When I run this I get:</p>

<pre><code>fp = file(in_memory_pdf, 'rb')
TypeError: coercing to Unicode: need string or buffer, _io.BytesIO found
</code></pre>

<p>How can I open this pdf bytestring for processing?</p>

<p>After the suggested change I'm getting:</p>

<pre><code>2016-10-17 23:59:35 [root] DEBUG: exec: ET
2016-10-17 23:59:35 [root] DEBUG: nexttoken: (2819L, /'Q')
2016-10-17 23:59:35 [root] DEBUG: do_keyword: pos=2819L, token=/'Q', stack=[]
2016-10-17 23:59:35 [root] DEBUG: add_results: ((2819L, /'Q'),)
2016-10-17 23:59:35 [root] DEBUG: nextobject: (2819L, /'Q')
2016-10-17 23:59:35 [root] DEBUG: exec: Q

Traceback (most recent call last):
  File "C:\\site-packages\twisted\internet\defer.py", line 588, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "C:\j1\spiders\j1_spider.py", line 235, in parse_pdf_to_html
    interpreter.process_page(page)
  File "C:\\site-packages\pdfminer\pdfinterp.py", line 835, in process_page
    self.device.end_page(page)
  File "C:\\site-packages\pdfminer\converter.py", line 53, in end_page
    self.receive_layout(self.cur_item)
  File "C:\\site-packages\pdfminer\converter.py", line 206, in receive_layout
    render(ltpage)
  File "C:\\site-packages\pdfminer\converter.py", line 196, in render
    render(child)
  File "C:\\site-packages\pdfminer\converter.py", line 196, in render
    render(child)
  File "C:\\site-packages\pdfminer\converter.py", line 196, in render
    render(child)
  File "C:\\site-packages\pdfminer\converter.py", line 198, in render
    self.write_text(item.get_text())
  File "C:\\site-packages\pdfminer\converter.py", line 189, in write_text
    self.outfp.write(text)
TypeError: unicode argument expected, got 'str'
</code></pre>

<p>There are two problems:</p>

<ol>
<li><code>in_memory_pdf</code> is already a file-like object for <code>str</code> (or <code>bytes</code> in Py3), can be directly used without opening. Thus changing <code>fp = file(in_memory_pdf, 'rb')</code> to <code>fp = in_memory_pdf</code> partially worked.</li>
<li>The second parameter of <code>TextConverter</code> should also be a file-like object for <code>str</code> (or <code>bytes</code> in Py3). But <code>retstr</code> in the question is for <code>unicode</code> (or <code>str</code> in Py3). Therefore <code>retstr = StringIO()</code> should be changed to <code>retstr = BytesIO()</code>.</li>
</ol>

python pdf