Magic letterhead tray – making preprinted stationary automatically on plain white paper

A couple of years ago I stopped buying letterheads for my business and instead created a special print queue that merged any document sent to print with a PDF letterhead template.

Last month, it stopped working on just my PC. I’m not quite sure if it broke from a Windows update or something else had changed but the result was I needed to start from scratch and make it all again. My original notes weren’t very good and in 3 years time I’ll look back on these notes and think much the same. If you’re looking to print letterheads on demand too, these notes may help you achieve it.

Getting here took me far to long (days!) but the end result is not having to buy preprinted stationary or an extra tray for our printer.

Summary of what I’m about to do:

  • Create a Print Server (a virtual machine) running linux (Ubuntu) and CUPS (common unix printing system)
  • Connect the Print server to the network printer
  • Create a queue for the printer and share that queue to the network.
  • Within that queue, call a script that turns the print document into a PDF, merge that with a pre created ‘letterhead’ PDF
  • Send that newly merged document to the printer.

Things that are good to know: Issues and solutions:

  • Sometimes windows doesn’t like browsing for printers, giving error .“Couldn’t connect to printer. Check the printer name and try again. If this is a network printer, make sure that the printer is turned on and that the printer address is correct”
    Solution was to open ‘services’ and restart ‘Print spooler’ service
  • I also found using “http:” for printer connections rather than https seemed to help.

Instructions from start to finish (mostly)…

Create virtual server

# We need to resize the logical volume
to use all the existing and free space of the volume group
$ lvm
lvm> lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
lvm> exit
# And then, we need to resize the file
system to use the new available space in the logical volume
$ resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 58
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 120784896 (4k) blocks long.
# Finally, you can check that you now
have available space:
$ df -h
Filesystem                         Size  Used Avail Use% Mounted on
udev                               3.9G     0  3.9G   0% /dev
tmpfs                              786M  1.2M  785M   1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv  454G  3.8G  432G   1% /

Install CUPS

  1. Openssh-server was installed by default, Login and get IP address
    >ip addr show
  2. Install cups
    #sudo apt install cups
  3. Add my local user to cups lpadmin group
    usermod -aG lpadmin <myusername>
  4. Configure cups
    1. I had problems with cups rewriting the config files I’d changed by hand when the server rebooted. The solution is NEVER EDIT THE CUPS CONFIG FILES WHEN THE SERVER IS RUNNING!
      systemctl stop cups.service
    2. Configure following in /etc/cups/cupsd.conf
      Listen *:631 
    3. Comment out the other listen directives. *:631 listens on all connections but you can also restrict to a specific network with
  5. Need allow access to the web GUI and web admin GUI by adding an ‘Allow all’ line to cupsd.conf. Note: This may not be a good idea if you can’t trust every device on your network. In that case I think you can restrict access by IP address or other auth methods.

<Location />
Order allow,deny
  Allow all

<Location /admin>
  Order allow,deny
  Allow all

systemctl start cups.service

  1. Turn off discovery of remote printers
    • There seems to be an issue with remote discovery of printers. They disappear through reboot and maybe other triggers.  So for now I’m manually adding the network printers in the hope they are retained (this has worked so far…)

systemctl stop cups-browsed.service


BrowseRemoteProtocols none
# was BrowseRemoteProtocols dnssd cups

systemctl start cups-browsed.service

Add printers via the CUPS web interface

  1. Go to web interface in browser, http://<ip>:631

Check box “share printers connected to this system”

  + check allow printing from the internet

Nb: it will redirect you to https pages then ask for your username/password as per the account you added to the lpadmin group.
I decided to call my print server “Mars” and setup up print queues using the format ‘server’+’printer model name’+’optional descriptive label’

  1. Add network printers manually to print server.
    1. Administration > Add Printer
    2. Other network printers > Internet Printing Protocol (ipp)
    3. Connection address: “ipp://<ip>:631”  where IP is the printer IP. EG: ipp://
    4. Name “mars”+model+(dash)+description”, eg “marsP6035” for the default printer “marsP6035-letterhead” for magic letterhead, or maybe “marsP6035-duplex” for a forced duplex mode
    5. Leave the description blank (it appears in the  windows print dialog boxes, better for us to have useful description detail as part of the name).
    6. Check the box ‘share this printer’
    7. Use the manufacturer provided PPD file (mine all came from the manufacturers install files, in a sub directory called ../linux/eu/)
    8. Test print to make sure it works

