Skip to content

Commit f5edee0

Browse files
committed
v2.6.1 - Explode - Added -e|--explode option to split a PDF into many files (one per page)
1 parent 44eb839 commit f5edee0

3 files changed

Lines changed: 83 additions & 41 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ nppBackup
1313
## Hidden files
1414
.*
1515

16-
## Random file
16+
## Random stuff
1717
*.pdf
18+
old

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ $ pdfscale -v -v -r 'custom mm 200 300' -f disable -s 0.95 ../mixsync\ manual\ v
133133

134134
## Help info
135135
```
136-
$ ./pdfScale.sh --help
137-
pdfScale.sh v2.6.0
136+
$ ./pdfScale.sh -h
137+
pdfScale.sh v2.6.1
138138
139139
Usage: pdfScale.sh <inFile.pdf>
140140
pdfScale.sh -i <inFile.pdf>
@@ -165,6 +165,7 @@ Parameters:
165165
-n, --no-overwrite
166166
Aborts execution if the output PDF file already exists
167167
By default, the output file will be overwritten
168+
Does NOT work if using --explode
168169
-m, --mode <mode>
169170
Paper size detection mode
170171
Modes: a, adaptive Default mode, tries all the methods below
@@ -175,6 +176,8 @@ Parameters:
175176
s, gs Forces the use of Ghostscript (PS script)
176177
-i, --info <file>
177178
Prints <file> Paper Size information to screen and exits
179+
-e, --explode
180+
Explode (split) outuput PDF into many files (one per page)
178181
-s, --scale <factor>
179182
Changes the scaling factor or forces mixed mode
180183
Defaults: 0.95 (scale mode) / Disabled (resize mode)

pdfScale.sh

Lines changed: 76 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#
2323
################################################################
2424

25-
VERSION="2.6.0"
25+
VERSION="2.6.1"
2626

2727

2828
###################### EXTERNAL PROGRAMS #######################
@@ -36,12 +36,12 @@ MDLSBIN="" # MacOS mdls Binary
3636

3737
##################### ENVIRONMENT SET-UP #######################
3838

39-
LC_MEASUREMENT="C" # To make sure our numbers have .decimals
40-
LC_ALL="C" # Some languages use , as decimal token
39+
LC_MEASUREMENT="C" # To make sure our numbers have .decimals
40+
LC_ALL="C" # Some languages use , as decimal token
4141
LC_CTYPE="C"
4242
LC_NUMERIC="C"
4343

44-
TRUE=0 # Silly stuff
44+
TRUE=0 # Silly stuff
4545
FALSE=1
4646

4747

@@ -57,7 +57,8 @@ INFILEPDF="" # Input PDF file name
5757
OUTFILEPDF="" # Output PDF file name
5858
JUST_IDENTIFY=$FALSE # Flag to just show PDF info
5959
ABORT_ON_OVERWRITE=$FALSE # Flag to abort if OUTFILEPDF already exists
60-
ADAPTIVEMODE=$TRUE # Automatically try to guess best mode
60+
EXPLODE_MODE=$FALSE # Turn explode mode (split) on or off
61+
ADAPTIVE_MODE=$TRUE # Automatically try to guess best mode
6162
AUTOMATIC_SCALING=$TRUE # Default scaling in $SCALE, disabled in resize mode
6263
MODE="" # Which page size detection to use
6364
RESIZE_PAPER_TYPE="" # Pre-defined paper to use
@@ -73,6 +74,7 @@ PGWIDTH="" # Input PDF Page Width
7374
PGHEIGHT="" # Input PDF Page Height
7475
RESIZE_WIDTH="" # Resized PDF Page Width
7576
RESIZE_HEIGHT="" # Resized PDF Page Height
77+
PAGE_RANGE="" # Pages to be processed (feeds -sPageList)
7678

7779
############################# Image resolution (dpi)
7880
IMAGE_RESOLUTION=300 # 300 is /Printer default
@@ -146,15 +148,15 @@ EXIT_INVALID_IMAGE_RESOLUTION=51
146148

147149
# Main function called at the end
148150
main() {
149-
printPDFSizes # may exit here
151+
isJustIdentify && printPDFSizes # may exit here
150152
local finalRet=$EXIT_ERROR
151153

152154
if isMixedMode; then
153155
initMain " Mixed Tasks: Resize & Scale"
154156
local tempFile=""
155157
local tempSuffix="$RANDOM$RANDOM""_TEMP_$RANDOM$RANDOM.pdf"
156158
outputFile="$OUTFILEPDF" # backup outFile name
157-
tempFile="${OUTFILEPDF%.pdf}.$tempSuffix" # set a temp file name
159+
tempFile="${INFILEPDF%.pdf}.$tempSuffix" # set a temp file name
158160
if isFile "$tempFile"; then
159161
printError $'Error! Temporary file name already exists!\n'"File: $tempFile"$'\nAborting execution to avoid overwriting the file.\nPlease Try again...'
160162
exit $EXIT_TEMP_FILE_EXISTS
@@ -214,30 +216,30 @@ initMain() {
214216
isDryRun && sim="TRUE (Simulating)"
215217
vprint " Dry-Run: $sim"
216218
vPrintFileInfo
219+
local exp="Disabled"
220+
isExplodeMode && local exp="Enabled"
221+
vprint " Explode Mode: $exp"
217222
getPageSize
218223
vPrintPageSizes ' Source'
219224
vShowPrintMode
220225
}
221226

222227
# Prints PDF Info and exits with $EXIT_SUCCESS, but only if $JUST_IDENTIFY is $TRUE
223228
printPDFSizes() {
224-
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
225-
VERBOSE=0
226-
printVersion 3 " - Paper Sizes"
227-
getPageSize || initError "Could not get pagesize!"
228-
local paperType="$(getGSPaperName $PGWIDTH $PGHEIGHT)"
229-
isEmpty "$paperType" && paperType="Custom Paper Size"
230-
printf '%s\n' "-------------+-----------------------------"
231-
printf " File | %s\n" "$(basename "$INFILEPDF")"
232-
printf " Paper Type | %s\n" "$paperType"
233-
printf '%s\n' "-------------+-----------------------------"
234-
printf '%s\n' " | WIDTH x HEIGHT"
235-
printf " Points | %+8s x %-8s\n" "$PGWIDTH" "$PGHEIGHT"
236-
printf " Millimeters | %+8s x %-8s\n" "$(pointsToMillimeters $PGWIDTH)" "$(pointsToMillimeters $PGHEIGHT)"
237-
printf " Inches | %+8s x %-8s\n" "$(pointsToInches $PGWIDTH)" "$(pointsToInches $PGHEIGHT)"
238-
exit $EXIT_SUCCESS
239-
fi
240-
return $EXIT_SUCCESS
229+
VERBOSE=0
230+
printVersion 3 " - Paper Sizes"
231+
getPageSize || initError "Could not get pagesize!"
232+
local paperType="$(getGSPaperName $PGWIDTH $PGHEIGHT)"
233+
isEmpty "$paperType" && paperType="Custom Paper Size"
234+
printf '%s\n' "-------------+-----------------------------"
235+
printf " File | %s\n" "$(basename "$INFILEPDF")"
236+
printf " Paper Type | %s\n" "$paperType"
237+
printf '%s\n' "-------------+-----------------------------"
238+
printf '%s\n' " | WIDTH x HEIGHT"
239+
printf " Points | %+8s x %-8s\n" "$PGWIDTH" "$PGHEIGHT"
240+
printf " Millimeters | %+8s x %-8s\n" "$(pointsToMillimeters $PGWIDTH)" "$(pointsToMillimeters $PGHEIGHT)"
241+
printf " Inches | %+8s x %-8s\n" "$(pointsToInches $PGWIDTH)" "$(pointsToInches $PGHEIGHT)"
242+
exit $EXIT_SUCCESS
241243
}
242244

243245

@@ -523,6 +525,10 @@ getOptions() {
523525
JUST_IDENTIFY=$TRUE
524526
shift
525527
;;
528+
-e|--explode|--split)
529+
EXPLODE_MODE=$TRUE
530+
shift
531+
;;
526532
-s|--scale|--setscale|--set-scale)
527533
shift
528534
parseScale "$1"
@@ -659,7 +665,7 @@ getOptions() {
659665

660666
isEmpty "${_optArgs[2]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[2]}" $EXIT_INVALID_OPTION
661667

662-
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
668+
if isJustIdentify; then
663669
isEmpty "${_optArgs[1]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[1]}" $EXIT_INVALID_OPTION
664670
VERBOSE=0 # remove verboseness if present
665671
fi
@@ -673,20 +679,22 @@ getOptions() {
673679

674680
checkDeps
675681

676-
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
682+
if isJustIdentify; then
677683
return $TRUE # no need to get output file, so return already
678684
fi
679685

680686
_tgtFile="${_optArgs[1]}"
681687
local _autoName="${INFILEPDF%.*}" # remove possible stupid extension, like .pDF
688+
local _exSuffix=""
689+
isExplodeMode && _exSuffix='.Page%d'
682690
if isMixedMode; then
683-
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE).SCALED.pdf"
691+
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE).SCALED$_exSuffix.pdf"
684692
elif isResizeMode; then
685-
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE).pdf"
693+
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE)$_exSuffix.pdf"
686694
else
687-
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.SCALED.pdf"
695+
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.SCALED$_exSuffix.pdf"
688696
fi
689-
isNotEmpty "$_tgtFile" && OUTFILEPDF="${_tgtFile%.pdf}.pdf"
697+
isNotEmpty "$_tgtFile" && OUTFILEPDF="${_tgtFile%.pdf}$_exSuffix.pdf"
690698
validateOutFile
691699
}
692700

@@ -1056,32 +1064,32 @@ parseMode() {
10561064
local param="$(lowercase $1)"
10571065
case "${param}" in
10581066
c|catgrep|'cat+grep'|grep|g)
1059-
ADAPTIVEMODE=$FALSE
1067+
ADAPTIVE_MODE=$FALSE
10601068
MODE="CATGREP"
10611069
return $TRUE
10621070
;;
10631071
i|imagemagick|identify)
1064-
ADAPTIVEMODE=$FALSE
1072+
ADAPTIVE_MODE=$FALSE
10651073
MODE="IDENTIFY"
10661074
return $TRUE
10671075
;;
10681076
m|mdls|quartz|mac)
1069-
ADAPTIVEMODE=$FALSE
1077+
ADAPTIVE_MODE=$FALSE
10701078
MODE="MDLS"
10711079
return $TRUE
10721080
;;
10731081
p|pdfinfo)
1074-
ADAPTIVEMODE=$FALSE
1082+
ADAPTIVE_MODE=$FALSE
10751083
MODE="PDFINFO"
10761084
return $TRUE
10771085
;;
10781086
s|gs|ghostscript|ps|postscript)
1079-
ADAPTIVEMODE=$FALSE
1087+
ADAPTIVE_MODE=$FALSE
10801088
MODE="GS"
10811089
return $TRUE
10821090
;;
10831091
a|auto|automatic|adaptive)
1084-
ADAPTIVEMODE=$TRUE
1092+
ADAPTIVE_MODE=$TRUE
10851093
MODE=""
10861094
return $TRUE
10871095
;;
@@ -2014,7 +2022,7 @@ isMixedMode() {
20142022

20152023
# Return $TRUE if adaptive mode is enabled, $FALSE otherwise
20162024
isAdaptiveMode() {
2017-
return $ADAPTIVEMODE
2025+
return $ADAPTIVE_MODE
20182026
}
20192027

20202028
# Return $TRUE if adaptive mode is disabled, $FALSE otherwise
@@ -2023,6 +2031,29 @@ isNotAdaptiveMode() {
20232031
return $TRUE
20242032
}
20252033

2034+
# Return $TRUE if explode mode is enabled, $FALSE otherwise
2035+
isExplodeMode() {
2036+
return $EXPLODE_MODE
2037+
}
2038+
2039+
# Return $TRUE if explode mode is disabled, $FALSE otherwise
2040+
isNotExplodeMode() {
2041+
isExplodeMode && return $FALSE
2042+
return $TRUE
2043+
}
2044+
2045+
# Return $TRUE if we should just print PDF info, $FALSE otherwise
2046+
isJustIdentify() {
2047+
return $JUST_IDENTIFY
2048+
}
2049+
2050+
# Return $TRUE if we are not just printing PDF info, $FALSE otherwise
2051+
isNotJustIdentify() {
2052+
isJustIdentify && return $FALSE
2053+
return $TRUE
2054+
}
2055+
2056+
20262057

20272058
########################## VALIDATORS ##########################
20282059

@@ -2123,7 +2154,11 @@ isNotDir() {
21232154

21242155
# Returns 0 if succeded, other integer otherwise
21252156
isTouchable() {
2126-
touch "$1" 2>/dev/null
2157+
if touch "$1" 2>/dev/null; then
2158+
rm "$1" 2>/dev/null
2159+
return $EXIT_SUCCESS
2160+
fi
2161+
RETURN $EXIT_ERROR
21272162
}
21282163

21292164
# Returns $TRUE if $1 has a .pdf extension, false otherwsie
@@ -2266,6 +2301,7 @@ Parameters:
22662301
-n, --no-overwrite
22672302
Aborts execution if the output PDF file already exists
22682303
By default, the output file will be overwritten
2304+
Does NOT work if using --explode
22692305
-m, --mode <mode>
22702306
Paper size detection mode
22712307
Modes: a, adaptive Default mode, tries all the methods below
@@ -2276,6 +2312,8 @@ Parameters:
22762312
s, gs Forces the use of Ghostscript (PS script)
22772313
-i, --info <file>
22782314
Prints <file> Paper Size information to screen and exits
2315+
-e, --explode
2316+
Explode (split) outuput PDF into many files (one per page)
22792317
-s, --scale <factor>
22802318
Changes the scaling factor or forces mixed mode
22812319
Defaults: $SCALE (scale mode) / Disabled (resize mode)

0 commit comments

Comments
 (0)