Search code examples
djangoherokudjango-templateslatexpdflatex

How to install LaTeX class on Heroku?


I have a Django app hosted on Heroku. In it, I am using a view written in LaTeX to generate a pdf on-the-fly, and have installed the Heroku LaTeX buildpack to get this to work. My LaTeX view is below.

def pdf(request):
    context = {}
    template = get_template('cv/cv.tex')
    rendered_tpl = template.render(context).encode('utf-8')  
    with tempfile.TemporaryDirectory() as tempdir:
        process = Popen(
            ['pdflatex', '-output-directory', tempdir],
            stdin=PIPE,
            stdout=PIPE,
        )
        out, err = process.communicate(rendered_tpl)
        with open(os.path.join(tempdir, 'texput.pdf'), 'rb') as f:
           pdf = f.read()
    r = HttpResponse(content_type='application/pdf')  
    r.write(pdf)
    return r

This works fine when I use one of the existing document classes in cv.tex (eg. \documentclass{article}), but I would like to use a custom one, called res. Ordinarily I believe there are two options for using a custom class.

  1. Place the class file (res.cls, in this case) in the same folder as the .tex file. For me, that would be in the templates folder of my app. I have tried this, but pdflatex cannot find the class file. (Presumably because it is not running in the templates folder, but in a temporary directory? Would there be a way to copy the class file to the temporary directory?)

  2. Place the class file inside another folder with the structure localtexmf/tex/latex/res.cls, and make pdflatex aware of it using the method outlined in the answer to this question. I've tried running the CLI instructions on Heroku using heroku run bash, but it does not recognise initexmf, and I'm not entirely sure how to specify a relevant directory.

How can I tell pdflatex where to find to find the class file?


Solution

  • I ended up finding another workaround to achieve my goal, but the most straightforward solution I found would be to change TEXMFHOME at runtime, for example...

    TEXMFHOME=/d pdflatex <filename>.tex
    

    ...if you had /d/tex/latex/res/res.cls.

    Credit goes to cfr on tex.stackexchange.com for the suggestion.