Install PDFtk

  1. Apt install pdftk
  2. disable aparmour, it seems to block the scripts we’re going to create. I guess there’s an easy way to allow them but I don’t know what it is.

AppArmor can be disabled, and the kernel module unloaded by entering the following:
sudo systemctl stop apparmor.service
sudo update-rc.d -f apparmor remove

To re-enable AppArmor in the future enter:
sudo systemctl start apparmor.service
sudo update-rc.d apparmor

Add the magic letterhead code

  1. The knowledge of this came from from but I have slightly modified parts to suit my needs
  2. Copy your Letterhead.pdf artwork into /etc/cups
  3. Setup watermark scripts in /usr/lib/cups/filters named: letterheadwatermark

Chmod 755



tempdir=$(mktemp -d)

echo $(date) “$0 $* (tempdir: $tempdir)” >> $logfile

# Command line arguments

if [ -z “$filename” ] ; then

if [ $# -ge 7 ]; then
        cat $6 > $tempdir/
        cat > $tempdir/

# convert Postscript to PDF
/usr/bin/ps2pdf $tempdir/ $tempdir/ 2>>$tempdir/err

# watermarking.  ‘stamp’ puts letterhead over document, ‘background’ puts it under. Some documents send all white background so letterhead will not show through white areas.
/usr/bin/pdftk $tempdir/ stamp “$watermark” output $tempdir/pdf.out 2>>$tempdir/err

# convert PDF to Postscript
/usr/bin/pdftops $tempdir/pdf.out – 2>>$tempdir/err

# clean-up. I don’t remove the tmp files as they are very useful to find out what stage of this script is failing
# rm -rf $tempdir

From <>

  1. In /etc/cups create a link to the cups filter folder, eases finding things later
    Ln -s /usr/lib/cups/filter/ cups_filter_directory
  2. Copy script into filter folder, chmod 755 & chown to root
  3. Restart again: systemctl restart cups.service
  4. I’m using the kyocera PPD from <myserver>/software/printer then inserting cupsfilter line to make it print a letterhead. I wanted to use the IPP everywhere PPD (default in cups) but that didn’t work. I suspect the IPP everywhere PPD gets the first input as PDF whereas the kyocera PPD gets the first input as postscript. I tried skipping the PStoPDF but that didn’t work either and I stopped searching once I got it working with the kyocera PPD
  5. Modify the printer PPD (/etc/cups/ppd/<your-print-queue.ppd> to call the script.
    systemctl stop cups.service
    then add this as second line:
    *cupsFilter: "application/vnd.cups-postscript 100 letterheadwatermarkpdf"
    systemctl start cups.service

Add the printer to the client

  1. Add printers to clients
    1. Printer and scanners > add printer or scanner
    2. From the printers discovered, select printer, eg: “marsP6035 @ marsprint”. Selecting the @server i’m guessing is selecting the IPP service.
    3. Send a test print, hope it works 🙂
  2. Alternative method that had issues of print being scaled down, perhaps near a5 printing instead of a4 before the stamp/watermark applied. Ps2pdf command seems to be the issue here.
    1. Add printer, The printer that I want wasn’t listed, select a shared printer by name
    2. Paste the url from the printers page on the server, it will be like:
    3. Note: not https (sometimes that seems to fail, not sure why)
    4. Driver: Microsoft > Microsoft PS Class Driver
  3. Add printer directly to computer with manufacturer driver file
    1. This is useful because the Kyocera drivers allow more print options and control, eg paper type, make poster from multiple pages, etc.
    2. Test print… Hopefully this works too !

A Small Business Payroll Solution – how and why I moved our payroll process from an accounting firm to do it ourselves using ‘Software as a Service’

My apologies in advance for the grammar and sentence structure. I haven’t blogged for ages but having just overhauled my small business payroll solution and procedures this post will help me remember what, why and how I did things and also may help someone else going through the same steps.

Starting point:

My small business has 4 employees. Many, many years ago we paid for Sage software’s payroll solution. It cost a lot of money every year. We then moved to ‘Payroll as a Service’ where a local accounting firm charged a £50 per month to do all of our payroll needs and had been doing that for many years. I’d email the accountant our workers hours, variations for the month for sick pay, etc. The accountants would do: all the calculations, report to HMRC, send me a list of journals for Sage Line 50, post printed payslips to us. It worked, things were good.

Why the change:

Sending the accountants the hours and variations each month added a few days between knowing these numbers and when I could actually set things up to pay people. We pay for the whole month on the 26th of the same month. Allowing me time to set up the payments meant I wanted to know the numbers by around the 22nd. To allow time for the processing and weekends I’d be emailing the accountants around the 18th, probably trying to find out how many hours people had done from the 15th. Accounts are human too so allow for the occasional error and time to fix, I started to look for ways to improve the process and reduce the time I needed to wait for numbers.

We’d already moved onto Xero a couple of years ago [1] and they have a payroll module so my starting point was to assume that we’d start using that instead of our accountants.

Planning ahead:

Before changing our processes I like to try and work through all the details. My experience is that no software or system is perfect and my goal is to find out what is likely to go wrong or work in unexpected ways so we can mitigate any impact. As well as Xero I looked at several other payroll software providers, lead mostly by a list of suppliers on the HMRC web site

Xero were out of the frame quickly due to [attitude towards bug reports] + CIS + [the mess of their user forums] + [finding someone having significant payroll issues on a forum and seeing them getting nowhere]. I went through several other vendors and opened up trial accounts with fictitious payroll data to see how they worked.

I was generally not feeling very good about changing at all, until I finally contacted payfit.

I’d excluded them early as they had no prices on their web site and to find out more the only real option was to ‘book a demo’. They appeared a relatively new company and I’d briefly researched them to find they had some <large number to me> venture capital funding a couple of years before. Their move into the UK market was newer still and seeing no prices I thought their target was much larger business than our small business. I also expected their prices to be out of our league but having excluded all other options as being no better than our current process I bit the bullet and got in touch. I was pleasantly surprised! The demo answered all my questions and the software looked like something I could work well with. The price was good too(better than using our accountants) so I decided to switch.

The switching process

Let’s not hide from the fact, payroll is complicated by all the details and rules, the variations and exceptions, the historic requirements and the new things constantly being applied as legislation changes. My switch has been stress free because I’ve been working to keep ahead of things that could trip us up.

Firstly, Payfit provide an onboarding point of contact and they were great at working with me to make sure every detail was complete before the first pay run. I was sent an invite to a secure file sharing platform which contained some spreadsheets for me to complete. They contained placeholders for employee names, rates of pay, hours of work, holiday entitlement, pension provider details and so on. Whilst I could answer most questions for the few I couldn’t I solved by a) Asking payfit for help (every query answered same day) and b) sending copies of my previous months pay slips and journals (the onboarding contact then pointed out several other answers to questions I hadn’t yet asked which was handy). It took me about 2 hours to find all the answers but that was spread over 5 days.

