(data)
take a pdf data string, return a compressed string
        compression is done using ps2pdf14 in ghostscript
take a pdf data string, return a compressed string
        compression is done using ps2pdf14 in ghostscript
take a pdf data string return a compressed string compression is done using ps2pdf14 in ghostscript
def pdf_compress(data):
    """ take a pdf data string, return a compressed string
        compression is done using ps2pdf14 in ghostscript
    """
    if os.name == 'nt':
        log_info("Does not support pdf compress on windows!")
        return data
    f = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf')
    f.write(data)
    f.close()

    f2 = tempfile.NamedTemporaryFile(delete=False, suffix='.pdf')
    f2.close()
    ret = os.system('ps2pdf14 "{0}" "{1}"'.format(f.name, f2.name))
    if ret != 0:
        log_err("Compress: ps2pdf14 failed!")
        newdata = None
    else:
        newdata = open(f2.name).read()
    file_succ = newdata is not None and \
            check_file_type(f2.name, 'PDF document') and \
            len(newdata) >= ukconfig.FILE_SIZE_MINIMUM
    try:
        os.remove(f2.name)
        os.remove(f.name)
    except OSError:
        pass
    if file_succ and \
       len(newdata) < len(data):
        log_info("Compress succeed: {0}->{1}".format(
            parse_file_size(len(data)), parse_file_size(len(newdata))))
        return newdata
    else:
        return data
def pdf_compress ( data ) : if os . name == 'nt' : log_info ( "Does not support pdf compress on windows!" ) return data f = tempfile . NamedTemporaryFile ( delete = False , suffix = '.pdf' ) f . write ( data ) f . close ( ) f2 = tempfile . NamedTemporaryFile ( delete = False , suffix = '.pdf' ) f2 . close ( ) ret = os . system ( 'ps2pdf14 "{0}" "{1}"' . format ( f . name , f2 . name ) ) if ret != 0 : log_err ( "Compress: ps2pdf14 failed!" ) newdata = None else : newdata = open ( f2 . name ) . read ( ) file_succ = newdata is not None and check_file_type ( f2 . name , 'PDF document' ) and len ( newdata ) >= ukconfig . FILE_SIZE_MINIMUM try : os . remove ( f2 . name ) os . remove ( f . name ) except OSError : pass if file_succ and len ( newdata ) < len ( data ) : log_info ( "Compress succeed: {0}->{1}" . format ( parse_file_size ( len ( data ) ) , parse_file_size ( len ( newdata ) ) ) ) return newdata else : return data