Run `CERMINE <https://github.com/CeON/CERMINE>`_ to extract metadata from \
            the given PDF file, to retrieve citations (and more) from the \
            provided PDF file. This function returns the raw output of \
            CERMINE call.

    .. note::

        Try to use a local CERMINE JAR file, and falls back to using the API. \
                JAR file is expected to be found in \
                ``libbmc/external/cermine.jar``. You can override this using \
                the ``override_local`` parameter.

    .. note::

        CERMINE JAR file can be found at \
                `<http://maven.icm.edu.pl/artifactory/simple/kdd-releases/pl/edu/icm/cermine/cermine-impl/>`_.

    .. note::

        This fallback using the \
                `CERMINE API <http://cermine.ceon.pl/about.html>`_, and \
                hence, uploads the PDF file (so uses network). Check out \
                the CERMINE API terms.

    :param pdf_file: Path to the PDF file to handle.
    :param force_api: Force the use of the Cermine API \
            (and do not try to use a local JAR file). Defaults to ``False``.
    :param override_local: Use this specific JAR file, instead of the one at \
            the default location (``libbmc/external/cermine.jar``).
    :returns: Raw output from CERMINE API or ``None`` if an error occurred. \
            No post-processing is done.

def cermine(pdf_file, force_api=False, override_local=None):
    """
    Run `CERMINE <https://github.com/CeON/CERMINE>`_ to extract metadata from \
            the given PDF file, to retrieve citations (and more) from the \
            provided PDF file. This function returns the raw output of \
            CERMINE call.

    .. note::

        Try to use a local CERMINE JAR file, and falls back to using the API. \
                JAR file is expected to be found in \
                ``libbmc/external/cermine.jar``. You can override this using \
                the ``override_local`` parameter.

    .. note::

        CERMINE JAR file can be found at \
                `<http://maven.icm.edu.pl/artifactory/simple/kdd-releases/pl/edu/icm/cermine/cermine-impl/>`_.

    .. note::

        This fallback using the \
                `CERMINE API <http://cermine.ceon.pl/about.html>`_, and \
                hence, uploads the PDF file (so uses network). Check out \
                the CERMINE API terms.

    :param pdf_file: Path to the PDF file to handle.
    :param force_api: Force the use of the Cermine API \
            (and do not try to use a local JAR file). Defaults to ``False``.
    :param override_local: Use this specific JAR file, instead of the one at \
            the default location (``libbmc/external/cermine.jar``).
    :returns: Raw output from CERMINE API or ``None`` if an error occurred. \
            No post-processing is done.
    """
    try:
        # Check if we want to load the local JAR from a specific path
        local = override_local
        # Else, try to stat the JAR file at the expected local path
        if (local is None) and (not force_api):
            if os.path.isfile(os.path.join(SCRIPT_DIR,
                                           "../external/cermine.jar")):
                local = os.path.join(SCRIPT_DIR,
                                     "../external/cermine.jar")

        # If we want to force the API use, or we could not get a local JAR
        if force_api or (local is None):
            print("Using API")
            with open(pdf_file, "rb") as fh:
                # Query the API
                request = requests.post(
                    CERMINE_BASE_URL + "extract.do",
                    headers={"Content-Type": "application/binary"},
                    files={"file": fh}
                )
                return request.text
        # Else, use the local JAR file
        else:
            return subprocess.check_output([
                "java",
                "-cp", local,
                "pl.edu.icm.cermine.PdfNLMContentExtractor",
                "-path", pdf_file]).decode("utf-8")
    except (RequestException,
            subprocess.CalledProcessError,
            FileNotFoundError):
        # In case of any error, return None
        return None