Next was the first pay run, set for 3 days before the pay day. It wasn’t perfect. I found errors like a wrong amount of tax taken on one employee. I messaged payfit and within hours I’d had emails explaining what went wrong and that they had fixed the problem, including regenerating payslips. Yes, within hours! I was very relieved at this point. I think payfit must have been newer than I thought, or maybe no one reports bugs as thoroughly as I do, because one of the issues was that an export of different reports reused the same file name. I reported that and it was fixed the same day! That’s a small detail but the different file names help me automate things as you’ll see later.

Having got everything working well, and repeated a second month, I could start perfecting my automation. I still had a few problems but that was mostly related to me learning how to enter hours, pay rates and sick pay. Once again, Payfit answered questions the same day and reran reports to show corrected information. I also discovered the helpful UI messages within the app, for example, this one

Automation – because I don’t like typing numbers repeatedly.

So, I now have a payroll software as a service application that will generate reports in various formats. Payfit provide a journal template for Xero and several other things but like most businesses I have requirements that are slightly different from the next business. For example, our Xero account numbers are different than the default numbers Xero uses. We want to record some staff as a direct cost and some as an overhead. I also want to take all the numbers from Payfit and set up payments to my employees, tax payments to HMRC, the pension scheme and so on. Less typing means less chance for typping errors, and it’s also very fast 🙂

