Print Documents as PDF and send it home

Well, I got another Problem.
I’ve installed a linux-remote-desktop. This works really nice, but the windows-Users can’t forward their Printer.

I come up with the solution: Print it as PDF and send it home.

As usual, this will be done on a debian system.

Install & configure the Printer

I’m using the default CUPS-PDF since it’s powerfull enough (and easy to configure)

apt-get update && apt-get install cups-pdf


The next step is optional. The lookup will get it right – but just to be complete:
Now open up the local CUPS config in your webbrowser at “http://localhost:631”. If you are asked for username and password, use root and your user-pw.

Go to Setting ->Printer and add check if there is already a PDF-Printer created. If not – create one.

We now to have the PDF created and then send.
so edit the cups-pdf.conf

nano /etc/cups/cups-pdf.conf

Uncomment “PostProcessing” and add the Path to our script.

For me, it’s like this:

PostProcessing /usr/local/bin/cups-pdf-sendemail

that’s it.

How to Send the E-Mail

I’m using the tool sendEmail (similar to sendmail, since i want to use an SMTP-Server)

apt-get install sendemail

you can now use it like this:

<span class="co4">$ </span>sendEmail <span class="re5">-f</span> <span class="st0">"<span class="es2">$from</span>"</span> <span class="re5">-t</span> <span class="st0">"<span class="es2">$to</span>"</span> <span class="re5">-m</span> <span class="st0">"<span class="es2">$body</span>"</span> <span class="re5">-u</span> <span class="st0">"<span class="es2">$subject</span>"</span> <span class="re5">-s</span> <span class="st0">"<span class="es3">${smtpserver}</span>:<span class="es3">${smtpport}</span>"</span> <span class="re5">-xu</span> <span class="st0">"<span class="es2">$user</span>"</span> <span class="re5">-xp</span> <span class="st0">"<span class="es2">$password</span>"</span> <span class="re5">-a</span> foo.pdf <span class="re5">-a</span> bar.jpg

Create our Email-Script

Open up our cups-pdf-sendmail-Script

nano /usr/local/bin/cups-pdf-sendemail

and add the following:


FILENAME=`basename $1`
DIRNAME=`dirname $1`
DATE=`date +"%Y-%m-%d_%H:%M:%S"`


MSUBJECT="subject of the email"
MBODY="body-text of the email"

echo "================================"
echo "================================"
echo "Filename: " $FILENAME
echo "DATE: " $DATE
echo "MFROM: " $MFROM
echo "MHOST: " $MHOST
echo "MUSER: " $MUSER
echo "MTO: " $MTO
echo "MBODY: " $MBODY

echo "mv " $1 $DIRNAME"/"$DATE".pdf"
mv $1 $DIRNAME"/"$DATE".pdf"

echo "sendemail -f "$MFROM" -t "$MTO" -m "$MBODY" -u "$MSUBJECT" -s "$MHOST":"$MHOSTPORT" -xu "$MUSER" -xp "$MPWD" -a "$DIRNAME"/"$DATE".pdf"
sendemail -f $MFROM -t $MTO -m $MBODY -u $MSUBJECT -s $MHOST":"$MHOSTPORT -xu $MUSER -xp $MPWD -a $DIRNAME"/"$DATE".pdf"

rm $DIRNAME"/"$DATE".pdf"

echo "================================"

So what are we doing here?

We create a few varialbes and print them out.
After that we mv the name of the PDF to some DIRNAME and the actual date (for own reasons – you don’t have to do this)
then i’m sending the actual E-Mail.
If your mail-provider don’t accept TLS, use the option

-o tls=no

And then I’m removing the file to not have it laying on the hard-disk.

That’s all!

The script is a little bit of a shitty one – but it has to be done very fast! ^^’

Leave a Reply

Your email address will not be published. Required fields are marked *