Apache serving LaTeX files as rendered PDFs using a Bash script handler

/* CRUDE */

I thought I’d start logging my daily [geekie] explorations. I’m comming out at this and believe this will be crappy at some degree. I’m also guessing I’ll be better at this over time. This is first time out so bare with me.

I had this idea for years. To have apache render LaTeX files as rendered PDF files. Many have had the idea but their often met by objections that the Tex family programs use and produce files beside the rendered file.

I’m using

  • Apache web server
  • Bash a the script engine
  • LaTeX as my renderer. I could make this fancier by grep’ing the .tex file for clues like “\documentclass” and “\usepackage{fontspec}” but this is just a PoC and not a full-fledged technical sollutio.

My solution builds on

  • A LaTeX rendering CGI script.
  • Some Apache configuration.
  • Using the *nix mktemp command and LaTeX’s –output-directory option for the rendering.

In my daily doings I’ a Java-ist by birth and a terrible Bash’er so comments on my scripting style are probably justifies.

I had to check up on error-handling, the old HTTP environment variables as well as bash file tests and more.

The bash script goes, however, as follows:

#!/bin/bash -e

STATUS500="Status 500 internal server error"
STATUS404="Status 404 file not found"


function removeTempDirectory {
  [ -d "$DIR" ] && rm -Rf $DIR
}

function die {
    removeTempDirectory
    printf "$*\n\n" 1>&2
    trap - EXIT
    exit 1
}

PDFLATEX=`which pdflatex`
PDFLATEX_OPTIONS="-halt-on-error"
[ -x "$PDFLATEX"  ] || die $STATUS500

DIR=`mktemp -d 2>/dev/null || mktemp -d -t 'cgilatex'`
[ -d "$DIR" ] || mkdir -p $DIR || die $STATUS500

[ -f $PATH_TRANSLATED ] || die $STATUS404

$PDFLATEX -halt-on-error \
    -interaction=nonstopmode \
    -output-directory=$DIR \
    $PATH_TRANSLATED &> /dev/null || \
    die $STATUS500

printf "Content-Type: application/pdf\n\n"
cat $DIR/*.pdf

removeTempDirectory
exit

The extra Apache configuration to let the web server serve .tex files through the bash script is done locally for this project but could just as well have been made globally in the httpd.conf.

<Directory "/.../cgilatex">
    Options +Indexes +ExecCGI
    Action pdflate-action /cgilatex/cgilatex.cgi
    AddHandler pdflate-action .tex
</Directory>

The example LaTeX document was a hand crafted Lorem ipsum and goes something like this

\documentclass[a4paper,12pt]{article}
\title{CGILatex}
 \author{Mats Nyberg}
 \begin{document}
\maketitle

 Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do
 eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad
 minim veniam, quis nostrud exercitation ullamco
laboris nisi ut
 aliquip ex ea commodo consequat. Duis aute irure
dolor in
 reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla
 pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in
 culpa qui officia deserunt mollit anim id est
laborum."
 
 Lorem ipsum dolor sit amet, consectetur adipiscing
elit, sed do
 eiusmod tempor incididunt ut labore et dolore magna
aliqua. Ut enim ad
 minim veniam, quis nostrud exercitation ullamco
laboris nisi ut
 aliquip ex ea commodo consequat. Duis aute irure
dolor in
 reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla
 pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in
 culpa qui officia deserunt mollit anim id est
laborum.
 \end{document}
 

Entering

$ PATH_TRANSLATED=test.tex ./cgilatex.cgi # spews out a PDF text
$ PATH_TRANSLATED=test2.tex ./cgilatex.cgi # returns a 404 status
$ PATH_TRANSLATED=error.tex ./cgilatex.cgi # returns a 500 status

to see the three different cases that could occur.

References

https://github.com/jollobajano/cgilatex

http://linuxcommand.org/wss0150.php

http://linuxcommand.org/wss0160.php

http://tldp.org/LDP/abs/html/fto.html

 

Leave a comment