diff --git a/README.md b/README.md index 185ab2e..3716e8b 100644 --- a/README.md +++ b/README.md @@ -5,65 +5,6 @@ A Python script that simplifies compressing pdf files with gs. -## Change Log - -### MAY 10th, 2020 - v1.2 Released - Dragonfly - -Enhancements: - - - Use Python3 instead - - Progress Bar: - - ``` - ./cpdf ebook ~/Test/AppUserFlow_20180315.pdf ~/Test/compressed.pdf - - WARNING: "/home/hkdb/Test/compressed.pdf" already exists. Are you sure you want to overwrite it? (y/n) y - - Compressing... - 0% [##############################] 100% | ETA: 00:00:00 - Total time elapsed: 00:01:36 - Title: Compressing... - Started: 07/29/2018 20:45:50 - Finished: 07/29/2018 20:47:26 - Total time elapsed: 00:01:36 - - Compressed! - - AppUserFlow_20180315.pdf is 30MB in size. - compressed.pdf is 4.0MB after compression. - ``` - -### MAY 15th, 2018 - v1.1.2 Released - Butterfly - Hotfix 2 - -Bug Fixes: - -- Did not bump version number in version check function of v1.1.1 - -Enhancement: - -- Added version number in header comment of code - -### MAY 14th, 2018 - v1.1.1 Released - Butterfly - Hotfix - -Bug Fixes: - -- Did not include command building step for situations with no WARNINGS. - -### MAY 14th, 2018 - v1.1 Released - Butterfly - -Features: - -- Bolded and colored formating of output text to make it more readable -- Added new lines bewteen each output to make it more readable -- Added Error Handling (WARNING): Output file does not end with .pdf, verify with user that's really what they want -- Added Error Handling (WARNING): Output file name matches a file in the output directory -- Added Error Handling (ERROR): Unsafe input & output file names -- Added a version check argument - -### MAY 11th, 2018 - v1.0 Released - Birth - -- The birth of cPDF! - ## Usage Example Check file size of test.pdf: @@ -112,12 +53,18 @@ hkdb@machine:~/test$ ``` Notice that compressed.pdf is 4M; the results from compressing a 31M pdf? I then opened up compressed.pdf and it still looked great! +You can also specify input and output files with full path: + +``` +hkdb@machine:~/test$ cpdf ebook /home/hkdb/test.pdf /home/hkdb/Downloads/test-compressed.pdf +``` + Version Checker: ``` hkdb@machine:~/test$ cpdf version -cPDF v1.1 +cPDF v1.3 hkdb@machine:~/test$ ``` @@ -148,8 +95,8 @@ Ghostscript: - Input File Does Not End with .pdf - Input File and Output File are the same -- Input File Name Contains Unsupported Characters(/\\:;\`) -- Output File Name Contains Unsupported Characters(/\\:;\`) +- Input File Name Contains Unsupported Characters(| ; ` > < } { # *) +- Output File Name Contains Unsupported Characters(| ; ` > < } { # *) Questionable Conditions that the application will verify with User via A Dialog Message: @@ -158,10 +105,89 @@ Questionable Conditions that the application will verify with User via A Dialog ## Installation -To make this script behave more like a command, move it to a bin directory of choice that's defined in your env. +To make this script behave more like a command, copy cpdf to a bin directory of choice that's defined in your $PATH. This way, you can be in any directory and run this script to compress any pdf file without specifying the full path. +If you are on Linux, *BSD, or Mac, you can also run the install script: + +``` +git clone https://github.com/hkdb/cpdf.git +cd cpdf +./install.sh +``` +You will still have to manually ensure ~/.local/bin is in $PATH + + +## Change Log + +### JUNE 24th, 2024 - v1.3 Released - Bee + +- Use /usr/bin/env python3 header instead for better compatibility +- Added a check for whether or not the input file exists +- Allow for full path to be specified for both input and output file +- Adjusted illegal characters +- Adjusted help output to be more readable +- Added install script for Linux, *BSD, macos + + +### MAY 10th, 2020 - v1.2 Released - Dragonfly + +Enhancements: + + - Use Python3 instead + - Progress Bar: + + ``` + ./cpdf ebook ~/Test/AppUserFlow_20180315.pdf ~/Test/compressed.pdf + + WARNING: "/home/hkdb/Test/compressed.pdf" already exists. Are you sure you want to overwrite it? (y/n) y + + Compressing... + 0% [##############################] 100% | ETA: 00:00:00 + Total time elapsed: 00:01:36 + Title: Compressing... + Started: 07/29/2018 20:45:50 + Finished: 07/29/2018 20:47:26 + Total time elapsed: 00:01:36 + + Compressed! + + AppUserFlow_20180315.pdf is 30MB in size. + compressed.pdf is 4.0MB after compression. + ``` + +### MAY 15th, 2018 - v1.1.2 Released - Butterfly - Hotfix 2 + +Bug Fixes: + +- Did not bump version number in version check function of v1.1.1 + +Enhancement: + +- Added version number in header comment of code + +### MAY 14th, 2018 - v1.1.1 Released - Butterfly - Hotfix + +Bug Fixes: + +- Did not include command building step for situations with no WARNINGS. + +### MAY 14th, 2018 - v1.1 Released - Butterfly + +Features: + +- Bolded and colored formating of output text to make it more readable +- Added new lines bewteen each output to make it more readable +- Added Error Handling (WARNING): Output file does not end with .pdf, verify with user that's really what they want +- Added Error Handling (WARNING): Output file name matches a file in the output directory +- Added Error Handling (ERROR): Unsafe input & output file names +- Added a version check argument + +### MAY 11th, 2018 - v1.0 Released - Birth + +- The birth of cPDF! + ## Disclaimer This application is maintained by volunteers and in no way do the maintainers make any guarantees. Please use at your own risk! diff --git a/cpdf b/cpdf index 24961ee..71327ce 100755 --- a/cpdf +++ b/cpdf @@ -1,4 +1,4 @@ -#!/usr/bin/python3 +#!/usr/bin/env python3 ############################################################### ### PROJECT: @@ -6,7 +6,7 @@ ### SCRIPT: ### cpdf ### VERSION: -### v1.2 +### v1.3 ### DESCRIPTION: ### A script to make compressing pdf files more easily by ### automating gs commands @@ -22,7 +22,7 @@ import os, datetime, sys, glob, re, subprocess, pyprind, time if len(sys.argv) <= 1: print("\n" + '\033[1;37m' + "ERROR:" + '\033[0m' + " This script only takes 3 specific arguments. Use the \"help\" argument to learn more.\n") elif str(sys.argv[1]) == "help": - print("\nThis is a script to compress a pdf file with the convention of: " + '\033[1;37m' + "cpdf [type: screen, ebook, printer, prepress, or default] [input file name] [output file name]" + '\033[0m' + ". To learn more about the different types, use the \"types\" flag (ie. cpdf types)\n") + print("\nThis is a script to compress a pdf file with the convention of:\n\n" + '\033[1;37m' + "cpdf [type: screen, ebook, printer, prepress, or default] [input file name] [output file name]" + '\033[0m' + "\n\nTo learn more about the different types, use the \"types\" flag (ie. cpdf types)\n") sys.exit() elif str(sys.argv[1]) == "types": print("\n") @@ -34,7 +34,7 @@ elif str(sys.argv[1]) == "types": print("\n") sys.exit() elif str(sys.argv[1]) == "version": - print('\033[1;37m' + "\ncPDF v1.2\n" + '\033[0m') + print('\033[1;37m' + "\ncPDF v1.3\n" + '\033[0m') sys.exit() elif len(sys.argv) != 4: print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " This script only takes 3 specific arguments. Use the \"help\" argument to learn more.\n") @@ -54,15 +54,21 @@ else: cType = str(sys.argv[1]) # Compression Type inFile = str(sys.argv[2]) # Input File Name outFile = str(sys.argv[3]) # Output File Name + + # Check to make sure input file exists + exist = os.path.isfile(inFile) + if exist == False: + print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " " + inFile + " does not exist... Exiting...\n") + sys.exit() # Check to make sure that the input file name is safe - if re.search('[\\\\\|:;\`]', inFile): - print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " Input file contains invalid characters such as / \\ : ; \`.... Exiting...\n") + if re.search('[|;\`><{}#*]', inFile): + print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " Input file contains invalid characters such as | ; \` > < } { # *.... Exiting...\n") sys.exit() # Check to make sure that the output file name is safe - if re.search('[\\\\\|:;\`]', outFile): - print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " Output file contains invalid characters such as / \\ : ; \`.... Exiting...\n") + if re.search('[|;\`><{}#*]', outFile): + print("\n" + '\033[91m' + "ERROR:" + '\033[0m' + " Output file contains invalid characters such as | ; \` > < } { # *.... Exiting...\n") sys.exit() # Check to make sure the user really wants to overwrite the existing file with the new output file and that the output file does in fact end with .pdf diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..7c87cc0 --- /dev/null +++ b/install.sh @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +echo -e "\n" + +echo -e "✅️ Making sure there's a $HOME/.local/bin...\n" +if [[ ! -d "$HOME/.local/bin" ]]; then + mkdir -p $HOME/.local/bin + if [[ $? -ne 0 ]] ; then + echo -e "\n❌️ Failed to create $HOME/.local/bin... Exiting...\n" + exit 1 + fi +fi + +echo -e "\n💾️ Copying cpdf to $HOME/.local/bin...\n" +cp cpdf $HOME/.local/bin + + +echo -e "\n${GREEN}**************" +echo -e " 💯️ COMPLETED" +echo -e "**************${NC}\n" + +echo -e "⚠️ Make sure $HOME/.local/bin is already in \$PATH...\n"