
from urllib.request import urlopen
from pdfminer.pdfinterp import PDFResourceManager, process_pdf
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from io import StringIO
from io import open
import os

def readPDF(pdfFile):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, laparams=laparams)
    process_pdf(rsrcmgr, device, pdfFile)
    device.close()
    content = retstr.getvalue()
    retstr.close()
    return content
    

if __name__ == '__main__':
    disk = os.getcwd()
    path = disk[0] + r':/Work/AllData/pdftest/'
    string = ''
    with open(r'D:\Work\AllData\pdftest\脑卒中后继发肺血栓栓塞症的临床分析.pdf', 'rb') as f:
        string = readPDF(f)
    with open(path + 'file02.txt', 'w') as er:
        er.write(string)
    print('... have done!')
    """
    pdfFile = urlopen("http://pythonscraping.com/pages/warandpeace/chapter1.pdf")
    outputString = readPDF(pdfFile)
    print(outputString)
    pdfFile.close()
    """
