django_ocr_server/utils.py
def pdf_info(pdf_content):
    """
    It extract pdfInfo from pdf 2019-03-11
    :param pdf_content: a content of a pdf file as bytes
    :return: pdf info as {}-object
    """
    def parse_pdf_datetime(pdf_datetime_str):
        """
        This inner function parse a datetime from a string returned the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate'] 2019-03-11
        :param pdf_datetime_str: a string from the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate']
        :return: datetime of the pdf document creation date
        """
        # parse datetime like 'D:20190122061133Z'
        # parse datetime like 'D:20190122061133'
        match_ob = re.match(r'^D\:(\d{14})Z?$', pdf_datetime_str)
        if match_ob:
            pdf_datetime_str = match_ob[1]
        # parse datetime like 20190122061133
        if len(pdf_datetime_str) == 14:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[4:6] \
                   + '-' + pdf_datetime_str[6:8] \
                   + ' ' + pdf_datetime_str[8:10] \
                   + ':' + pdf_datetime_str[10:12] \
                   + ':' + pdf_datetime_str[12:14]
        # like 20190309053350+00'00'
        match_ob = re.match(r"^\d{14}\+\d{2}'\d{2}'", pdf_datetime_str)
        if match_ob:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[4:6] \
                   + '-' + pdf_datetime_str[6:8] \
                   + ' ' + pdf_datetime_str[8:10] \
                   + ':' + pdf_datetime_str[10:12] \
                   + ':' + pdf_datetime_str[12:14] \
                   + '+' + pdf_datetime_str[15:17] \
                   + pdf_datetime_str[18:20]
        # like '2019-01-28 04:14:58+'
        match_ob = re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", pdf_datetime_str)
        if match_ob:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[5:7] \
                   + '-' + pdf_datetime_str[8:10] \
                   + '-' + pdf_datetime_str[11:13] \
                   + '-' + pdf_datetime_str[14:16] \
                   + '-' + pdf_datetime_str[17:19]
        # otherwise
        print(f"could not parse pdf_date_time: '{pdf_datetime_str}', using now() instead")
        today = datetime.now()
        return f"{today.date()} {today.time()}"
        # return pdf_datetime_str[2:6] \
        #        + '-' + pdf_datetime_str[6:8] \
        #        + '-' + pdf_datetime_str[8:10] \
        #        + ' ' + pdf_datetime_str[10:12] \
        #        + ':' + pdf_datetime_str[12:14] \
        #        + ':' + pdf_datetime_str[14:16] \
        #        + '+' + pdf_datetime_str[17:19]

    info_out = {'Author': '', 'CreationDate': '', 'Creator': '', 'ModDate': '', 'Producer': '', 'Title': '', 'numPages': 0}
    try:
        pdf_reader = PyPDF2.PdfFileReader(BytesIO(pdf_content))
        info = pdf_reader.getDocumentInfo()
        info_out = {'Author': '', 'CreationDate': '', 'Creator': '', 'ModDate': '', 'Producer': '', 'Title': '', 'numPages': pdf_reader.numPages}
        if '/Author' in info:
            info_out['Author'] = info['/Author']
        if '/CreationDate' in info:
            info_out['CreationDate'] = parse_pdf_datetime(info['/CreationDate'])
        if '/Creator' in info:
            info_out['Creator'] = info['/Creator']
        if '/ModDate' in info:
            info_out['ModDate'] = parse_pdf_datetime(info['/ModDate'])
        if '/Producer' in info:
            info_out['Producer'] = info['/Producer']
        if '/Title' in info:
            info_out['Title'] = info['/Title']
    except Exception as e:
        print("PyPDF2.PdfFileReader exception: "+str(e))
    return info_out

    It extract pdfInfo from pdf 2019-03-11
    :param pdf_content: a content of a pdf file as bytes
    :return: pdf info as {}-object
    
It extract pdfInfo from pdf 2019-03-11
It extract pdfInfo from pdf 2019 - 03 - 11
def pdf_info(pdf_content):
    def parse_pdf_datetime(pdf_datetime_str):
        match_ob = re.match(r'^D\:(\d{14})Z?$', pdf_datetime_str)
        if match_ob:
            pdf_datetime_str = match_ob[1]
        if len(pdf_datetime_str) == 14:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[4:6] \
                   + '-' + pdf_datetime_str[6:8] \
                   + ' ' + pdf_datetime_str[8:10] \
                   + ':' + pdf_datetime_str[10:12] \
                   + ':' + pdf_datetime_str[12:14]
        match_ob = re.match(r"^\d{14}\+\d{2}'\d{2}'", pdf_datetime_str)
        if match_ob:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[4:6] \
                   + '-' + pdf_datetime_str[6:8] \
                   + ' ' + pdf_datetime_str[8:10] \
                   + ':' + pdf_datetime_str[10:12] \
                   + ':' + pdf_datetime_str[12:14] \
                   + '+' + pdf_datetime_str[15:17] \
                   + pdf_datetime_str[18:20]
        match_ob = re.match(r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}", pdf_datetime_str)
        if match_ob:
            return pdf_datetime_str[0:4] \
                   + '-' + pdf_datetime_str[5:7] \
                   + '-' + pdf_datetime_str[8:10] \
                   + '-' + pdf_datetime_str[11:13] \
                   + '-' + pdf_datetime_str[14:16] \
                   + '-' + pdf_datetime_str[17:19]
        print(f"could not parse pdf_date_time: '{pdf_datetime_str}', using now() instead")
        today = datetime.now()
        return f"{today.date()} {today.time()}"
    info_out = {'Author': '', 'CreationDate': '', 'Creator': '', 'ModDate': '', 'Producer': '', 'Title': '', 'numPages': 0}
    try:
        pdf_reader = PyPDF2.PdfFileReader(BytesIO(pdf_content))
        info = pdf_reader.getDocumentInfo()
        info_out = {'Author': '', 'CreationDate': '', 'Creator': '', 'ModDate': '', 'Producer': '', 'Title': '', 'numPages': pdf_reader.numPages}
        if '/Author' in info:
            info_out['Author'] = info['/Author']
        if '/CreationDate' in info:
            info_out['CreationDate'] = parse_pdf_datetime(info['/CreationDate'])
        if '/Creator' in info:
            info_out['Creator'] = info['/Creator']
        if '/ModDate' in info:
            info_out['ModDate'] = parse_pdf_datetime(info['/ModDate'])
        if '/Producer' in info:
            info_out['Producer'] = info['/Producer']
        if '/Title' in info:
            info_out['Title'] = info['/Title']
    except Exception as e:
        print("PyPDF2.PdfFileReader exception: "+str(e))
    return info_out
