Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ###########################################
- # NAME: pdf-cover
- # AUTHOR: RelUnrelated, Copyright 2020
- # LICENSE: Creative Commons Attribution - Share Alike 3.0 http://creativecommons.org/licenses/by-sa/3.0/
- # You are free to use and/or modify this script. If you choose to distribute this script, with or
- # without changes, you must attribute credit to the author listed above.
- # REQUIRES: ImageMagick, coreutils, GhostScript
- # VERSION: 1.2 [2021-01-10]
- # DESCRIPTION: A script to add a cover created from an existing cover image to an existing PDF file.
- ###########################################
- if [ $# -eq 0 ]
- then
- echo "Please state cover image and the PDF file you want to add the cover to."
- exit 0
- else
- image_filename=$1
- pdf_filename=$2
- pointsperinch="0.013888888888889"
- # Query files for information
- pagesize=`pdfinfo "$pdf_filename" | grep "Page size"`
- imagesize=`identify -verbose "$image_filename" | grep "Geometry:"`
- echo "Computing page dimensions"
- # Parse PDF "Page Size" into width and height in inches
- pagesize=${pagesize#Page size: }
- pagesize=${pagesize%% pts*}
- pagepts_x=${pagesize%% x *}
- pagepts_y=${pagesize#* x }
- pagein_x=`bc -l <<< "$pagepts_x * $pointsperinch"`
- pagein_y=`bc -l <<< "$pagepts_y * $pointsperinch"`
- # Parse image size and density
- imagesize=${imagesize# Geometry: }
- imagesize=${imagesize%%\+*\+*}
- imagepx_x=${imagesize%%x*}
- imagepx_y=${imagesize#*x}
- # Calculate desired image density
- newdensity_x=`bc -l <<< "$imagepx_x / $pagein_x"`
- newdensity_y=`bc -l <<< "$imagepx_y / $pagein_y"`
- echo "PDF width:" $pagein_x "in"
- echo "PDF height:" $pagein_y "in"
- echo "Image Size:" $imagepx_x "pixels X by" $imagepx_y "pixels Y"
- echo "Desired Image Density:" $newdensity_x "ppi X by" $newdensity_y "ppi Y"
- # Convert the image print size using the new density
- echo "Rewriting image print size"
- convert "$image_filename" -density "$newdensity_x"x"$newdensity_y" -units PixelsPerInch "$image_filename"
- # Convert the new cover image to a PDF
- echo "Converting cover image to PDF"
- convert "$image_filename" cover.pdf
- # Combine the cover PDF file with the orignal PDF into a new file
- echo "Combining cover PDF with original PDF into file covered.pdf"
- gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dAutoRotatePages=/None -sOutputFile=covered.pdf cover.pdf "$pdf_filename"
- rm cover.pdf
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement