diff --git a/README.md b/README.md index a54b296..5de6ebb 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,16 @@ # pdfScale.sh -Bash Script to scale PDFs from the command line. -Uses ghostscript to create a scaled version of the pdf input. -The "paper" size does not change, just the elements are resized. +Bash Script to scale and resize PDFs from the command line. +Uses ghostscript to create a scaled and or resized version of the pdf input. +In `scaling mode`, the PDF paper size does not change, just the elements are scaled. +In `resize mode`, the PDF paper will be changed and fit-to-page will be applied. +In `mixed mode`, the PDF will first be `resized` then `scaled` with two Ghostscript calls. +A temporary file is used in `mixed mode`, at the target location. ## Dependencies The script uses `basename`, `cat`, `grep`, `bc`, `head` and `gs` (ghostscript). You probably have everything installed already, except for ghostscript. -Optional dependencies are `imagemagick`, `pdfinfo` and `mdls` (Mac). +Optional dependencies are `imagemagick`, `pdfinfo` and `mdls` (Mac). +This app is focused in `Bash`, so it will probably not run on other Shells. ##### apt-get ``` @@ -21,56 +25,107 @@ sudo yum install ghostscript bc brew install ghostscript ``` ##### Optionals -From version 1.4.x I decided to create an adaptive method of getting the pagesize. It will try different methods if the previous one fails. People can also force a specific mode of operation with the `-m` parameter. +Page Size detection is by default in Adaptive Mode. +It will try the following methods in sequence: The order of operation is as follows: 1. Try to get `/MediaBox` with `cat` + `grep` 2. Failed AND MacOS ? Try `mdls` - 3. Failed AND NOT MacOS ? Try `pdfinfo` + 3. Failed ? Try `pdfinfo` 4. Failed ? Try ImageMagick's `identify` 5. Failed ? `Exit` with error message The `cat`+`grep` method will fail on PDFs without a `/MediaBox`. - -You may install any of the above to be used in that case. +You may install any of the optionals to be used in that case. + +##### apt-get +``` +sudo apt-get install imagemagick pdfinfo +``` +##### yum +``` +sudo yum install imagemagick pdfinfo +``` +##### homebrew MacOS +``` +brew install imagemagick xpdf +``` + ## Help info ``` $ pdfscale -h -pdfscale v1.4.9 +pdfscale v2.0.0 -Usage: pdfscale [-v] [-s ] [-m ] [outfile.pdf] +Usage: pdfscale [-v] [-s ] [-m ] [-r ] [outfile.pdf] + pdfscale -p pdfscale -h pdfscale -V Parameters: -v Verbose mode, prints extra information - Use twice for even more information + Use twice for timestamp -h Print this help to screen and exits -V Prints version to screen and exits - -m Force a mode of page size detection + -m Page size Detection mode May disable the Adaptive Mode - -s Changes the scaling factor, defaults to 0.95 + -s Changes the scaling factor or forces scaling + Defaults: 0.95 / no scaling (resize mode) MUST be a number bigger than zero - Eg. -s 0.8 for 80% of the original size + Eg. -s 0.8 for 80% of the original size + -r Triggers the Resize Paper Mode + Resize PDF paper proportionally + Must be a valid Ghostscript paper name + -p Prints Ghostscript paper info tables to screen + +Scaling Mode: +The default mode of operation is scaling mode with fixed paper +size and scaling pre-set to 0.95. By not using the resize mode +you are using scaling mode. + +Resize Paper Mode: +Disables the default scaling factor! (0.95) +Alternative mode of operation to change the PDF paper +proportionally. Will fit-to-page. -Modes: +Mixed Mode: +In mixed mode both the -s option and -r option must be specified. +The PDF will be both scaled and have the paper type changed. + +Output filename: +The output filename is optional. If no file name is passed +the output file will have the same name/destination of the +input file with added suffixes: + .SCALED.pdf is added to scaled files + ..pdf is added to resized files + ..SCALED.pdf is added in mixed mode + +Page Detection Modes: a, adaptive Default mode, tries all the methods below c, cat+grep Forces the use of the cat + grep method m, mdls Forces the use of MacOS Quartz mdls - p, pdfinfo Forces the use of Linux PdfInfo + p, pdfinfo Forces the use of PDFInfo i, identify Forces the use of ImageMagick's Identify +Valid Ghostscript Paper Names: +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 + Notes: - Adaptive Page size detection will try different modes until it gets a page size. You can force a mode with -m 'mode'. - Options must be passed before the file names to be parsed. - - The output filename is optional. If no file name is passed - the output file will have the same name/destination of the - input file, with .SCALED.pdf at the end (instead of just .pdf). - Having the extension .pdf on the output file name is optional, it will be added if not present. - - Should handle file names with spaces without problems. + - File and folder names with spaces should be quoted or escaped. - The scaling is centered and using a scale bigger than 1 may result on cropping parts of the pdf. @@ -86,35 +141,57 @@ Examples: ## Example runs ``` -$ pdfscale -m i -v -s 0.9 ../input.pdf -pdfscale v1.4.9 - Verbose execution +$ pdfscale -v -r a0 -s 1.05 ../mixsync\ manual\ v1-2-3.pdf +pdfscale v2.0.0 - Verbose Execution +Checking for ghostscript and bcmath + Input file: ../mixsync manual v1-2-3.pdf + Output file: ../mixsync manual v1-2-3.A0.SCALED.pdf + Get Page Size: Adaptive Enabled + Method: Cat + Grep + Mixed Tasks: Resize & Scale + Scale factor: 1.05 + Source Width: 842 postscript-points + Source Height: 595 postscript-points + Flip Detect: Wrong orientation! + Inverting Width <-> Height + Resizing to: A0 ( 3370 x 2384 ) + New Width: 3370 postscript-points + New Height: 2384 postscript-points + Translation X: -80.236330 + Translation Y: -56.760656 +``` +``` +$ pdfscale -v -v -s 0.9 ../input-nup.pdf "../my glorius PDF" +pdfscale v2.0.0 - Verbose Execution +2017-05-13:20:22:18 | Checking for ghostscript and bcmath +2017-05-13:20:22:18 | Input file: ../input-nup.pdf +2017-05-13:20:22:18 | Output file: ../my glorius PDF.pdf +2017-05-13:20:22:18 | Get Page Size: Adaptive Enabled +2017-05-13:20:22:18 | Method: Cat + Grep +2017-05-13:20:22:18 | Failed +2017-05-13:20:22:18 | Method: Mac Quartz mdls +2017-05-13:20:22:18 | Single Task: Scale PDF Contents +2017-05-13:20:22:18 | Scale factor: 0.9 (manual) +2017-05-13:20:22:18 | Source Width: 842 postscript-points +2017-05-13:20:22:18 | Source Height: 595 postscript-points +2017-05-13:20:22:18 | Translation X: 46.777310 +2017-05-13:20:22:18 | Translation Y: 33.055225 +``` +``` +$ pdfscale -v -r a2 "../mixsync manual v1-2-3.pdf" +pdfscale v2.0.0 - Verbose Execution Checking for ghostscript and bcmath -Checking for imagemagick's identify - Scale factor: 0.9 - Input file: ../input.pdf - Output file: ../input.SCALED.pdf - Adaptive mode: Disabled - Method: ImageMagick's Identify - Width: 595 postscript-points - Height: 842 postscript-points - Translation X: 33.055225 - Translation Y: 46.777310 -``` -``` -$ pdfscale -v -v -s 0.5 ../input-nup.pdf ../nuptest -2017-02-22:07:56:38 | pdfscale v1.4.9 - Verbose execution -2017-02-22:07:56:38 | Checking for ghostscript and bcmath -2017-02-22:07:56:38 | Scale factor: 0.5 -2017-02-22:07:56:38 | Input file: ../input-nup.pdf -2017-02-22:07:56:39 | Output file: ../nuptest.pdf -2017-02-22:07:56:39 | Adaptive mode: Enabled -2017-02-22:07:56:39 | Method: Cat + Grep -2017-02-22:07:56:39 | Failed -2017-02-22:07:56:39 | Method: Mac Quartz mdls -2017-02-22:07:56:39 | Width: 842 postscript-points -2017-02-22:07:56:39 | Height: 595 postscript-points -2017-02-22:07:56:39 | Translation X: 421.000000 -2017-02-22:07:56:39 | Translation Y: 297.500000 + Input file: ../mixsync manual v1-2-3.pdf + Output file: ../mixsync manual v1-2-3.A2.pdf + Get Page Size: Adaptive Enabled + Method: Cat + Grep + Single Task: Resize PDF Paper + Scale factor: Disabled (resize only) + Source Width: 842 postscript-points + Source Height: 595 postscript-points + Flip Detect: Wrong orientation! + Inverting Width <-> Height + Resizing to: A2 ( 1684 x 1191 ) ``` ## System Install diff --git a/pdfScale.sh b/pdfScale.sh index d3cf6fa..2537b3b 100755 --- a/pdfScale.sh +++ b/pdfScale.sh @@ -653,39 +653,6 @@ pageResize() { } -# Automatic name for SCALED pdf -getOutputName() { - - # Parse output filename for scaled files - if [[ -z $OUTFILEPDF ]]; then - OUTFILEPDF="${INFILEPDF%.pdf}.SCALED.pdf" - else - OUTFILEPDF="${OUTFILEPDF%.pdf}.pdf" - fi -} - -# Automatic name for SCALED pdf -getScaledOutputName() { - # Parse output filename for scaled files - if [[ -z $OUTFILEPDF ]]; then - OUTFILEPDF="${INFILEPDF%.pdf}.SCALED.pdf" - else - OUTFILEPDF="${OUTFILEPDF%.pdf}.pdf" - fi -} - -# Automatica name for RESIZED pdf -getResizedOutputName() { - # Parse output filename for scaled files - if [[ -z $OUTFILEPDF ]]; then - OUTFILEPDF="${INFILEPDF%.pdf}.$PAPER_RESIZE.pdf" - else - OUTFILEPDF="${OUTFILEPDF%.pdf}.pdf" - fi -} - - - getPaperInfo() {