-
Notifications
You must be signed in to change notification settings - Fork 0
/
watermark-pdf.sh
39 lines (32 loc) · 1.1 KB
/
watermark-pdf.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
#!/bin/bash
# Copyright 2018 mrl5
# Distributed under the terms of the GNU General Public License v3
# Creates a watermark on given pdf
#
# $1: input file
#
# Example: 'sh watermark-pdf.sh /path/to/input.pdf'
# creates input-watermark.pdf from input.pdf in current dir
# stores directory of the script
# source: https://stackoverflow.com/a/246128
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd -P)"
START_DIR="$(pwd -P)"
INPUT_FILE=$1
SUCCESS_MSG="Added watermark to the input file. Output file: "
FAIL_MSG="Operation failed. Aborting"
# get extension and filename
# source: https://stackoverflow.com/a/965072
filename=$(basename -- "$INPUT_FILE")
extension="${filename##*.}"
filename="${filename%.*}"
output=$START_DIR/$filename-watermark.pdf
watermark_script_location="postscript/watermark.ps"
cd $SCRIPT_DIR
gs -q -dNOPAUSE -dSAFER -dBATCH -sOutputFile=$output -sDEVICE=pdfwrite $watermark_script_location -f $START_DIR/$INPUT_FILE
# "$?" = success flag of last operation (0 = success; other = fail)
if [ $? -eq 0 ]; then
echo $SUCCESS_MSG $output
else
echo $FAIL_MSG
fi
cd $START_DIR