This function finds URLs inside of PDF bytes.

def _pdf_find_urls(bytes, mimetype):
    """ This function finds URLs inside of PDF bytes. """

    # Start with only the ASCII bytes. Limit it to 12+ character strings.
    try:
        ascii_bytes = b' '.join(re.compile(b'[\x00\x09\x0A\x0D\x20-\x7E]{12,}').findall(bytes))
        ascii_bytes = ascii_bytes.replace(b'\x00', b'')
    except:
        return []

    urls = []

    # Find the embedded text sandwiched between [ ]
    embedded_text = set(re.compile(b'(\[(\([\x20-\x27\x2A-\x7E]{1,3}\)[\-\d]*){5,}\])').findall(ascii_bytes))

    # Get the text inside the parentheses. This catches URLs embedded in the text of the PDF that don't
    # use the normal "URI/URI()>>" method.
    for match in embedded_text:
        text = match[0]
        parentheses_text = b''.join(re.compile(b'\((.*?)\)').findall(text))
        urls.append(parentheses_text)

    # Find any URLs that use the "URI/URI()>>" method.
    urls += re.compile(b'\/URI\s*\((.*?)\)\s*>>').findall(ascii_bytes)

    if urls:
        # PDF URLs escape certain characters. We want to remove any of the escapes (backslashes)
        # from the URLs so that we get the original URL.
        urls = [u.replace(b'\\', b'') for u in urls]

    return urls