def compress_file(file_name): try: compressed_file_name = "{}_compressed.pdf".format(file_name[:-4]) compress(file_name, compressed_file_name, power=4) return compressed_file_name except: return file_name
def upload_file(): if request.method == 'POST': f = request.files['file'] print('file', f) quality = request.form['quality'] f.save(os.path.join(os.getcwd(), 'file.pdf')) compress('file.pdf', 'compressed.pdf', int(quality)) return send_file('compressed.pdf', attachment_filename=f.filename)
def creatUser(userDetails): response = {} home = os.environ['USERPROFILE'] checkDir(E9PASS_BACKUP) certificateFilesPath = os.path.join(home, E9PASS_USER) pdfFilesPath = os.path.join(home, E9PASS_PDF) backUpDir = os.path.join(home, E9PASS_BACKUP) try: arcNumber = userDetails["arcNumber"] certificateName = userDetails["certificateName"] table = str.maketrans('', '', string.punctuation) folderName = arcNumber.translate(table) path = pathlib.Path( os.path.join(backUpDir, folderName + '\\NPKI\\KICA\\USER\\' + certificateName)) path.mkdir(parents=True, exist_ok=True) destination = os.path.join( backUpDir, folderName + '\\NPKI\\KICA\\USER\\' + certificateName) source = os.path.join(certificateFilesPath, certificateName) copyAndOverwrite(source, destination) response["certStatus"] = True response["certMsg"] = 'Success' try: pdfName = userDetails["pdfName"] source = os.path.join(pdfFilesPath, pdfName) destination = os.path.join(backUpDir, os.path.join(folderName, pdfName)) result = None result = compress(source, destination, power=2) while result is None: pass response["pdfStatus"] = result response["pdfMsg"] = 'PDF Compress Status' except: response["pdfStatus"] = False response["pdfMsg"] = 'Unable to copy Pdf!' try: os.chdir(backUpDir) table = str.maketrans('', '', string.punctuation) fileName = arcNumber.translate(table) result = shutil.make_archive(fileName, 'zip', base_dir=folderName) if bool(result): shutil.rmtree(folderName) response["zipStatus"] = True response["zipMsg"] = 'Success' return response except: response["zipStatus"] = False response["zipMsg"] = 'Unable to Create ZIP Archive' return response except OSError as err: print(err) response["certStatus"] = False response["certMsg"] = 'Unable to copy Certificate!' return response
def compress_file(file_name): compressed_file_name = "{}_compressed.pdf".format(file_name) compress(file_name, compressed_file_name, power=4) return compressed_file_name
from pdf_compressor import compress print("Hello from inside the container!") compress("Hindustan Times 04072020.pdf", "compressed.pdf", power=4)