Here’s my magic Payfit-to-Xero conversion spreadsheet and you’re welcome to use it and modify it to meet your own needs

There are notes throughout the sheet to help you. As a rule, I put formulas in blue text (so I know not to edit them) and Yellow background cells need editing each month.

My process is as follows:

  1. From:
    Payfit > Accounting > Reports > [Set Month!] — Company costs : Employee Pay Structure : Pension contributions
    Download and copy the 3 tables into this spreadsheet
  2. Write the pay date into the yellow box (these words are used to creating meaningful descriptions in journals and bills)
  3. Employers Allowance Calculations Make a note and adjust values if we have any employers allowance left to use.
  4. Go to NEST Pensions web site (our pension provider) and create a schedule based on the numbers in the ‘Pensions’ table.
  5. Data Exports and Imports:
    1. Export spreadsheet tab ImportTemplateBill as a CSV file then import to Xero as a bill
    2. Export spreadsheet tab ImportTemplateJournal as a CSV file then import to Xero as a journal
  6. Approve the draft bills in Xero.
  7. Pay HMRC and Employees in normal way, I use batch payment for the relevant dates and upload the file to our bank in advance
  8. NEST is a direct debit, so ‘view bill’ then pay from bank (without actually going to bank and sending money, so the Direct Debit will match the payment when it happens and it wont be listed on bills waiting to be paid)

If you find the spreadsheet useful or if you use it an improve it leave a comment to help others.

[1] Foot note on Xero: A blessing and a curse.

Xero does the basics fine but I find the CIS implementation is awful (how dare I receive a credit note from a subcontractor in the same month and expect the monthly total to work! – let alone make an input error and use a credit note to make the correction) and when I reported an error with an export format for bank transfers (Xero doesn’t follow the spec – I even sent a link to the specification file on the bank web site) after 12 months of requesting updates on a fix I was told they’re not going to fix as it doesn’t affect enough users. How hard can it be to export a TXT file with a correct date? Generating the wrong date creates a failed import so I have to hand correct each one.

I still like Xero for the basic accounts functions & that out of all the accounts software I tested it was the only one at the time that allowed me to do my own form of project costing (using ‘tagging’ + several custom reports). I hate Xero for it’s poor support, terrible user forum/platform, limitations in report formatting and exports.

How many 1st class stamps to use on heavy letters?

In our office, we buy Royal Mail stamps in ready to use denominations of “1st Class”, “2nd Class”, “Large 1st Class” and “Large 2nd Class”. However, these are only useful for 100g weight letters.

For heavy letters, we can use multiples of these stamps. I noticed I was routinely having to do the calculation in my head as to what is the optimum combination of stamps. For example, what to stick on a 500g 2nd class large letter. I know that’s £1.58, I know the stamps represent the 100g values, so 1st=65p 2nd=56p Large_1st=98p and Large_2nd=76p.

It’s not too hard to work out, but it takes time and gets harder with bigger letters and parcels.

For a coding challenge, I wrote “Stampulator“. It’s a single web page that tells us which combinations of stamps to use, so for the 500g 2nd Class large letter, £1.58 example, we need 1 x 1st class and 1x Large 1st class. That’s over by 5p, but it’s the nearest value to the cost.

I also made it so that if I have different value stamps or a different target value to reach (say; special offer or I’ve been slow to update the values when Royal Mail prices change) I can input those and get an instant result.

I then printed the page and stuck it buy our post box.  Stampulator is on my web server and free to use – it works well from a mobile phone too.

Post a comment here if it’s useful and that’ll encourage me to keep it up to date.