Automatically Print Markdown Files to a Thermal Printer

Markdown to Epson Thermal Printer

Thermal Printer

YouTube Demonstration video

Assumptions Software and Equipment

Software

  • Ubuntu Linux
  • Pandoc
  • htmldoc
  • Dropbox

Hardware

Your Mind (final requirements)

  • You have a brain and use Google or other search engine for research
  • You are familiar with Linux
  • You have already installed the drivers for your printer
  • You have already setup 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 found that 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

[code lang="bash"]
pandoc FILE.md \
-o FILE.html \
--wrap=preserve \
-f markdown \
-t html \
[/code]

Convert HTML to PDF

[code lang="text"]
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
[/code]

Print to Epson TM-T20II Thermal Printer

[code lang="text"]
lp -d Epson FILE.pdf
[/code]

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

[code lang="bash"]
#!/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 {} \;
[/code]

Set up incrontab entry

Script Location: incontab

/etc/incron.d/autoprint_markdown

Script Contents

[code lang="text"]
/home/USERNAME/Dropbox/IFTTT/markdown_print/ IN_CLOSE_NOWRITE,IN_NO_LOOP /usr/local/bin/autoprint_markdown $@/$#
[/code]

Restart icon.d

[code lang="text"]
sudo service incron restart
[/code]

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