From a5b62f0ae11fa2ce6b00218347f8ba862f451c81 Mon Sep 17 00:00:00 2001 From: ungeschneuer <33438600+ungeschneuer@users.noreply.github.com> Date: Thu, 15 Apr 2021 13:54:49 +0200 Subject: [PATCH 1/3] Add directory search Walks trough directories and looks for all possible PDF Signed-off-by: ungeschneuer <33438600+ungeschneuer@users.noreply.github.com> --- README.md | 20 +++++++------ pdf_compressor.py | 75 ++++++++++++++++++++++++++++++++++------------- 2 files changed, 65 insertions(+), 30 deletions(-) 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..4bddc71 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,50 @@ 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: + os.remove(output_file_path) + print("Compression not sucessfull") + # 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")) + 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 = "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, 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 +111,24 @@ def main(): if not args.out: args.out = 'temp.pdf' - # Run - compress(args.input, args.out, power=args.compress) + 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) + - # 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 __name__ == '__main__': main() From 4c7203ca74d8fb194160fdb4b884ec5945de92cb Mon Sep 17 00:00:00 2001 From: ungeschneuer <33438600+ungeschneuer@users.noreply.github.com> Date: Thu, 15 Apr 2021 13:55:40 +0200 Subject: [PATCH 2/3] Added info to the terminal description Signed-off-by: ungeschneuer <33438600+ungeschneuer@users.noreply.github.com> --- pdf_compressor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pdf_compressor.py b/pdf_compressor.py index 4bddc71..dd90b42 100755 --- a/pdf_compressor.py +++ b/pdf_compressor.py @@ -96,7 +96,7 @@ def main(): description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) - parser.add_argument('input', help='Relative or absolute path of the input PDF file') + parser.add_argument('input', help='Relative or absolute path of the input PDF file or directory') parser.add_argument('-o', '--out', help='Relative or absolute path of the output PDF file') parser.add_argument('-c', '--compress', type=int, help='Compression level from 0 to 4') parser.add_argument('-b', '--backup', action='store_true', help="Backup the old PDF file") From a958af7980d97402a47d4ca366a13883d13e14a3 Mon Sep 17 00:00:00 2001 From: ungeschneuer <33438600+ungeschneuer@users.noreply.github.com> Date: Thu, 15 Apr 2021 16:11:57 +0200 Subject: [PATCH 3/3] Fix Backup Bug --- pdf_compressor.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/pdf_compressor.py b/pdf_compressor.py index dd90b42..f5f25c9 100755 --- a/pdf_compressor.py +++ b/pdf_compressor.py @@ -57,13 +57,14 @@ def compress(input_file_path, output_file_path, backup, power=0): final_size = os.path.getsize(output_file_path) # Check if compressing was resourceful - if final_size > initial_size: + if (final_size > initial_size) or (final_size == initial_size): os.remove(output_file_path) - print("Compression not sucessfull") + 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) @@ -75,7 +76,7 @@ def compress(input_file_path, output_file_path, backup, power=0): def compress_directory(input_dir_path, backup, power=0): # Default File Path - output_file_path = "temp.pdf" + output_file_path = os.path.join(input_dir_path, "temp.pdf") # The extension to search for exten = '.pdf' @@ -85,7 +86,7 @@ def compress_directory(input_dir_path, backup, power=0): for name in files: if name.lower().endswith(exten): file_path = os.path.join(dirpath, name) - compress(file_path, output_file_path, power) + compress(file_path, output_file_path, backup, power) for dirname in dirnames: new_dir_path = os.path.join(dirpath, dirname) @@ -96,7 +97,7 @@ def main(): description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter ) - parser.add_argument('input', help='Relative or absolute path of the input PDF file or directory') + parser.add_argument('input', help='Relative or absolute path of the input PDF file') parser.add_argument('-o', '--out', help='Relative or absolute path of the output PDF file') parser.add_argument('-c', '--compress', type=int, help='Compression level from 0 to 4') parser.add_argument('-b', '--backup', action='store_true', help="Backup the old PDF file") @@ -111,6 +112,7 @@ def main(): if not args.out: args.out = 'temp.pdf' + if os.path.isdir(args.input): compress_directory(args.input, args.backup, power=args.compress) elif os.path.isfile(args.input):