diff --git a/README.md b/README.md index 1945d69..4fca84f 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Pdfc -- PDF Compressor Simple python script to compress PDF. Installation -------------- +---- * Install dependency Ghostscript. On MacOSX: `brew install ghostscript` On Windows: install binaries via [official website] (https://www.ghostscript.com/) @@ -14,23 +14,25 @@ On Windows: install binaries via [official website] (https://www.ghostscript.com On MacOSX: `echo export=/absolute/path/of/the/folder/script/:$PATH >> ~/.bash_profile` +Or with ZSH +`echo path+=absolute/path/of/the/folder/script/ >> ~/.zshrc` + Usage ------ +---- `pdfc [-o output_file_path] [-c number] input_file_path` Ex: `pdfc -o out.pdf in.pdf` Output: -``` -Compress PDF... -Compression by 65%. -Final file size is 1.4MB -Done. -``` + Compress PDF... + Compression by 65%. + Final file size is 1.4MB + Done. +When compression did not work, the file is skipped. Options -------- +---- * `-c` or `--compress` specifies 5 levels of compression, similar to standard pdf generator level: * 0: default * 1: prepress diff --git a/pdf_compressor.py b/pdf_compressor.py index 40a25ad..f5f25c9 100755 --- a/pdf_compressor.py +++ b/pdf_compressor.py @@ -24,7 +24,7 @@ from shutil import copyfile -def compress(input_file_path, output_file_path, power=0): +def compress(input_file_path, output_file_path, backup, power=0): """Function to compress PDF via Ghostscript command line interface""" quality = { 0: '/default', @@ -46,19 +46,51 @@ def compress(input_file_path, output_file_path, power=0): sys.exit(1) print("Compress PDF...") - initial_size = os.path.getsize(input_file_path) subprocess.call(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-dPDFSETTINGS={}'.format(quality[power]), '-dNOPAUSE', '-dQUIET', '-dBATCH', '-sOutputFile={}'.format(output_file_path), input_file_path] ) + + initial_size = os.path.getsize(input_file_path) final_size = os.path.getsize(output_file_path) - ratio = 1 - (final_size / initial_size) - print("Compression by {0:.0%}.".format(ratio)) - print("Final file size is {0:.1f}MB".format(final_size / 1000000)) - print("Done.") + # Check if compressing was resourceful + if (final_size > initial_size) or (final_size == initial_size): + os.remove(output_file_path) + print("Skipped") + # In case no output file is specified, erase original file + elif output_file_path == 'temp.pdf': + if backup: + copyfile(input_file_path, input_file_path.replace(".pdf", "_BACKUP.pdf")) + os.remove(input_file_path) + copyfile(output_file_path, input_file_path) + os.remove(output_file_path) + + ratio = 1 - (final_size / initial_size) + print("Compression by {0:.0%}.".format(ratio)) + print("Final file size is {0:.1f}MB".format(final_size / 1000000)) + print("Done.") + +def compress_directory(input_dir_path, backup, power=0): + + # Default File Path + output_file_path = os.path.join(input_dir_path, "temp.pdf") + + # The extension to search for + exten = '.pdf' + + for dirpath, dirnames, files in os.walk(input_dir_path): + + for name in files: + if name.lower().endswith(exten): + file_path = os.path.join(dirpath, name) + compress(file_path, output_file_path, backup, power) + + for dirname in dirnames: + new_dir_path = os.path.join(dirpath, dirname) + compress_directory(new_dir_path, backup, power) def main(): parser = argparse.ArgumentParser( @@ -80,22 +112,25 @@ def main(): if not args.out: args.out = 'temp.pdf' - # Run - compress(args.input, args.out, power=args.compress) - # In case no output file is specified, erase original file - if args.out == 'temp.pdf': - if args.backup: - copyfile(args.input, args.input.replace(".pdf", "_BACKUP.pdf")) - copyfile(args.out, args.input) - os.remove(args.out) - - # In case we want to open the file after compression - if args.open: - if args.out == 'temp.pdf' and args.backup: - subprocess.call(['open', args.input]) - else: - subprocess.call(['open', args.out]) + if os.path.isdir(args.input): + compress_directory(args.input, args.backup, power=args.compress) + elif os.path.isfile(args.input): + # Run + compress(args.input, args.out, args.backup, power=args.compress) + + # In case we want to open the file after compression + if args.open: + if args.out == 'temp.pdf' and args.backup: + subprocess.call(['open', args.input]) + else: + subprocess.call(['open', args.out]) + + else: + print("Error: invalid path for input PDF file") + sys.exit(1) + + if __name__ == '__main__': main()