How handy would it be to have a shared network folder where you can drop a PDF to print and it just magically pops out of the printer a couple of mins later? Especially since you can print to PDF from pretty much any platform or device..including mobile phones and tablets. Pretty handy I think! Especially if you can do so with pretty much any old (or even ancient) printer rather than having to buy a funky new bluetooth/wireless one.
Here’s how to do just that…
Background…(you can skip this bit if you like)
Ok so we had an interesting situation the other week where my trusty Canon IP4200 printer which I’ve had pretty much forever suddenly stopped working. Shit.
However, when I got it I’d been printing a LOT onto DVDs so was going through printers like most people go through toilet roll. So clever me, when I bought the IP4200, since I happened to get it reconditioned and at a great price, I bought a spare one at the same time and stuck it in the loft for the time when the first one inevitably failed.
Well years passed and printer #1 just kept on truckin’ and then I stopped printing DVDs and it got relegated to printing the occasional ebay postage label and the odd document now and then for my partner. It was used very rarely but occasionally we DO need a printer.
So anyway I untangled the dead Canon from the wiring spaghetti it inhabited, retrieved the backup from the attic and plugged it in, swapped the cartridges over and…wohoo! all working \o/.
Well nearly. I’d forgotten to swap the print head over. No worries, cartridges out, whipped the print head out of printer #1 and fitted to printer #2, refitted the carts and we’re off to the races…or not.
Now #2 was behaving exactly the same way as #2 had done (i.e. nothing).
A little bit of googling later – it turns out apparently those Canons are prone to the printer head (replaceable unit) going, which kills the logic boards in the printer body. So by swapping my dead printer head into the new printer body I’d just killed printer number 2. Shit!
I resent having to buy printers at the best of times so figured someone in the family must have an old printer knocking about we could use. It only needed to be able to print black and white anyway so really anything would do.
So I spoke to my Dad and as I’d hoped he had a couple of spare printers which had become obsolete during his recent windows 10 upgrade frenzy…they were working perfectly fine but windows didn’t wanna know anymore so they were doomed for the scrapheap.
Enter our new (ancient) HP deskjet 3325…
After a bit of fiddling I established it not only didn’t play with windows 10, but also didn’t work with windows 7. Whilst it allegedly worked with XP and we do still have the occasional XP machine they’re tucked away in server cabs so placement wouldn’t really have been handy…so I figured there had to be a better way.
Linux to the rescue (as ever)
I think you reach a point in your IT career where most things can be solved with a linux machine and a bit of bash scripting…and this was very much one of those occasions.
I have a couple of Raspberry Pi machines which run various household tasks (torrent/TV server, CCTV server etc) so figured I’d put one of those to use, which as it turned out was actually very straightforward.
First you will need to install CUPS (common unix printing system). I won’t cover that here since there’s lots of tutorials on it. I did it on a raspberry pi and primarily used info from here:
And there’s some additional info on administering CUPS here:
Ok so if you follow those instructions you should hopefully get your printer installed and able to print a test page from the CUPS admin page.
In the past when my partner needed something printing she’d print it to PDF from her laptop, tablet or whatever and email it to me, since the printer was attached to my PC. This is a process which has worked pretty well for us for a number of years. The plan was to continue this format but in a slightly more automated way.
So the first thing to do is fire up a console on your linux machine (I was using SSH remotely on a Raspberry Pi) and find out the name of your installed printer:
NOTE: As usual – things you type will be in bold, important stuff or stuff you’ll need to change in pink.
[email protected]:~ $ lpstat -p -d printer HP_deskjet_3320_USB_HU31S1R1M34I_HPLIP is idle. enabled since Sun 05 Mar 2017 21:24:06 GMT no system default destination
the name of your printer should be where the pink text is here. If not, go back and retry installing CUPS or your printer *on* CUPS.
Groovy, so the next thing to figure out is how to print a PDF from the linux command line…for which I culled info from here:
I ended up having to use a line like this:
[email protected]:~ $ lp -d HP_deskjet_3320_USB_HU31S1R1M34I_HPLIP blahblah.pdf
where “HP_deskjet_3320_USB_HU31S1R1M34I_HPLIP” was the name of the printer as listed in CUPS and blahblah.pdf is the PDF file to print.
For this to work you’ll probably also want to set up SAMBA too so that windows machines can easily drop PDFs into the shared folder. Ironically it’s less of an issue from phones/tablets since they seem to support sftp natively (e.g. ES File Explorer on android). There’s info on setting up Samba here:
Bringing it all together…
ok so by now hopefully you have a printer running via CUPS on your linux machine, a samba share which all your machines can access from the network..time for a couple of lines of Bash magic to make it all work…
open up a new file called autoprint.sh
[email protected]:~ $ sudo nano autoprint.sh
and enter the following:
#!/bin/bash cd /home/pi/to_print for d in *.pdf ; do echo "Processing file: $d" lp -d HP_deskjet_3320_USB_HU31S1R1M34I_HPLIP "$d" rm "$d" echo "DONE: $d" done
Replace the printer name in pink with the name of your printer you found out from doing lpstat earlier, and you will also need to change the first line “cd /home/pi/to_print” to match whatever folder you designated as your shared folder.
Then CTRL+X and yes to exit & save.
We need to make the script executable so do:
[email protected]:~ $ sudo chmod +x autoprint.sh
What this script does is changes to your “to print” folder and iterates through all the pdf files in the specified folder and prints them, then deletes the PDF after printing. Simples.
Obviously use this as a “temp” folder, don’t put the only copy of your PDF you want to print in there. since it will get deleted. If you want two copies, put two copies of the file in there. The script doesn’t care about the filename as long as it’s a PDF.
You can test the script by dropping a PDF in the to_print folder and running the script manually:
[email protected]:~ $ ./autoprint.sh
The printer should start doing its thing. If not – check you’ve got your paths set up right.
Once the script is working, the last thing to do is to add a line to cron to make the linux machine periodically run the script…and then we’re set!
So open up the cron:
[email protected]:~ $ sudo nano /etc/crontab
and towards the end add the following line:
*/3 * * * * pi /home/pi/autoprint.sh
If you’re running on a raspberry pi you can leave the “pi” bit as is, otherwise you will need to put whatever user account you want to run it under. You will probably also need to adjust the path to point to wherever your script is located..otherwise obviously it won’t run.
The */3 at the start means that the script will run every 3 mins.
So now with a bit of luck you should be able to browse to the shared folder on the Raspberry pi (or whatever linux machine you used), drop a PDF in the “to print” folder and after a few mins the printer should start doing its thing.