Skip to content

Commit c45f788

Browse files
committed
v2.6.0 - Added check for strings binary (binutils); Added Ghostscript (PS) method for page size retrieval both for manual mode and adaptive mode; Added gs method mode to help info
1 parent 971e83f commit c45f788

2 files changed

Lines changed: 64 additions & 13 deletions

File tree

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ nppBackup
1212

1313
## Hidden files
1414
.*
15+
16+
## Random file
17+
*.pdf

pdfScale.sh

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

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

2727

2828
###################### EXTERNAL PROGRAMS #######################
@@ -227,14 +227,14 @@ printPDFSizes() {
227227
getPageSize || initError "Could not get pagesize!"
228228
local paperType="$(getGSPaperName $PGWIDTH $PGHEIGHT)"
229229
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"
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"
236236
printf " Millimeters | %+8s x %-8s\n" "$(pointsToMillimeters $PGWIDTH)" "$(pointsToMillimeters $PGHEIGHT)"
237-
printf " Inches | %+8s x %-8s\n" "$(pointsToInches $PGWIDTH)" "$(pointsToInches $PGHEIGHT)"
237+
printf " Inches | %+8s x %-8s\n" "$(pointsToInches $PGWIDTH)" "$(pointsToInches $PGHEIGHT)"
238238
exit $EXIT_SUCCESS
239239
fi
240240
return $EXIT_SUCCESS
@@ -446,6 +446,7 @@ shouldFlip() {
446446
# Loads external dependencies and checks for errors
447447
initDeps() {
448448
GREPBIN="$(command -v grep 2>/dev/null)"
449+
STRINGSBIN="$(command -v strings 2>/dev/null)"
449450
GSBIN="$(command -v gs 2>/dev/null)"
450451
BCBIN="$(command -v bc 2>/dev/null)"
451452
IDBIN=$(command -v identify 2>/dev/null)
@@ -455,6 +456,7 @@ initDeps() {
455456
vprint "Checking for basename, grep, ghostscript and bcmath"
456457
basename "" >/dev/null 2>&1 || printDependency 'basename'
457458
isNotAvailable "$GREPBIN" && printDependency 'grep'
459+
isNotAvailable "$STRINGSBIN" && printDependency 'strings (binutils)'
458460
isNotAvailable "$GSBIN" && printDependency 'ghostscript'
459461
isNotAvailable "$BCBIN" && printDependency 'bc'
460462
return $TRUE
@@ -1073,6 +1075,11 @@ parseMode() {
10731075
MODE="PDFINFO"
10741076
return $TRUE
10751077
;;
1078+
s|gs|ghostscript|ps|postscript)
1079+
ADAPTIVEMODE=$FALSE
1080+
MODE="GS"
1081+
return $TRUE
1082+
;;
10761083
a|auto|automatic|adaptive)
10771084
ADAPTIVEMODE=$TRUE
10781085
MODE=""
@@ -1427,6 +1434,9 @@ getPageSize() {
14271434
elif [[ $MODE = "IDENTIFY" ]]; then
14281435
vprint " Method: ImageMagick's Identify"
14291436
getPageSizeImagemagick
1437+
elif [[ $MODE = "GS" ]]; then
1438+
vprint " Method: Ghostscript PS Script"
1439+
getPageSizeGS
14301440
else
14311441
printError "Error! Invalid Mode: $MODE"
14321442
printError "Aborting execution..."
@@ -1439,25 +1449,31 @@ getPageSize() {
14391449
vprint " Method: Grep"
14401450
getPageSizeCatGrep
14411451
if pageSizeIsInvalid && [[ $OSNAME = "Darwin" ]]; then
1442-
vprint " Failed"
1452+
vprint " Failed, trying next option"
14431453
vprint " Method: Mac Quartz mdls"
14441454
getPageSizeMdls
14451455
fi
14461456

14471457
if pageSizeIsInvalid; then
1448-
vprint " Failed"
1458+
vprint " Failed, trying next option"
14491459
vprint " Method: PDFInfo"
14501460
getPageSizePdfInfo
14511461
fi
14521462

14531463
if pageSizeIsInvalid; then
1454-
vprint " Failed"
1464+
vprint " Failed, trying next option"
14551465
vprint " Method: ImageMagick's Identify"
14561466
getPageSizeImagemagick
14571467
fi
14581468

14591469
if pageSizeIsInvalid; then
1460-
vprint " Failed"
1470+
vprint " Failed, trying next option"
1471+
vprint " Method: Ghostscript PS Script"
1472+
getPageSizeGS
1473+
fi
1474+
1475+
if pageSizeIsInvalid; then
1476+
vprint " Failed all options"
14611477
printError "Error when detecting PDF paper size!"
14621478
printError "All methods of detection failed"
14631479
printError "You may want to install pdfinfo or imagemagick"
@@ -1552,6 +1568,37 @@ getPageSizePdfInfo() {
15521568
return $TRUE
15531569
}
15541570

1571+
# Gets page size using Ghostscript and a PS script
1572+
getPageSizeGS() {
1573+
# Get MediaBox size using Ghostscript
1574+
local mediaBox="$("$GSBIN" -dNOSAFER -dNODISPLAY -dBATCH -dQUIET -sFileName="$INFILEPDF" -c 'FileName (r) file runpdfbegin 1 pdfgetpage /MediaBox get {=print ( ) print} forall')"
1575+
1576+
# No page size data available
1577+
if isEmpty "$mediaBox" && isNotAdaptiveMode; then
1578+
notAdaptiveFailed "There is no MediaBox in the pdf document!"
1579+
elif isEmpty "$mediaBox" && isAdaptiveMode; then
1580+
return $FALSE
1581+
fi
1582+
1583+
mediaBox=($mediaBox) # make it an array
1584+
mbCount=${#mediaBox[@]} # array size
1585+
1586+
# sanity
1587+
if [[ $mbCount -lt 4 ]] || ! isFloat "${mediaBox[2]}" || ! isFloat "${mediaBox[3]}" || isZero "${mediaBox[2]}" || isZero "${mediaBox[3]}"; then
1588+
if isNotAdaptiveMode; then
1589+
notAdaptiveFailed $'Error when reading the page size!\nThe page size information is invalid!'
1590+
fi
1591+
return $FALSE
1592+
fi
1593+
1594+
# we are done
1595+
PGWIDTH=$(printf '%.0f' "${mediaBox[2]}") # Get Round Width
1596+
PGHEIGHT=$(printf '%.0f' "${mediaBox[3]}") # Get Round Height
1597+
1598+
#echo "PGWIDTH=$PGWIDTH // PGHEIGHT=$PGHEIGHT"
1599+
return $TRUE
1600+
}
1601+
15551602
# Gets page size using cat and grep
15561603
getPageSizeCatGrep() {
15571604
# get MediaBox info from PDF file using grep, these are all possible
@@ -1561,7 +1608,7 @@ getPageSizeCatGrep() {
15611608

15621609
# Get MediaBox data if possible
15631610
#local mediaBox="$("$GREPBIN" -a -e '/MediaBox' -m 1 "$INFILEPDF" 2>/dev/null)"
1564-
local mediaBox="$(strings "$INFILEPDF" | "$GREPBIN" -a -e '/MediaBox' -m 1 2>/dev/null)"
1611+
local mediaBox="$("$STRINGSBIN" "$INFILEPDF" | "$GREPBIN" -a -e '/MediaBox' -m 1 2>/dev/null)"
15651612

15661613
mediaBox="${mediaBox##*/MediaBox}"
15671614
mediaBox="${mediaBox##*[}"
@@ -2226,6 +2273,7 @@ Parameters:
22262273
m, mdls Forces the use of MacOS Quartz mdls
22272274
p, pdfinfo Forces the use of PDFInfo
22282275
i, identify Forces the use of ImageMagick's Identify
2276+
s, gs Forces the use of Ghostscript (PS script)
22292277
-i, --info <file>
22302278
Prints <file> Paper Size information to screen and exits
22312279
-s, --scale <factor>

0 commit comments

Comments
 (0)