arthur.pizza

× Home About Mastodon PeerTube YouTube Patreon
arthur.pizza

The Importance of Shell Scripts and CLI Tools!

Many people overlook the practical value of command-line tools, and demonstrating their usefulness, whether through scripting or combining them, can serve as a compelling real-world example of their power. This little script is probably completly silly, and I’m sure there’s a million better ways to do what I want to do.

I write most of my documents in Markdown. It’s simple, flexible, and gets the job done effectively without unnecessary complexity

I need to keep things lightweight and portable. I like to have the same tools, if I can, on my phone as well as my computers. While Pandoc requires LaTeX for PDF generation, LaTeX is a bit overwhelming and large for my workflow, I’ve found wkhtmltopdf * to be a great alternative. It’s compact, converts HTML to PDF reliably, and even supports CSS styling. Also, wkhtmltopdf can work within Pandoc.

The reason behind my scripting, is that the default CSS for wkhtmltopdf is kinda weird. It leaves a margin of almost 25% around the sides. So the first bit I add to the HTML is some CSS to make the margins more ideal.

* {
    margin: 0px;
}

Another issue I have is with utf-8. I have no idea why, but certain elements get mangled if I don’t set the HTML document to <meta charset="utf-8">.

My terrible script

I’m running Debian 13, as of this writing. To make all these tools work I need to install Pandoc from my Debian repos, and ether grab a binary from the wkhtmltopdf Git or complile it. As of this document I was able to grab a .deb file that installed.

The first thing we need the script to do is echo the meta and style tags to the html document. This has to happen first so it’s in the head of the document. I’m setting the temp file as .temp.html so that it’s invisible to the user.

echo '<meta charset="utf-8">' > .temp.html
echo "<style> * { margin: 0px; } </style>" >> .temp.html

Notice the first line is piped with a single > and the second line >>. The single arrow will over write the file if it exists, where as the double arrow will append. This is also a good idea, as it’ll clean the existing .temp.html if the script ever fails.

Next we want to have Pandoc convert the input document to html without overwriting any html tags we need.

pandoc example.markdown --wrap none >> .temp.html

Once again, we’re taking advantage of the >> pipes.

We can combine thses small one-liners with a simple variable for the input:

#! /bin/sh

FILENAME="${1%.*}"

# Create the file with the header info first
echo '<meta charset="utf-8">' > .temp.html
echo "<style> * { margin: 0px; } </style>" >> .temp.html

# Append the Pandoc conversion to the BOTTOM of that file
pandoc "$1" --wrap none >> .temp.html

# Convert to PDF
wkhtmltopdf --page-size Letter .temp.html "$FILENAME.pdf"

# Clean up temp files
rm .temp.html

This so far has worked great for me. Again, there are lots of ways to do this, that are probably a lot easier and modern. One could probably turn this into a shell funtion in their .bashrc/.zshrc and not have to have another file on the system.

Personally, I’ve added the ~/.local/bin folder to my $PATH so any script I drop in there is global. I like doing it that way so I can share or sync my little scripts.


* Some might notice that wkhtmltopdf is no longer being developed, but I think as long as we have the source code, we should be able to build it. You mileage may vary.