Automatically Print Markdown Files to a Thermal Printer

Markdown to Epson Thermal Printer


Assumptions Software and Equipment

Software

  • Ubuntu Linux
  • pandoc
  • htmldoc
  • Dropbox

Hardware

  • Epson TM-T20II Thermal receipt printer
  • Receipt paper (80 mm wide)

Your Mind (final requirements)

  • You have a brain and use Google or another search engine for research
  • You are familiar with Linux
  • You have already installed the drivers for your printer
  • You have already set up and configured Dropbox on your Linux server
  • For this documentation effort, the Linux printer is named Epson

File Generation Steps

  1. Convert markdown to HTML
  2. Convert HTML to a pdf
  3. Print the PDF to a printer

There are three steps because printing HTML to a printer using the lp command will print the HTML code and not the result of the HTML. You can use pandoc to convert markdown to a PDF. I had more control using htmldoc to generate the final printable PDF file. PDF is better than postscript because of file size.

Examples

  • FILE.md is the markdown file
  • FILE.html is the intermediate html document
  • FILE.pdf is the final print-ready file
  • Epson is the CUPS printer name
  • USERNAME is your user account on the Linux system

Convert Markdown to HTML using pandoc

 pandoc FILE.md \ -o FILE.html \ --wrap=preserve \ -f markdown \ -t html \

Convert HTML to PDF

htmldoc -f FILE.pdf FILE.html \ --no-toc \ --no-title \ --no-numbered \ --header . \ --footer . \ --top 1mm \ --bottom 1mm \ --left 3.5mm \ --right 5mm \ --size 79.5x297mm \ --embedfonts \ --gray

Print to Epson TM-T20II Thermal Printer

lp -d Epson FILE.pdf


Automation

Write a script to convert markdown and print it

Script Location

/usr/local/bin/autoprint_markdown Make sure the permissions are set to execute (chmod +x)

Script Contents

#!/bin/sh fname="$1"
if [ $(file -b --mime-type "$fname") != "text/plain" ] ; then exit 0 fi
pandoc "$fname" -o "$fname.html" \ --wrap=preserve \ -f markdown \ -t html
htmldoc -f "$fname".pdf "$fname.html" \ --no-toc \ --no-title \ --no-numbered \ --header . \ --footer . \ --top 5mm \ --bottom 5mm \ --left 5mm \ --right 5mm \ --size 80x297mm \ --embedfonts \ --gray
lp -d Epson "$fname.pdf"
chown USERNAME "$fname" chgrp USERNAME "$fname" chown USERNAME "$fname.html" chgrp USERNAME "$fname.html" chown USERNAME "$fname.pdf" chgrp USERNAME "$fname.pdf"
mv "$fname" /home/USERNAME/Dropbox/IFTTT/markdown_complete/ mv "$fname.html" /home/USERNAME/Dropbox/IFTTT/markdown_complete/ mv "$fname.pdf" /home/USERNAME/Dropbox/IFTTT/markdown_complete/
find /home/USERNAME/Dropbox/IFTTT/markdown_complete/* -mtime +0 -exec rm {} \;

Set up incrontab entry

Script Location: incontab

/etc/incron.d/autoprint_markdown

Script Contents

/home/USERNAME/Dropbox/IFTTT/markdown_print/ IN_CLOSE_NOWRITE,IN_NO_LOOP /usr/local/bin/autoprint_markdown $@/$#

Restart icon.d

sudo service incron restart


Enjoy

Files dropped into the markdown folder will print!

I am using IFTTT to send files to a Dropbox folder monitored by my Linux system.

Scroll to Top