I'm looking to build a solution that accomplishes the following:

  1. A client send specifications for a file from a server
  2. A TeX file is assembled from a "pool of TeX fragments" according to these specifications
  3. The assembled TeX file is compiled to pdf
  4. The pdf is served to the client

It is important to notice that the client is not allowed to edit the file at the TeX level. As far as the UX goes, the user just downloads a pdf.

But, the pdf can not be precompiled since I'm looking to serve "families" of files made out of different choices of the mentioned "TeX fragment pool".

What would be available solutions to accomplish this ?

What about this ? Anyone ever tried to deploy it ? https://manuels.github.io/texlive.js/

  • 2
    It doesn't sound too difficult, but the scope of your question seems to be more about the server-side implementation and less about building the TeX document itself. Maybe your question is served better over at the sister site stackoverflow.com ? – Huang_d May 12 '18 at 17:40
  • Yes, indeed. But for me it was worth trying here first. – cladelpino May 12 '18 at 17:45
  • 2
    If you have a server, you don't need texlive.js. You can just use any regular TeX compiler (pdfTeX / XeTeX / LuaTeX, whatever you use). Just run it inside a sandbox for security reasons. – ShreevatsaR May 12 '18 at 18:02

I think I know what you're asking

This will be a stack overflow style answer, but you could set up a server that invokes pdflatex. There are many server providers out there like https://www.digitalocean.com/ and Amazon Web Services. You'd set up a linux server and follow some guide on how to install LaTeX on ubuntu. Then you could make a small script, in e.g. Python, PHP, Ruby or which ever language you want on from the server.

To give you an idea for how this could be implemented in python, then a script could be something along these lines (NB! Not tested. Mostly a modified script for a Slack-bot).

    from string import Template
    import os
    import subprocess
    import tempfile
    from fpdf import FPDF

    from flask import Flask, Response, request
    import requests as py3reqs
    app = Flask(__name__)
    @app.route("/toTex/<theTex>", methods=['GET'])
    def render_latex(theTex):
        with tempfile.TemporaryDirectory() as work_dir:
                str2pdf(theTex, work_dir)
                return send_from_directory(directory=work_dir,
            except Exception as e:
                return "Invalid LaTeX?\n{}".format(request.form['text'])

    def str2pdf(input_string, work_dir):
        with open('template.tex','r') as f:
            s = Template(f.read())
        out_txt = s.substitute(my_text=input_string)

        with open(os.path.join(work_dir, 'out.tex'),'w') as f:
            subprocess.check_call(['pdflatex', '-pdf', '-halt-on-error', 'out.tex'], cwd=work_dir, stdout=None, stderr=None)

    if __name__=="__main__":
        app.run("", port=int(port))

Now you can connect to your server and download the TeX-file. Since the end user is not supposed to change the TeX, then this obviously have to be changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.