# 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. ## 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). ##### apt-get ``` sudo apt-get install ghostscript bc ``` ##### yum ``` sudo yum install ghostscript bc ``` ##### homebrew MacOS ``` 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. 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` 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. ## Help info ``` $ pdfscale -h pdfscale v1.4.9 Usage: pdfscale [-v] [-s ] [-m ] [outfile.pdf] pdfscale -h pdfscale -V Parameters: -v Verbose mode, prints extra information Use twice for even more information -h Print this help to screen and exits -V Prints version to screen and exits -m Force a mode of page size detection May disable the Adaptive Mode -s Changes the scaling factor, defaults to 0.95 MUST be a number bigger than zero Eg. -s 0.8 for 80% of the original size 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 i, identify Forces the use of ImageMagick's Identify 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. - The scaling is centered and using a scale bigger than 1 may result on cropping parts of the pdf. Examples: pdfscale myPdfFile.pdf pdfscale myPdfFile.pdf myScaledPdf pdfscale -v -v myPdfFile.pdf pdfscale -s 0.85 myPdfFile.pdf myScaledPdf.pdf pdfscale -m pdfinfo -s 0.80 -v myPdfFile.pdf pdfscale -v -v -m i -s 0.7 myPdfFile.pdf pdfscale -h ``` ## Example runs ``` $ pdfscale -m i -v -s 0.9 ../input.pdf pdfscale v1.4.9 - 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 ``` ## System Install The installer will name the executable as `pdfscale` with no uppercase chars and without the `.sh` extension. If you have `make` installed you can use it to install to `/usr/local/bin/pdfscale` with: ``` sudo make install ``` To remove the installation use: ``` sudo make uninstall ```