def compress(input_file_path, output_file_path, level=0, method=1):
    'Function to compress PDF via Ghostscript command line interface'
    quality = {0: '/default', 1: '/prepress', 2: '/printer', 3: '/ebook', 4: '/screen'}
    if (not os.path.isfile(input_file_path)):
        print(f'Error: invalid path for input file: {input_file_path}')
        sys.exit(1)
    if (input_file_path.split('.')[(- 1)].lower() != 'pdf'):
        print(f'Error: input file is not a PDF: {input_file_path}')
        sys.exit(1)
    gs = get_ghostscript_path()
    file_name = input_file_path.split('/')[(- 1)]
    print('Compressing PDF "{}"...'.format(file_name))
    if (method == 1):
        cmd = [gs, '-sDEVICE=pdfwrite', '-dNumRenderingThreads=2', '-dPDFSETTINGS={}'.format(quality[level]), '-dCompatibilityLevel=1.5', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-dSAFER', '-dSubsetFonts=true', '-dCompressFonts=true', '-dEmbedAllFonts=true', '-sProcessColorModel=DeviceRGB', '-sColorConversionStrategy=RGB', '-sColorConversionStrategyForImages=RGB', '-dConvertCMYKImagesToRGB=true', '-dDetectDuplicateImages=true', '-dColorImageDownsampleType=/Bicubic', '-dColorImageResolution=300', '-dGrayImageDownsampleType=/Bicubic', '-dGrayImageResolution=300', '-dMonoImageDownsampleType=/Subsample', '-dMonoImageResolution=300', '-dDownsampleColorImages=true', '-dDoThumbnails=false', '-dCreateJobTicket=false', '-dPreserveEPSInfo=false', '-dPreserveOPIComments=false', '-dPreserveOverprintSettings=false', '-dUCRandBGInfo=/Remove', '-sOutputFile={}'.format(output_file_path), input_file_path]
    elif (method == 2):
        cmd = [gs, '-sDEVICE=pdfwrite', '-dNumRenderingThreads=2', '-dPDFSETTINGS={}'.format(quality[level]), '-dCompatibilityLevel=1.4', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-dSAFER', '-dDetectDuplicateImages=true', '-sOutputFile={}'.format(output_file_path), input_file_path]
    try:
        subprocess.call(cmd, stderr=sys.stdout)
    except:
        print(' '.join(cmd))
    if (not os.path.exists(output_file_path)):
        raise Exception(f'''Ghostscript failed to create {output_file_path}, time to debug...
''', ' '.join(cmd))
    initial_size = round((os.path.getsize(input_file_path) / (1024 * 1024)), 2)
    final_size = round((os.path.getsize(output_file_path) / (1024 * 1024)), 2)
    ratio = round((100 - ((final_size / initial_size) * 100)), 1)
    print(f'Initial file size is {initial_size}MB', f'; Final file size is {final_size}MB', f'''; Compression Ratio = {ratio}%
''')
    if ((final_size > initial_size) and (method == 1)):
        print(('-' * 100))
        print('Compression Failed\nTrying another ghostscript compression method...')
        print(('-' * 100))
        info = compress(input_file_path, output_file_path, 4, 2)
        initial_size = info[0]
        final_size = info[1]
        ratio = info[2]
    return [initial_size, final_size, ratio]
Function to compress PDF via Ghostscript command line interface
def compress(input_file_path, output_file_path, level=0, method=1):
    
    quality = {0: '/default', 1: '/prepress', 2: '/printer', 3: '/ebook', 4: '/screen'}
    if (not os.path.isfile(input_file_path)):
        print(f'Error: invalid path for input file: {input_file_path}')
        sys.exit(1)
    if (input_file_path.split('.')[(- 1)].lower() != 'pdf'):
        print(f'Error: input file is not a PDF: {input_file_path}')
        sys.exit(1)
    gs = get_ghostscript_path()
    file_name = input_file_path.split('/')[(- 1)]
    print('Compressing PDF "{}"...'.format(file_name))
    if (method == 1):
        cmd = [gs, '-sDEVICE=pdfwrite', '-dNumRenderingThreads=2', '-dPDFSETTINGS={}'.format(quality[level]), '-dCompatibilityLevel=1.5', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-dSAFER', '-dSubsetFonts=true', '-dCompressFonts=true', '-dEmbedAllFonts=true', '-sProcessColorModel=DeviceRGB', '-sColorConversionStrategy=RGB', '-sColorConversionStrategyForImages=RGB', '-dConvertCMYKImagesToRGB=true', '-dDetectDuplicateImages=true', '-dColorImageDownsampleType=/Bicubic', '-dColorImageResolution=300', '-dGrayImageDownsampleType=/Bicubic', '-dGrayImageResolution=300', '-dMonoImageDownsampleType=/Subsample', '-dMonoImageResolution=300', '-dDownsampleColorImages=true', '-dDoThumbnails=false', '-dCreateJobTicket=false', '-dPreserveEPSInfo=false', '-dPreserveOPIComments=false', '-dPreserveOverprintSettings=false', '-dUCRandBGInfo=/Remove', '-sOutputFile={}'.format(output_file_path), input_file_path]
    elif (method == 2):
        cmd = [gs, '-sDEVICE=pdfwrite', '-dNumRenderingThreads=2', '-dPDFSETTINGS={}'.format(quality[level]), '-dCompatibilityLevel=1.4', '-dNOPAUSE', '-dQUIET', '-dBATCH', '-dSAFER', '-dDetectDuplicateImages=true', '-sOutputFile={}'.format(output_file_path), input_file_path]
    try:
        subprocess.call(cmd, stderr=sys.stdout)
    except:
        print(' '.join(cmd))
    if (not os.path.exists(output_file_path)):
        raise Exception(f'Ghostscript failed to create {output_file_path}, time to debug...
', ' '.join(cmd))
    initial_size = round((os.path.getsize(input_file_path) / (1024 * 1024)), 2)
    final_size = round((os.path.getsize(output_file_path) / (1024 * 1024)), 2)
    ratio = round((100 - ((final_size / initial_size) * 100)), 1)
    print(f'Initial file size is {initial_size}MB', f'; Final file size is {final_size}MB', f'; Compression Ratio = {ratio}%
')
    if ((final_size > initial_size) and (method == 1)):
        print(('-' * 100))
        print('Compression Failed\nTrying another ghostscript compression method...')
        print(('-' * 100))
        info = compress(input_file_path, output_file_path, 4, 2)
        initial_size = info[0]
        final_size = info[1]
        ratio = info[2]
    return [initial_size, final_size, ratio]