def pdf_info ( pdf_content ) : def parse_pdf_datetime ( pdf_datetime_str ) : """
        This inner function parse a datetime from a string returned the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate'] 2019-03-11
        :param pdf_datetime_str: a string from the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate']
        :return: datetime of the pdf document creation date
        """ match_ob = re . match ( r'^D\:(\d{14})Z?$' , pdf_datetime_str ) if match_ob : pdf_datetime_str = match_ob [ 1 ] if len ( pdf_datetime_str ) == 14 : return pdf_datetime_str [ 0 : 4 ] + '-' + pdf_datetime_str [ 4 : 6 ] + '-' + pdf_datetime_str [ 6 : 8 ] + ' ' + pdf_datetime_str [ 8 : 10 ] + ':' + pdf_datetime_str [ 10 : 12 ] + ':' + pdf_datetime_str [ 12 : 14 ] match_ob = re . match ( r"^\d{14}\+\d{2}'\d{2}'" , pdf_datetime_str ) if match_ob : return pdf_datetime_str [ 0 : 4 ] + '-' + pdf_datetime_str [ 4 : 6 ] + '-' + pdf_datetime_str [ 6 : 8 ] + ' ' + pdf_datetime_str [ 8 : 10 ] + ':' + pdf_datetime_str [ 10 : 12 ] + ':' + pdf_datetime_str [ 12 : 14 ] + '+' + pdf_datetime_str [ 15 : 17 ] + pdf_datetime_str [ 18 : 20 ] match_ob = re . match ( r"^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}" , pdf_datetime_str ) if match_ob : return pdf_datetime_str [ 0 : 4 ] + '-' + pdf_datetime_str [ 5 : 7 ] + '-' + pdf_datetime_str [ 8 : 10 ] + '-' + pdf_datetime_str [ 11 : 13 ] + '-' + pdf_datetime_str [ 14 : 16 ] + '-' + pdf_datetime_str [ 17 : 19 ] print ( f"could not parse pdf_date_time: '{pdf_datetime_str}', using now() instead" ) today = datetime . now ( ) return f"{today.date()} {today.time()}" info_out = { 'Author' : '' , 'CreationDate' : '' , 'Creator' : '' , 'ModDate' : '' , 'Producer' : '' , 'Title' : '' , 'numPages' : 0 } try : pdf_reader = PyPDF2 . PdfFileReader ( BytesIO ( pdf_content ) ) info = pdf_reader . getDocumentInfo ( ) info_out = { 'Author' : '' , 'CreationDate' : '' , 'Creator' : '' , 'ModDate' : '' , 'Producer' : '' , 'Title' : '' , 'numPages' : pdf_reader . numPages } if '/Author' in info : info_out [ 'Author' ] = info [ '/Author' ] if '/CreationDate' in info : info_out [ 'CreationDate' ] = parse_pdf_datetime ( info [ '/CreationDate' ] ) if '/Creator' in info : info_out [ 'Creator' ] = info [ '/Creator' ] if '/ModDate' in info : info_out [ 'ModDate' ] = parse_pdf_datetime ( info [ '/ModDate' ] ) if '/Producer' in info : info_out [ 'Producer' ] = info [ '/Producer' ] if '/Title' in info : info_out [ 'Title' ] = info [ '/Title' ] except Exception as e : print ( "PyPDF2.PdfFileReader exception: " + str ( e ) ) return info_out
It extract pdfInfo from pdf 2019-03-11
It extract pdfInfo from pdf 2019 - 03 - 11
"""
    It extract pdfInfo from pdf 2019-03-11
    :param pdf_content: a content of a pdf file as bytes
    :return: pdf info as {}-object
    """ """
        This inner function parse a datetime from a string returned the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate'] 2019-03-11
        :param pdf_datetime_str: a string from the PdfFileReader.getDocumentInfo()['/CreationDate'] or ['/ModDate']
        :return: datetime of the pdf document creation date
        """ # parse datetime like 'D:20190122061133Z' # parse datetime like 'D:20190122061133' # parse datetime like 20190122061133 # like 20190309053350+00'00' # like '2019-01-28 04:14:58+' # otherwise # return pdf_datetime_str[2:6] \ #        + '-' + pdf_datetime_str[6:8] \ #        + '-' + pdf_datetime_str[8:10] \ #        + ' ' + pdf_datetime_str[10:12] \ #        + ':' + pdf_datetime_str[12:14] \ #        + ':' + pdf_datetime_str[14:16] \ #        + '+' + pdf_datetime_str[17:19]
pdf info as {}-object
pdf info as {} - object
pdf_content
a content of a pdf file as bytes
a content of a pdf file as bytes