Advertisement
RelUnrelated

pdf-cover v.1.2

Dec 14th, 2020 (edited)
1,009
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.37 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ###########################################
  4. # NAME:     pdf-cover
  5. # AUTHOR:   RelUnrelated, Copyright 2020
  6. # LICENSE:  Creative Commons Attribution - Share Alike 3.0 http://creativecommons.org/licenses/by-sa/3.0/
  7. #       You are free to use and/or modify this script. If you choose to distribute this script, with or
  8. #       without changes, you must attribute credit to the author listed above.
  9. # REQUIRES: ImageMagick, coreutils, GhostScript
  10. # VERSION:  1.2 [2021-01-10]
  11. # DESCRIPTION:  A script to add a cover created from an existing cover image to an existing PDF file.
  12. ###########################################
  13.  
  14. if [ $# -eq 0 ]
  15. then
  16.     echo "Please state cover image and the PDF file you want to add the cover to."
  17.     exit 0
  18. else
  19.  
  20.     image_filename=$1
  21.     pdf_filename=$2
  22.     pointsperinch="0.013888888888889"
  23.     # Query files for information
  24.     pagesize=`pdfinfo "$pdf_filename" | grep "Page size"`
  25.     imagesize=`identify -verbose "$image_filename" | grep "Geometry:"`
  26.     echo "Computing page dimensions"
  27.     # Parse PDF "Page Size" into width and height in inches
  28.     pagesize=${pagesize#Page size: }
  29.     pagesize=${pagesize%% pts*}
  30.     pagepts_x=${pagesize%% x *}
  31.     pagepts_y=${pagesize#* x }
  32.     pagein_x=`bc -l <<< "$pagepts_x * $pointsperinch"`
  33.     pagein_y=`bc -l <<< "$pagepts_y * $pointsperinch"`
  34.     # Parse image size and density
  35.     imagesize=${imagesize#  Geometry: }
  36.     imagesize=${imagesize%%\+*\+*}
  37.     imagepx_x=${imagesize%%x*}
  38.     imagepx_y=${imagesize#*x}
  39.     # Calculate desired image density
  40.     newdensity_x=`bc -l <<< "$imagepx_x / $pagein_x"`
  41.     newdensity_y=`bc -l <<< "$imagepx_y / $pagein_y"`
  42.  
  43.     echo "PDF width:" $pagein_x "in"
  44.     echo "PDF height:" $pagein_y "in"
  45.     echo "Image Size:" $imagepx_x "pixels X by" $imagepx_y "pixels Y"
  46.     echo "Desired Image Density:" $newdensity_x "ppi X by" $newdensity_y "ppi Y"
  47.  
  48.     # Convert the image print size using the new density
  49.     echo "Rewriting image print size"
  50.     convert "$image_filename" -density "$newdensity_x"x"$newdensity_y" -units PixelsPerInch "$image_filename"
  51.     # Convert the new cover image to a PDF
  52.     echo "Converting cover image to PDF"
  53.     convert "$image_filename" cover.pdf
  54.     # Combine the cover PDF file with the orignal PDF into a new file
  55.     echo "Combining cover PDF with original PDF into file covered.pdf"
  56.     gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=covered.pdf  cover.pdf "$pdf_filename"
  57.     rm cover.pdf
  58.    
  59. fi
  60.  
  61. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement