Skip to content

Commit

Permalink
v1.3 features/fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
hkdb committed Jun 24, 2024
1 parent 8f8131b commit 6b017db
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 71 deletions.
152 changes: 89 additions & 63 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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$
```
Expand Down Expand Up @@ -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:

Expand All @@ -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!
Expand Down
22 changes: 14 additions & 8 deletions cpdf
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#!/usr/bin/python3
#!/usr/bin/env python3

###############################################################
### PROJECT:
### cpdf - The PDF Compressor
### SCRIPT:
### cpdf
### VERSION:
### v1.2
### v1.3
### DESCRIPTION:
### A script to make compressing pdf files more easily by
### automating gs commands
Expand All @@ -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")
Expand All @@ -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")
Expand All @@ -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
Expand Down
22 changes: 22 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 6b017db

Please sign in to comment.