Giter VIP home page Giter VIP logo

cgilatex's Introduction

cgilatex

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

I thought I'd start logging my daily [geekie] explorations. This is first time out so bear 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 as 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

DIR=mktemp -d 2>/dev/null || mktemp -d -t 'cgilatex' PDFLATEX=which pdflatex

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

function die { removeTempDirectory printf "Content-Type: text/plain\n\n" echo "$1" 1>&2 echo "$@" 1>&2 trap - EXIT exit 1 }

trap die INT #TERM

[ -d "$DIR" ] || mkdir -p $DIR || die "could not make temp dir" [ -f $PATH_TRANSLATED ] || die "file $PATH_TRANSLATED not found" OUT=$PDFLATEX -output-directory=$DIR $PATH_TRANSLATED || die $OUT

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 "/Users/mats/Sites/cgilatex"> Options +Indexes +ExecCGI DirectoryIndex index.cgi AddHandler cgi-script .cgi

Action pdflate-action /cgilatex/cgilatex.cgi
AddHandler pdflate-action .tex

The example LaTeX document was a hand crafted 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}

References

http://linuxcommand.org/wss0150.php

http://linuxcommand.org/wss0160.php

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.