forked from tavinus/pdfScale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdfScale.sh
executable file
·1420 lines (1236 loc) · 49.1 KB
/
pdfScale.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# pdfScale.sh
#
# Scale PDF to specified percentage of original size.
#
# Gustavo Arnosti Neves - 2016 / 07 / 10
# Latest Version - 2017 / 05 / 19
#
# This script: https://github.com/tavinus/pdfScale
# Based on: http://ma.juii.net/blog/scale-page-content-of-pdf-files
# And: https://gist.github.com/MichaelJCole/86e4968dbfc13256228a
VERSION="2.1.2"
###################### EXTERNAL PROGRAMS #######################
GSBIN="" # GhostScript Binary
BCBIN="" # BC Math Binary
IDBIN="" # Identify Binary
PDFINFOBIN="" # PDF Info Binary
MDLSBIN="" # MacOS mdls Binary
##################### ENVIRONMENT SET-UP #######################
LC_MEASUREMENT="C" # To make sure our numbers have .decimals
LC_ALL="C" # Some languages use , as decimal token
LC_CTYPE="C"
LC_NUMERIC="C"
TRUE=0 # Silly stuff
FALSE=1
########################### GLOBALS ############################
SCALE="0.95" # scaling factor (0.95 = 95%, e.g.)
VERBOSE=0 # verbosity Level
PDFSCALE_NAME="$(basename $0)" # simplified name of this script
OSNAME="$(uname 2>/dev/null)" # Check where we are running
GS_RUN_STATUS="" # Holds GS error messages, signals errors
INFILEPDF="" # Input PDF file name
OUTFILEPDF="" # Output PDF file name
JUST_IDENTIFY=$FALSE # Flag to just show PDF info
ABORT_ON_OVERWRITE=$FALSE # Flag to abort if OUTFILEPDF already exists
ADAPTIVEMODE=$TRUE # Automatically try to guess best mode
AUTOMATIC_SCALING=$TRUE # Default scaling in $SCALE, disabled in resize mode
MODE="" # Which page size detection to use
RESIZE_PAPER_TYPE="" # Pre-defined paper to use
CUSTOM_RESIZE_PAPER=$FALSE # If we are using a custom-defined paper
FLIP_DETECTION=$TRUE # If we should run the Flip-detection
FLIP_FORCE=$FALSE # If we should force Flipping
AUTO_ROTATION='/PageByPage' # GS call auto-rotation setting
PGWIDTH="" # Input PDF Page Width
PGHEIGHT="" # Input PDF Page Height
RESIZE_WIDTH="" # Resized PDF Page Width
RESIZE_HEIGHT="" # Resized PDF Page Height
########################## EXIT FLAGS ##########################
EXIT_SUCCESS=0
EXIT_ERROR=1
EXIT_INVALID_PAGE_SIZE_DETECTED=10
EXIT_FILE_NOT_FOUND=20
EXIT_INPUT_NOT_PDF=21
EXIT_INVALID_OPTION=22
EXIT_NO_INPUT_FILE=23
EXIT_INVALID_SCALE=24
EXIT_MISSING_DEPENDENCY=25
EXIT_IMAGEMAGIK_NOT_FOUND=26
EXIT_MAC_MDLS_NOT_FOUND=27
EXIT_PDFINFO_NOT_FOUND=28
EXIT_NOWRITE_PERMISSION=29
EXIT_NOREAD_PERMISSION=30
EXIT_TEMP_FILE_EXISTS=40
EXIT_INVALID_PAPER_SIZE=50
############################# MAIN #############################
# Main function called at the end
main() {
printPDFSizes # may exit here
local finalRet=$EXIT_ERROR
if isMixedMode; then
initMain " Mixed Tasks: Resize & Scale"
local tempFile=""
local tempSuffix="$RANDOM$RANDOM""_TEMP_$RANDOM$RANDOM.pdf"
outputFile="$OUTFILEPDF" # backup outFile name
tempFile="${OUTFILEPDF%.pdf}.$tempSuffix" # set a temp file name
if isFile "$tempFile"; then
printError $'Error! Temporary file name already exists!\n'"File: $tempFile"$'\nAborting execution to avoid overwriting the file.\nPlease Try again...'
exit $EXIT_TEMP_FILE_EXISTS
fi
OUTFILEPDF="$tempFile" # set output to tmp file
pageResize # resize to tmp file
finalRet=$?
INFILEPDF="$tempFile" # get tmp file as input
OUTFILEPDF="$outputFile" # reset final target
PGWIDTH=$RESIZE_WIDTH # we already know the new page size
PGHEIGHT=$RESIZE_HEIGHT # from the last command (Resize)
vPrintPageSizes ' New'
vPrintScaleFactor
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"
elif isResizeMode; then
initMain " Single Task: Resize PDF Paper"
vPrintScaleFactor "Disabled (resize only)"
pageResize
finalRet=$?
else
initMain " Single Task: Scale PDF Contents"
local scaleMode=""
isManualScaledMode && scaleMode='(manual)' || scaleMode='(auto)'
vPrintScaleFactor "$SCALE $scaleMode"
pageScale
finalRet=$?
fi
if [[ $finalRet -eq $EXIT_SUCCESS ]] && isEmpty "$GS_RUN_STATUS"; then
vprint " Final Status: File created successfully"
else
vprint " Final Status: Error detected. Exit status: $finalRet"
printError "PdfScale: ERROR!"$'\n'"Ghostscript Debug Info:"$'\n'"$GS_RUN_STATUS"
fi
return $finalRet
}
# Initializes PDF processing for all modes of operation
initMain() {
printVersion 1 'verbose'
isNotEmpty "$1" && vprint "$1"
vPrintFileInfo
getPageSize
vPrintPageSizes ' Source'
}
# Prints PDF Info and exits with $EXIT_SUCCESS, but only if $JUST_IDENTIFY is $TRUE
printPDFSizes() {
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
VERBOSE=0
printVersion 3 " - Paper Sizes"
getPageSize || initError "Could not get pagesize!"
local paperType="$(getGSPaperName $PGWIDTH $PGHEIGHT)"
isEmpty "$paperType" && paperType="Custom Paper Size"
printf '%s\n' "------------+-----------------------------"
printf " File | %s\n" "$(basename "$INFILEPDF")"
printf " Paper Type | %s\n" "$paperType"
printf '%s\n' "------------+-----------------------------"
printf '%s\n' " | WIDTH x HEIGHT"
printf " Points | %+8s x %-8s\n" "$PGWIDTH" "$PGHEIGHT"
printf " Milimeters | %+8s x %-8s\n" "$(pointsToMilimeters $PGWIDTH)" "$(pointsToMilimeters $PGHEIGHT)"
printf " Inches | %+8s x %-8s\n" "$(pointsToInches $PGWIDTH)" "$(pointsToInches $PGHEIGHT)"
exit $EXIT_SUCCESS
fi
return $EXIT_SUCCESS
}
###################### GHOSTSCRIPT CALLS #######################
# Runs the ghostscript scaling script
pageScale() {
# Compute translation factors (to center page).
XTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGWIDTH" | "$BCBIN")
YTRANS=$(echo "scale=6; 0.5*(1.0-$SCALE)/$SCALE*$PGHEIGHT" | "$BCBIN")
vprint " Translation X: $XTRANS"
vprint " Translation Y: $YTRANS"
local increase=$(echo "scale=0; (($SCALE - 1) * 100)/1" | "$BCBIN")
vprint " Run Scaling: $increase %"
GS_RUN_STATUS="$GS_RUN_STATUS""$(gsPageScale 2>&1)"
return $? # Last command is always returned I think
}
# Runs GS call for scaling, nothing else should run here
gsPageScale() {
# Scale page
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="/printer" \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dSubsetFonts=true -dEmbedAllFonts=true \
-dDEVICEWIDTHPOINTS=$PGWIDTH -dDEVICEHEIGHTPOINTS=$PGHEIGHT \
-sOutputFile="$OUTFILEPDF" \
-c "<</BeginPage{$SCALE $SCALE scale $XTRANS $YTRANS translate}>> setpagedevice" \
-f "$INFILEPDF"
return $?
}
# Runs the ghostscript paper resize script
pageResize() {
# Get paper sizes from source if not resizing
isResizePaperSource && { RESIZE_WIDTH=$PGWIDTH; RESIZE_HEIGHT=$PGHEIGHT; }
# Get new paper sizes if not custom or source paper
isNotCustomPaper && ! isResizePaperSource && getGSPaperSize "$RESIZE_PAPER_TYPE"
vprint " Auto Rotate: $(basename $AUTO_ROTATION)"
runFlipDetect
vprint " Run Resizing: $(uppercase "$RESIZE_PAPER_TYPE") ( "$RESIZE_WIDTH" x "$RESIZE_HEIGHT" ) pts"
GS_RUN_STATUS="$GS_RUN_STATUS""$(gsPageResize 2>&1)"
return $?
}
# Runs GS call for resizing, nothing else should run here
gsPageResize() {
# Change page size
"$GSBIN" \
-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dSAFER \
-dCompatibilityLevel="1.5" -dPDFSETTINGS="/printer" \
-dColorConversionStrategy=/LeaveColorUnchanged \
-dSubsetFonts=true -dEmbedAllFonts=true \
-dDEVICEWIDTHPOINTS=$RESIZE_WIDTH -dDEVICEHEIGHTPOINTS=$RESIZE_HEIGHT \
-dAutoRotatePages=$AUTO_ROTATION \
-dFIXEDMEDIA -dPDFFitPage \
-sOutputFile="$OUTFILEPDF" \
-f "$INFILEPDF"
return $?
}
# Returns $TRUE if we should use the source paper size, $FALSE otherwise
isResizePaperSource() {
[[ "$RESIZE_PAPER_TYPE" = 'source' ]] && return $TRUE
return $FALSE
}
# Filp-Detect Logic
runFlipDetect() {
if isFlipForced; then
vprint " Flip Detect: Forced Mode!"
applyFlipRevert
elif isFlipDetectionEnabled && shouldFlip; then
vprint " Flip Detect: Wrong orientation detected!"
applyFlipRevert
elif ! isFlipDetectionEnabled; then
vprint " Flip Detect: Disabled"
else
vprint " Flip Detect: No change needed"
fi
}
# Inverts $RESIZE_HEIGHT with $RESIZE_WIDTH
applyFlipRevert() {
local tmpInverter=""
tmpInverter=$RESIZE_HEIGHT
RESIZE_HEIGHT=$RESIZE_WIDTH
RESIZE_WIDTH=$tmpInverter
vprint " Inverting Width <-> Height"
}
# Returns the $FLIP_DETECTION flag
isFlipDetectionEnabled() {
return $FLIP_DETECTION
}
# Returns the $FLIP_FORCE flag
isFlipForced() {
return $FLIP_FORCE
}
# Returns $TRUE if the the paper size will invert orientation from source, $FALSE otherwise
shouldFlip() {
[[ $PGWIDTH -gt $PGHEIGHT && $RESIZE_WIDTH -lt $RESIZE_HEIGHT ]] || [[ $PGWIDTH -lt $PGHEIGHT && $RESIZE_WIDTH -gt $RESIZE_HEIGHT ]] && return $TRUE
return $FALSE
}
########################## INITIALIZERS #########################
# Loads external dependencies and checks for errors
initDeps() {
GREPBIN="$(which grep 2>/dev/null)"
GSBIN="$(which gs 2>/dev/null)"
BCBIN="$(which bc 2>/dev/null)"
IDBIN=$(which identify 2>/dev/null)
MDLSBIN="$(which mdls 2>/dev/null)"
PDFINFOBIN="$(which pdfinfo 2>/dev/null)"
vprint "Checking for basename, grep, ghostscript and bcmath"
basename "" >/dev/null 2>&1 || printDependency 'basename'
isNotAvailable "$GREPBIN" && printDependency 'grep'
isNotAvailable "$GSBIN" && printDependency 'ghostscript'
isNotAvailable "$BCBIN" && printDependency 'bc'
return $TRUE
}
# Checks for dependencies errors, run after getting options
checkDeps() {
if [[ $MODE = "IDENTIFY" ]]; then
vprint "Checking for imagemagick's identify"
if isNotAvailable "$IDBIN"; then printDependency 'imagemagick'; fi
fi
if [[ $MODE = "PDFINFO" ]]; then
vprint "Checking for pdfinfo"
if isNotAvailable "$PDFINFOBIN"; then printDependency 'pdfinfo'; fi
fi
if [[ $MODE = "MDLS" ]]; then
vprint "Checking for MacOS mdls"
if isNotAvailable "$MDLSBIN"; then
initError 'mdls executable was not found! Is this even MacOS?' $EXIT_MAC_MDLS_NOT_FOUND
fi
fi
return $TRUE
}
######################### CLI OPTIONS ##########################
# Parse options
getOptions() {
local _optArgs=() # things that do not start with a '-'
local _tgtFile="" # to set $OUTFILEPDF
local _currParam="" # to enable case-insensitiveness
while [ ${#} -gt 0 ]; do
if [[ "${1:0:2}" = '--' ]]; then
# Long Option, get lowercase version
_currParam="$(lowercase ${1})"
elif [[ "${1:0:1}" = '-' ]]; then
# short Option, just assign
_currParam="${1}"
else
# file name arguments, store as is and reset loop
_optArgs+=("$1")
shift
continue
fi
case "$_currParam" in
-v|--verbose)
((VERBOSE++))
shift
;;
-n|--no-overwrite|--nooverwrite)
ABORT_ON_OVERWRITE=$TRUE
shift
;;
-h|--help)
printHelp
exit $EXIT_SUCCESS
;;
-V|--version)
printVersion 3
exit $EXIT_SUCCESS
;;
-i|--identify|--info)
JUST_IDENTIFY=$TRUE
shift
;;
-s|--scale|--setscale|--set-scale)
shift
parseScale "$1"
shift
;;
-m|--mode|--paperdetect|--paper-detect|--pagesizemode|--page-size-mode)
shift
parseMode "$1"
shift
;;
-r|--resize)
shift
parsePaperResize "$1"
shift
;;
-p|--printpapers|--print-papers|--listpapers|--list-papers)
printPaperInfo
exit $EXIT_SUCCESS
;;
-f|--flipdetection|--flip-detection|--flip-mode|--flipmode|--flipdetect|--flip-detect)
shift
parseFlipDetectionMode "$1"
shift
;;
-a|--autorotation|--auto-rotation|--autorotate|--auto-rotate)
shift
parseAutoRotationMode "$1"
shift
;;
*)
initError "Invalid Parameter: \"$1\"" $EXIT_INVALID_OPTION
;;
esac
done
isEmpty "${_optArgs[2]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[2]}" $EXIT_INVALID_OPTION
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
isEmpty "${_optArgs[1]}" || initError "Seems like you passed an extra file name?"$'\n'"Invalid option: ${_optArgs[1]}" $EXIT_INVALID_OPTION
VERBOSE=0 # remove verboseness if present
fi
# Validate input PDF file
INFILEPDF="${_optArgs[0]}"
isEmpty "$INFILEPDF" && initError "Input file is empty!" $EXIT_NO_INPUT_FILE
isPDF "$INFILEPDF" || initError "Input file is not a PDF file: $INFILEPDF" $EXIT_INPUT_NOT_PDF
isFile "$INFILEPDF" || initError "Input file not found: $INFILEPDF" $EXIT_FILE_NOT_FOUND
isReadable "$INFILEPDF" || initError "No read access to input file: $INFILEPDF"$'\nPermission Denied' $EXIT_NOREAD_PERMISSION
checkDeps
if [[ $JUST_IDENTIFY -eq $TRUE ]]; then
return $TRUE # no need to get output file, so return already
fi
_tgtFile="${_optArgs[1]}"
local _autoName="${INFILEPDF%.*}" # remove possible stupid extension, like .pDF
if isMixedMode; then
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE).SCALED.pdf"
elif isResizeMode; then
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.$(uppercase $RESIZE_PAPER_TYPE).pdf"
else
isEmpty "$_tgtFile" && OUTFILEPDF="${_autoName}.SCALED.pdf"
fi
isNotEmpty "$_tgtFile" && OUTFILEPDF="${_tgtFile%.pdf}.pdf"
validateOutFile
}
# Checks if output file is valid and writable
validateOutFile() {
local _tgtDir="$(dirname "$OUTFILEPDF")"
isDir "$_tgtDir" || initError "Output directory does not exist!"$'\n'"Target Dir: $_tgtDir" $EXIT_NOWRITE_PERMISSION
isAbortOnOverwrite && isFile "$OUTFILEPDF" && initError $'Output file already exists and --no-overwrite was used!\nRemove the "-n" or "--no-overwrite" option if you want to overwrite the file\n'"Target File: $OUTFILEPDF" $EXIT_NOWRITE_PERMISSION
isTouchable "$OUTFILEPDF" || initError "Could not get write permission for output file!"$'\n'"Target File: $OUTFILEPDF"$'\nPermission Denied' $EXIT_NOWRITE_PERMISSION
}
# Returns $TRUE if we should not overwrite $OUTFILEPDF, $FALSE otherwise
isAbortOnOverwrite() {
return $ABORT_ON_OVERWRITE
}
# Parses and validates the scaling factor
parseScale() {
AUTOMATIC_SCALING=$FALSE
if ! isFloatBiggerThanZero "$1"; then
printError "Invalid factor: $1"
printError "The factor must be a floating point number greater than 0"
printError "Example: for 80% use 0.8"
exit $EXIT_INVALID_SCALE
fi
SCALE="$1"
}
# Parse a forced mode of operation
parseMode() {
local param="$(lowercase $1)"
case "${param}" in
c|catgrep|'cat+grep'|grep|g)
ADAPTIVEMODE=$FALSE
MODE="CATGREP"
return $TRUE
;;
i|imagemagick|identify)
ADAPTIVEMODE=$FALSE
MODE="IDENTIFY"
return $TRUE
;;
m|mdls|quartz|mac)
ADAPTIVEMODE=$FALSE
MODE="MDLS"
return $TRUE
;;
p|pdfinfo)
ADAPTIVEMODE=$FALSE
MODE="PDFINFO"
return $TRUE
;;
a|auto|automatic|adaptive)
ADAPTIVEMODE=$TRUE
MODE=""
return $TRUE
;;
*)
initError "Invalid PDF Size Detection Mode: \"$1\"" $EXIT_INVALID_OPTION
return $FALSE
;;
esac
return $FALSE
}
# Parses and validates the scaling factor
parseFlipDetectionMode() {
local param="$(lowercase $1)"
case "${param}" in
d|disable)
FLIP_DETECTION=$FALSE
FLIP_FORCE=$FALSE
;;
f|force)
FLIP_DETECTION=$FALSE
FLIP_FORCE=$TRUE
;;
a|auto|automatic)
FLIP_DETECTION=$TRUE
FLIP_FORCE=$FALSE
;;
*)
initError "Invalid Flip Detection Mode: \"$1\"" $EXIT_INVALID_OPTION
return $FALSE
;;
esac
}
# Parses and validates the scaling factor
parseAutoRotationMode() {
local param="$(lowercase $1)"
case "${param}" in
n|none)
AUTO_ROTATION='/None'
;;
a|all)
AUTO_ROTATION='/All'
;;
p|pagebypage|auto)
AUTO_ROTATION='/PageByPage'
;;
*)
initError "Invalid Auto Rotation Mode: \"$1\"" $EXIT_INVALID_OPTION
return $FALSE
;;
esac
}
# Validades the a paper resize CLI option and sets the paper to $RESIZE_PAPER_TYPE
parsePaperResize() {
isEmpty "$1" && initError 'Invalid Paper Type: (empty)' $EXIT_INVALID_PAPER_SIZE
local lowercasePaper="$(lowercase $1)"
local customPaper=($lowercasePaper)
if [[ "$customPaper" = 'same' || "$customPaper" = 'keep' || "$customPaper" = 'source' ]]; then
RESIZE_PAPER_TYPE='source'
elif [[ "${customPaper[0]}" = 'custom' ]]; then
if isNotValidMeasure "${customPaper[1]}" || ! isFloatBiggerThanZero "${customPaper[2]}" || ! isFloatBiggerThanZero "${customPaper[3]}"; then
initError "Invalid Custom Paper Definition!"$'\n'"Use: -r 'custom <measurement> <width> <height>'"$'\n'"Measurements: mm, in, pts" $EXIT_INVALID_OPTION
fi
RESIZE_PAPER_TYPE="custom"
CUSTOM_RESIZE_PAPER=$TRUE
if isMilimeter "${customPaper[1]}"; then
RESIZE_WIDTH="$(milimetersToPoints "${customPaper[2]}")"
RESIZE_HEIGHT="$(milimetersToPoints "${customPaper[3]}")"
elif isInch "${customPaper[1]}"; then
RESIZE_WIDTH="$(inchesToPoints "${customPaper[2]}")"
RESIZE_HEIGHT="$(inchesToPoints "${customPaper[3]}")"
elif isPoint "${customPaper[1]}"; then
RESIZE_WIDTH="${customPaper[2]}"
RESIZE_HEIGHT="${customPaper[3]}"
else
initError "Invalid Custom Paper Definition!"$'\n'"Use: -r 'custom <measurement> <width> <height>'"$'\n'"Measurements: mm, in, pts" $EXIT_INVALID_OPTION
fi
else
isPaperName "$lowercasePaper" || initError "Invalid Paper Type: $1" $EXIT_INVALID_PAPER_SIZE
RESIZE_PAPER_TYPE="$lowercasePaper"
fi
}
################### PDF PAGE SIZE DETECTION ####################
################################################################
# Detects operation mode and also runs the adaptive mode
# PAGESIZE LOGIC
# 1- Try to get Mediabox with GREP
# 2- MacOS => try to use mdls
# 3- Try to use pdfinfo
# 4- Try to use identify (imagemagick)
# 5- Fail
################################################################
getPageSize() {
if isNotAdaptiveMode; then
vprint " Get Page Size: Adaptive Disabled"
if [[ $MODE = "CATGREP" ]]; then
vprint " Method: Grep"
getPageSizeCatGrep
elif [[ $MODE = "MDLS" ]]; then
vprint " Method: Mac Quartz mdls"
getPageSizeMdls
elif [[ $MODE = "PDFINFO" ]]; then
vprint " Method: PDFInfo"
getPageSizePdfInfo
elif [[ $MODE = "IDENTIFY" ]]; then
vprint " Method: ImageMagick's Identify"
getPageSizeImagemagick
else
printError "Error! Invalid Mode: $MODE"
printError "Aborting execution..."
exit $EXIT_INVALID_OPTION
fi
return $TRUE
fi
vprint " Get Page Size: Adaptive Enabled"
vprint " Method: Grep"
getPageSizeCatGrep
if pageSizeIsInvalid && [[ $OSNAME = "Darwin" ]]; then
vprint " Failed"
vprint " Method: Mac Quartz mdls"
getPageSizeMdls
fi
if pageSizeIsInvalid; then
vprint " Failed"
vprint " Method: PDFInfo"
getPageSizePdfInfo
fi
if pageSizeIsInvalid; then
vprint " Failed"
vprint " Method: ImageMagick's Identify"
getPageSizeImagemagick
fi
if pageSizeIsInvalid; then
vprint " Failed"
printError "Error when detecting PDF paper size!"
printError "All methods of detection failed"
printError "You may want to install pdfinfo or imagemagick"
exit $EXIT_INVALID_PAGE_SIZE_DETECTED
fi
return $TRUE
}
# Gets page size using imagemagick's identify
getPageSizeImagemagick() {
# Sanity and Adaptive together
if isNotFile "$IDBIN" && isNotAdaptiveMode; then
notAdaptiveFailed "Make sure you installed ImageMagick and have identify on your \$PATH" "ImageMagick's Identify"
elif isNotFile "$IDBIN" && isAdaptiveMode; then
return $FALSE
fi
# get data from image magick
local identify="$("$IDBIN" -format '%[fx:w] %[fx:h]BREAKME' "$INFILEPDF" 2>/dev/null)"
if isEmpty "$identify" && isNotAdaptiveMode; then
notAdaptiveFailed "ImageMagicks's Identify returned an empty string!"
elif isEmpty "$identify" && isAdaptiveMode; then
return $FALSE
fi
identify="${identify%%BREAKME*}" # get page size only for 1st page
identify=($identify) # make it an array
PGWIDTH=$(printf '%.0f' "${identify[0]}") # assign
PGHEIGHT=$(printf '%.0f' "${identify[1]}") # assign
return $TRUE
}
# Gets page size using Mac Quarts mdls
getPageSizeMdls() {
# Sanity and Adaptive together
if isNotFile "$MDLSBIN" && isNotAdaptiveMode; then
notAdaptiveFailed "Are you even trying this on a Mac?" "Mac Quartz mdls"
elif isNotFile "$MDLSBIN" && isAdaptiveMode; then
return $FALSE
fi
local identify="$("$MDLSBIN" -mdls -name kMDItemPageHeight -name kMDItemPageWidth "$INFILEPDF" 2>/dev/null)"
if isEmpty "$identify" && isNotAdaptiveMode; then
notAdaptiveFailed "Mac Quartz mdls returned an empty string!"
elif isEmpty "$identify" && isAdaptiveMode; then
return $FALSE
fi
identify=${identify//$'\t'/ } # change tab to space
identify=($identify) # make it an array
if [[ "${identify[5]}" = "(null)" || "${identify[2]}" = "(null)" ]] && isNotAdaptiveMode; then
notAdaptiveFailed "There was no metadata to read from the file! Is Spotlight OFF?"
elif [[ "${identify[5]}" = "(null)" || "${identify[2]}" = "(null)" ]] && isAdaptiveMode; then
return $FALSE
fi
PGWIDTH=$(printf '%.0f' "${identify[5]}") # assign
PGHEIGHT=$(printf '%.0f' "${identify[2]}") # assign
return $TRUE
}
# Gets page size using Linux PdfInfo
getPageSizePdfInfo() {
# Sanity and Adaptive together
if isNotFile "$PDFINFOBIN" && isNotAdaptiveMode; then
notAdaptiveFailed "Do you have pdfinfo installed and available on your \$PATH?" "Linux pdfinfo"
elif isNotFile "$PDFINFOBIN" && isAdaptiveMode; then
return $FALSE
fi
# get data from image magick
local identify="$("$PDFINFOBIN" "$INFILEPDF" 2>/dev/null | "$GREPBIN" -i 'Page size:' )"
if isEmpty "$identify" && isNotAdaptiveMode; then
notAdaptiveFailed "Linux PdfInfo returned an empty string!"
elif isEmpty "$identify" && isAdaptiveMode; then
return $FALSE
fi
identify="${identify##*Page size:}" # remove stuff
identify=($identify) # make it an array
PGWIDTH=$(printf '%.0f' "${identify[0]}") # assign
PGHEIGHT=$(printf '%.0f' "${identify[2]}") # assign
return $TRUE
}
# Gets page size using cat and grep
getPageSizeCatGrep() {
# get MediaBox info from PDF file using grep, these are all possible
# /MediaBox [0 0 595 841]
# /MediaBox [ 0 0 595.28 841.89]
# /MediaBox[ 0 0 595.28 841.89 ]
# Get MediaBox data if possible
local mediaBox="$("$GREPBIN" -a -e '/MediaBox' -m 1 "$INFILEPDF" 2>/dev/null)"
mediaBox="${mediaBox##*/MediaBox}"
# No page size data available
if isEmpty "$mediaBox" && isNotAdaptiveMode; then
notAdaptiveFailed "There is no MediaBox in the pdf document!"
elif isEmpty "$mediaBox" && isAdaptiveMode; then
return $FALSE
fi
# remove chars [ and ]
mediaBox="${mediaBox//[}"
mediaBox="${mediaBox//]}"
mediaBox=($mediaBox) # make it an array
mbCount=${#mediaBox[@]} # array size
# sanity
if [[ $mbCount -lt 4 ]]; then
printError "Error when reading the page size!"
printError "The page size information is invalid!"
exit $EXIT_INVALID_PAGE_SIZE_DETECTED
fi
# we are done
PGWIDTH=$(printf '%.0f' "${mediaBox[2]}") # Get Round Width
PGHEIGHT=$(printf '%.0f' "${mediaBox[3]}") # Get Round Height
return $TRUE
}
# Prints error message and exits execution
notAdaptiveFailed() {
local errProgram="$2"
local errStr="$1"
if isEmpty "$2"; then
printError "Error when reading input file!"
printError "Could not determine the page size!"
else
printError "Error! $2 was not found!"
fi
isNotEmpty "$errStr" && printError "$errStr"
printError "Aborting! You may want to try the adaptive mode."
exit $EXIT_INVALID_PAGE_SIZE_DETECTED
}
# Verbose print of the Width and Height (Source or New) to screen
vPrintPageSizes() {
vprint " $1 Width: $PGWIDTH postscript-points"
vprint "$1 Height: $PGHEIGHT postscript-points"
}
#################### GHOSTSCRIPT PAPER INFO ####################
# Loads valid paper info to memory
getPaperInfo() {
# name inchesW inchesH mmW mmH pointsW pointsH
sizesUS="\
11x17 11.0 17.0 279 432 792 1224
ledger 17.0 11.0 432 279 1224 792
legal 8.5 14.0 216 356 612 1008
letter 8.5 11.0 216 279 612 792
lettersmall 8.5 11.0 216 279 612 792
archE 36.0 48.0 914 1219 2592 3456
archD 24.0 36.0 610 914 1728 2592
archC 18.0 24.0 457 610 1296 1728
archB 12.0 18.0 305 457 864 1296
archA 9.0 12.0 229 305 648 864"
sizesISO="\
a0 33.1 46.8 841 1189 2384 3370
a1 23.4 33.1 594 841 1684 2384
a2 16.5 23.4 420 594 1191 1684
a3 11.7 16.5 297 420 842 1191
a4 8.3 11.7 210 297 595 842
a4small 8.3 11.7 210 297 595 842
a5 5.8 8.3 148 210 420 595
a6 4.1 5.8 105 148 297 420
a7 2.9 4.1 74 105 210 297
a8 2.1 2.9 52 74 148 210
a9 1.5 2.1 37 52 105 148
a10 1.0 1.5 26 37 73 105
isob0 39.4 55.7 1000 1414 2835 4008
isob1 27.8 39.4 707 1000 2004 2835
isob2 19.7 27.8 500 707 1417 2004
isob3 13.9 19.7 353 500 1001 1417
isob4 9.8 13.9 250 353 709 1001
isob5 6.9 9.8 176 250 499 709
isob6 4.9 6.9 125 176 354 499
c0 36.1 51.1 917 1297 2599 3677
c1 25.5 36.1 648 917 1837 2599
c2 18.0 25.5 458 648 1298 1837
c3 12.8 18.0 324 458 918 1298
c4 9.0 12.8 229 324 649 918
c5 6.4 9.0 162 229 459 649
c6 4.5 6.4 114 162 323 459"
sizesJIS="\
jisb0 NA NA 1030 1456 2920 4127
jisb1 NA NA 728 1030 2064 2920
jisb2 NA NA 515 728 1460 2064
jisb3 NA NA 364 515 1032 1460
jisb4 NA NA 257 364 729 1032
jisb5 NA NA 182 257 516 729
jisb6 NA NA 128 182 363 516"
sizesOther="\
flsa 8.5 13.0 216 330 612 936
flse 8.5 13.0 216 330 612 936
halfletter 5.5 8.5 140 216 396 612
hagaki 3.9 5.8 100 148 283 420"
sizesAll="\
$sizesUS
$sizesISO
$sizesJIS
$sizesOther"
}
# Gets a paper size in points and sets it to RESIZE_WIDTH and RESIZE_HEIGHT
getGSPaperSize() {
isEmpty "$sizesall" && getPaperInfo
while read l; do
local cols=($l)
if [[ "$1" == ${cols[0]} ]]; then
RESIZE_WIDTH=${cols[5]}
RESIZE_HEIGHT=${cols[6]}
return $TRUE
fi
done <<< "$sizesAll"
}
# Gets a paper size in points and sets it to RESIZE_WIDTH and RESIZE_HEIGHT
getGSPaperName() {
local w="$(printf "%.0f" $1)"
local h="$(printf "%.0f" $2)"
isEmpty "$sizesall" && getPaperInfo
# Because US Standard has inverted sizes, I need to scan 2 times
# instead of just testing if width is bigger than height
while read l; do
local cols=($l)
if [[ "$w" == ${cols[5]} && "$h" == ${cols[6]} ]]; then
printf "%s Portrait" $(uppercase ${cols[0]})
return $TRUE
fi
done <<< "$sizesAll"
while read l; do
local cols=($l)
if [[ "$w" == ${cols[6]} && "$h" == ${cols[5]} ]]; then
printf "%s Landscape" $(uppercase ${cols[0]})
return $TRUE
fi
done <<< "$sizesAll"
return $FALSE
}
# Loads an array with paper names to memory
getPaperNames() {
paperNames=(a0 a1 a2 a3 a4 a4small a5 a6 a7 a8 a9 a10 isob0 isob1 isob2 isob3 isob4 isob5 isob6 c0 c1 c2 c3 c4 c5 c6 \
11x17 ledger legal letter lettersmall archE archD archC archB archA \
jisb0 jisb1 jisb2 jisb3 jisb4 jisb5 jisb6 \
flsa flse halfletter hagaki)
}
# Prints uppercase paper names to screen (used in help)
printPaperNames() {
isEmpty "$paperNames" && getPaperNames
for i in "${!paperNames[@]}"; do
[[ $i -eq 0 ]] && echo -n -e ' '
[[ $i -ne 0 && $((i % 5)) -eq 0 ]] && echo -n -e $'\n '
ppN="$(uppercase ${paperNames[i]})"
printf "%-14s" "$ppN"
done
echo ""
}
# Returns $TRUE if $! is a valid paper name, $FALSE otherwise
isPaperName() {
isEmpty "$1" && return $FALSE
isEmpty "$paperNames" && getPaperNames
for i in "${paperNames[@]}"; do
[[ "$i" = "$1" ]] && return $TRUE
done
return $FALSE
}
# Prints all tables with ghostscript paper information
printPaperInfo() {
printVersion 3
echo $'\n'"Paper Sizes Information"$'\n'
getPaperInfo
printPaperTable "ISO STANDARD" "$sizesISO"; echo
printPaperTable "US STANDARD" "$sizesUS"; echo
printPaperTable "JIS STANDARD *Aproximated Points" "$sizesJIS"; echo
printPaperTable "OTHERS" "$sizesOther"; echo
}
# GS paper table helper, prints a full line
printTableLine() {
echo '+-----------------------------------------------------------------+'
}
# GS paper table helper, prints a line with dividers
printTableDivider() {
echo '+-----------------+-------+-------+-------+-------+-------+-------+'
}
# GS paper table helper, prints a table header
printTableHeader() {
echo '| Name | inchW | inchH | mm W | mm H | pts W | pts H |'
}
# GS paper table helper, prints a table title
printTableTitle() {
printf "| %-64s%s\n" "$1" '|'
}
# GS paper table printer, prints a table for a paper variable
printPaperTable() {
printTableLine
printTableTitle "$1"
printTableLine
printTableHeader
printTableDivider
while read l; do
local cols=($l)
printf "| %-15s | %+5s | %+5s | %+5s | %+5s | %+5s | %+5s |\n" ${cols[*]};
done <<< "$2"
printTableDivider
}
# Returns $TRUE if $1 is a valid measurement for a custom paper, $FALSE otherwise
isNotValidMeasure() {
isMilimeter "$1" || isInch "$1" || isPoint "$1" && return $FALSE
return $TRUE
}
# Returns $TRUE if $1 is a valid milimeter string, $FALSE otherwise
isMilimeter() {
[[ "$1" = 'mm' || "$1" = 'milimeters' || "$1" = 'milimeter' ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if $1 is a valid inch string, $FALSE otherwise
isInch() {
[[ "$1" = 'in' || "$1" = 'inch' || "$1" = 'inches' ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if $1 is a valid point string, $FALSE otherwise
isPoint() {
[[ "$1" = 'pt' || "$1" = 'pts' || "$1" = 'point' || "$1" = 'points' ]] && return $TRUE
return $FALSE
}
# Returns $TRUE if a custom paper is being used, $FALSE otherwise
isCustomPaper() {
return $CUSTOM_RESIZE_PAPER
}
# Returns $FALSE if a custom paper is being used, $TRUE otherwise
isNotCustomPaper() {
isCustomPaper && return $FALSE
return $TRUE
}
######################### CONVERSIONS ##########################
# Prints the lowercase char value for $1
lowercaseChar() {
case "$1" in
[A-Z])
n=$(printf "%d" "'$1")
n=$((n+32))
printf \\$(printf "%o" "$n")