Skip to content

Commit

Permalink
Two new options. --dry-run to simulate execution without calling ghos…
Browse files Browse the repository at this point in the history
…tscript; --print-gs-call to print Ghostscript call to stdout; Using both one can just create the GS call.
  • Loading branch information
tavinus committed Apr 1, 2018
1 parent a1881ab commit d92a457
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 14 deletions.
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ $ pdfscale -v -v -r 'custom mm 200 300' -f disable -s 0.95 ../mixsync\ manual\ v
## Help info
```
$ pdfscale --help
pdfscale v2.2.0
pdfscale v2.2.1
Usage: pdfscale <inFile.pdf>
pdfscale -i <inFile.pdf>
Expand Down Expand Up @@ -161,19 +161,19 @@ Parameters:
n, none Retains orientation of each page
a, all Rotates all pages (or none) depending
on a kind of "majority decision"
--hor-align,--horizontal-alignment <left|center|right>
--hor-align, --horizontal-alignment <left|center|right>
Where to translate the scaled page
Default: center
Options: left, right, center
--vert-align,--vertical-alignment <top|center|bottom>
--vert-align, --vertical-alignment <top|center|bottom>
Where to translate the scaled page
Default: center
Options: top, bootom, center
--xoffset,--xtrans-offset <FloatNumber>
--xoffset, --xtrans-offset <FloatNumber>
Add/Subtract from the X translation (move left-right)
Default: 0.0 (zero)
Options: Positive or negative floating point number
--yoffset,--ytrans-offset <FloatNumber>
--yoffset, --ytrans-offset <FloatNumber>
Add/Subtract from the Y translation (move top-bottim)
Default: 0.0 (zero)
Options: Positive or negative floating point number
Expand All @@ -188,6 +188,10 @@ Parameters:
--image-resolution <dpi>
Resolution in DPI of color and grayscale images in output
Default: 300
--dry-run, --simulate
Just simulate execution. Will not run ghostscript
--print-gs-call, --gs-call
Print GS call to stdout. Will print at the very end between markers
-p, --print-papers
Prints Standard Paper info tables to screen and exits
Expand Down
103 changes: 94 additions & 9 deletions pdfScale.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# And: https://gist.github.com/MichaelJCole/86e4968dbfc13256228a


VERSION="2.2.0"
VERSION="2.2.1"


###################### EXTERNAL PROGRAMS #######################
Expand Down Expand Up @@ -85,6 +85,11 @@ HOR_ALIGN="CENTER"
XTRANSOFFSET=0.0
YTRANSOFFSET=0.0

############################# Execution Flags
SIMULATE=$FALSE # Avoid execution
PRINT_GS_CALL=$FALSE # Print GS Call to stdout
GS_CALL_STRING="" # Buffer

########################## EXIT FLAGS ##########################

EXIT_SUCCESS=0
Expand Down Expand Up @@ -135,7 +140,9 @@ main() {
pageScale # scale the resized pdf
finalRet=$(($finalRet+$?))
# remove tmp file
isFile "$tempFile" && rm "$tempFile" >/dev/null 2>&1 || printError "Error when removing temporary file: $tempFile"
if isFile "$tempFile"; then
rm "$tempFile" >/dev/null 2>&1 || printError "Error when removing temporary file: $tempFile"
fi
elif isResizeMode; then
initMain " Single Task: Resize PDF Paper"
vPrintScaleFactor "Disabled (resize only)"
Expand All @@ -151,11 +158,19 @@ main() {
fi

if [[ $finalRet -eq $EXIT_SUCCESS ]] && isEmpty "$GS_RUN_STATUS"; then
vprint " Final Status: File created successfully"
if isDryRun; then
vprint " Final Status: Simulation completed successfully"
else
vprint " Final Status: File created successfully"
fi
else
vprint " Final Status: Error detected. Exit status: $finalRet"
printError "PdfScale: ERROR!"$'\n'"Ghostscript Debug Info:"$'\n'"$GS_RUN_STATUS"
fi

if isNotEmpty "$GS_CALL_STRING" && shouldPrintGSCall; then
printf "%s" "$GS_CALL_STRING"
fi

return $finalRet
}
Expand All @@ -164,6 +179,9 @@ main() {
initMain() {
printVersion 1 'verbose'
isNotEmpty "$1" && vprint "$1"
local sim="FALSE"
isDryRun && sim="TRUE (Simulating)"
vprint " Dry-Run: $sim"
vPrintFileInfo
getPageSize
vPrintPageSizes ' Source'
Expand Down Expand Up @@ -223,11 +241,15 @@ pageScale() {
vprint " Run Scaling: $increase %"

GS_RUN_STATUS="$GS_RUN_STATUS""$(gsPageScale 2>&1)"
GS_CALL_STRING="$GS_CALL_STRING"$'[GS SCALE CALL STARTS]\n'"$(gsPrintPageScale)"$'\n[GS SCALE CALL ENDS]\n'
return $? # Last command is always returned I think
}

# Runs GS call for scaling, nothing else should run here
gsPageScale() {
if isDryRun; then
return $TRUE
fi
# Scale page
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
Expand All @@ -240,7 +262,25 @@ gsPageScale() {
-sOutputFile="$OUTFILEPDF" \
-c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \
-f "$INFILEPDF"
return $?

}

# Prints GS call for scaling
gsPrintPageScale() {
# Print Scale page command
cat << _EOF_
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \
-dColorImageResolution=$IMAGE_RESOLUTION -dGrayImageResolution=$IMAGE_RESOLUTION \
-dColorImageDownsampleType="$IMAGE_DOWNSAMPLE_TYPE" -dGrayImageDownsampleType="$IMAGE_DOWNSAMPLE_TYPE" \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dSubsetFonts=true -dEmbedAllFonts=true \
-dDEVICEWIDTHPOINTS=$PGWIDTH -dDEVICEHEIGHTPOINTS=$PGHEIGHT \
-sOutputFile="$OUTFILEPDF" \
-c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \
-f "$INFILEPDF"
_EOF_
}

# Runs the ghostscript paper resize script
Expand All @@ -253,11 +293,15 @@ pageResize() {
runFlipDetect
vprint " Run Resizing: $(uppercase "$RESIZE_PAPER_TYPE") ( "$RESIZE_WIDTH" x "$RESIZE_HEIGHT" ) pts"
GS_RUN_STATUS="$GS_RUN_STATUS""$(gsPageResize 2>&1)"
GS_CALL_STRING="$GS_CALL_STRING"$'[GS RESIZE CALL STARTS]\n'"$(gsPrintPageScale)"$'\n[GS RESIZE CALL ENDS]\n'
return $?
}

# Runs GS call for resizing, nothing else should run here
gsPageResize() {
if isDryRun; then
return $TRUE
fi
# Change page size
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
Expand All @@ -270,10 +314,29 @@ gsPageResize() {
-dAutoRotatePages=$AUTO_ROTATION \
-dFIXEDMEDIA -dPDFFitPage \
-sOutputFile="$OUTFILEPDF" \
-f "$INFILEPDF"
-f "$INFILEPDF"
return $?
}

# Prints GS call for resizing
gsPrintPageResize() {
# Print Resize page command
cat << _EOF_
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="$PDF_SETTINGS" \
-dColorImageResolution=$IMAGE_RESOLUTION -dGrayImageResolution=$IMAGE_RESOLUTION \
-dColorImageDownsampleType="$IMAGE_DOWNSAMPLE_TYPE" -dGrayImageDownsampleType="$IMAGE_DOWNSAMPLE_TYPE" \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dSubsetFonts=true -dEmbedAllFonts=true \
-dDEVICEWIDTHPOINTS=$RESIZE_WIDTH -dDEVICEHEIGHTPOINTS=$RESIZE_HEIGHT \
-dAutoRotatePages=$AUTO_ROTATION \
-dFIXEDMEDIA -dPDFFitPage \
-sOutputFile="$OUTFILEPDF" \
-f "$INFILEPDF"
_EOF_
}

# Returns $TRUE if we should use the source paper size, $FALSE otherwise
isResizePaperSource() {
[[ "$RESIZE_PAPER_TYPE" = 'source' ]] && return $TRUE
Expand Down Expand Up @@ -465,6 +528,14 @@ getOptions() {
parseYTransOffset "$1"
shift
;;
--simulate|--dry-run)
SIMULATE=$TRUE
shift
;;
--print-gs-call|--gs-call)
PRINT_GS_CALL=$TRUE
shift
;;
*)
initError "Invalid Parameter: \"$1\"" $EXIT_INVALID_OPTION
;;
Expand Down Expand Up @@ -517,6 +588,16 @@ isAbortOnOverwrite() {
return $ABORT_ON_OVERWRITE
}

# Returns $TRUE if we should print the GS call to stdout
shouldPrintGSCall() {
return $PRINT_GS_CALL
}

# Returns $TRUE if we are simulating, dry-run (no GS execution)
isDryRun() {
return $SIMULATE
}

# Parses and validates the scaling factor
parseScale() {
AUTOMATIC_SCALING=$FALSE
Expand Down Expand Up @@ -1494,19 +1575,19 @@ Parameters:
n, none Retains orientation of each page
a, all Rotates all pages (or none) depending
on a kind of \"majority decision\"
--hor-align,--horizontal-alignment <left|center|right>
--hor-align, --horizontal-alignment <left|center|right>
Where to translate the scaled page
Default: center
Options: left, right, center
--vert-align,--vertical-alignment <top|center|bottom>
--vert-align, --vertical-alignment <top|center|bottom>
Where to translate the scaled page
Default: center
Options: top, bootom, center
--xoffset,--xtrans-offset <FloatNumber>
--xoffset, --xtrans-offset <FloatNumber>
Add/Subtract from the X translation (move left-right)
Default: 0.0 (zero)
Options: Positive or negative floating point number
--yoffset,--ytrans-offset <FloatNumber>
--yoffset, --ytrans-offset <FloatNumber>
Add/Subtract from the Y translation (move top-bottim)
Default: 0.0 (zero)
Options: Positive or negative floating point number
Expand All @@ -1521,6 +1602,10 @@ Parameters:
--image-resolution <dpi>
Resolution in DPI of color and grayscale images in output
Default: 300
--dry-run, --simulate
Just simulate execution. Will not run ghostscript
--print-gs-call, --gs-call
Print GS call to stdout. Will print at the very end between markers
-p, --print-papers
Prints Standard Paper info tables to screen and exits
Expand Down

0 comments on commit d92a457

Please sign in to comment.