Check if a document is a PDF file and return True if is is.

def is_pdf(document):
    """Check if a document is a PDF file and return True if is is."""
    if not executable_exists('pdftotext'):
        current_app.logger.warning(
            "GNU file was not found on the system. "
            "Switching to a weak file extension test."
        )
        if document.lower().endswith(".pdf"):
            return True
        return False

    # Tested with file version >= 4.10. First test is secure and works
    # with file version 4.25. Second condition is tested for file
    # version 4.10.
    file_output = os.popen('file ' + re.escape(document)).read()
    try:
        filetype = file_output.split(":")[-1]
    except IndexError:
        current_app.logger.error(
            "Your version of the 'file' utility seems to be unsupported."
        )
        raise IncompatiblePDF2Text('Incompatible pdftotext')

    pdf = filetype.find("PDF") > -1
    # This is how it should be done however this is incompatible with
    # file version 4.10.
    # os.popen('file -bi ' + document).read().find("application/pdf")
    return pdf