Skip to content

Instantly share code, notes, and snippets.

@MPW1412
Last active August 18, 2019 13:38
Show Gist options
  • Select an option

  • Save MPW1412/7723fed61c4d5749533116e9778fcd45 to your computer and use it in GitHub Desktop.

Select an option

Save MPW1412/7723fed61c4d5749533116e9778fcd45 to your computer and use it in GitHub Desktop.
Lazy scanning

Lazy Scanning

Nowadays, pdf documents can be signed digitally. Sadly those have lower acceptance rates than scaned pdf files. The following Bash script turns a pdf document into an emulated, faked scan.

Introduction

If you are too lazy to print and sign documents just like me, this is the right script for you. Instead of printing, signing and rescanning a document, which can easily take 15 minutes or so, you can just fake or emulate a scan with this script.

To make it look real, it converts the document to an image and rotates it a little, just as a cheap consumer ADF scanner would do. Also, converting the images to grayscale has proven to be helpful to make it look most authentic. Just as if it had been printed on a black-and-white only printer.

Preparation

Fill out the given PDF with a program for PDF editing. I suggest Xournal. Export it as pdf to a different filename and run my script on it.

Protip: Make the signature look different each time by rescaling it disproportionally.

Requirements

  • pdftk
  • imagemagick convert

Licence

Author: Matthias P. Walther ([email protected])
Licence: WTFPL.

I'd appreciate mentioning me in the credits.

Have fun

Most people can't tell the difference from a real scan. And if they don't accept the scan via email, send it via goo' ol' fax - there are various online services available.

#!/bin/bash
###
# Author: Matthias P. Walther ([email protected])
# License: WTFPL
pages=""
last_page=""
pdf_file=""
basename=""
main_rotation=""
function set_page_count {
pages=$(pdftk $pdf_file dump_data|grep NumberOfPages|cut -f2 -d' ')
}
function set_last_page {
last_page=$(( $pages - 1 ))
}
function check_parameters {
if [[ $1 == "" ]] || [ ! -s $1 ]
then
echo "Please give PDF file as first parameter"
exit 1
fi
pdf_file=$1
}
function rotate_page {
echo "Rotation: " $2
convert -density 150 -quiet $1 -quality 100 -flatten -type Grayscale -rotate $2 $3
}
function set_main_totaion {
main_rotation=$(shuf -n1 -i 300-500)
}
function get_rotation {
temp_rotation=$(shuf -n1 -i 0-200)
temp_rotation=$(( $temp_rotation - 35 ))
echo '0.'$(( $main_rotation + $temp_rotation ))
}
function set_basename {
basename=$(basename $pdf_file .pdf)
}
function rotate_pages {
for i in $(seq -w 0 $last_page)
do
rotate_page $pdf_file[$i] $(get_rotation) ${basename}_$i.jpg
done
}
function merge_images {
convert ${basename}*jpg -quality 100 -density 150 ${basename}_Scan.pdf
rm ${basename}*jpg
}
function init {
set_page_count
set_last_page
set_basename
set_main_totaion
}
check_parameters $1
init
rotate_pages
merge_images
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment