How can I get PHP to compile a LaTeX document if i

2019-01-19 20:24发布

问题:

I have a PHP script that compiles LaTeX documents with the use of:

exec('cd /path/to/doc && /usr/bin/latexmk -pdf filename.tex');

This is working for some of my LaTeX documents but my latest document doesn't compile and a look at the log reveals:

!pdfTeX error: pdflatex (file ecrm1000): Font ecrm1000 at 600 not found
==> Fatal error occurred, no output PDF file produced!

Which I have found is the result of LaTeX not being able to see the required font packages. When I run the same compile command under my username the document compiles as it should. So my question is, how can I get PHP (executing as www-data) to get access to the necessary LaTeX packages?

I have tried installing the required package under the www-data account using:

sudo -u www-data sudo apt-get install texlive-fonts-recommended

but it askes for www-data's password, which I don't believe was set by me and isn't anything I've thrown at it.

I'm running Ubuntu 12.04 if it's any help.

回答1:

sudo mktexpk --destdir /usr/share/texmf-texlive/fonts/pk/ljfour/jknappen/ec/ --mfmode / --bdpi 600 --mag 1+0/600 --dpi 600 ecrm1000

... makes the font available for all users (same as in missingfont.log but with --destdir argument).



回答2:

You are right with the sudo idea, but to get around the password problem, there is a simple solution.

www-data must be in the sudoers list, at etc/sudoers, and within this list you can define how the super commands are controlled. It is possible to set www-data as a sudoer that doesn't require a password... but that's not a very clever idea in terms of security. Therefore, in the sudoers list, you can specify what commands are allowed to be executed by the user. Allowing only the /usr/bin/latexmk command will get around security problems, and allow your webserver to run super commands without a password.

Check out this help page for Ubuntu: https://help.ubuntu.com/community/Sudoers - most notably the NOPASSWD section.

The example in your case would be:

# This lets "www-data" user run a command without a password
www-data mark-computer= NOPASSWD: /usr/bin/latexmk


回答3:

Here's what eventually got the compilation working.

Under my own home folder was a hidden folder that LaTeX was using to store the fonts it needed ~/.texmf-var. This folder contained fonts/pk/ljfour/jknappen/ec and in there were 13 font files (some of which were the ones that latexmk was complaining about).

The solution was to have PHP generate the LaTeX file I wanted to compile (file.tex) into a temp directory (where I had copied the fonts) where they would be compiled and then moved to where I wanted to store the output PDFs. I'll explain with an example:

file_put_contents('~/website/latexFiles/temp/file.tex', $latex_data);
exec('/usr/bin/latexmk -pdf -cd ~/website/latexFiles/temp/file.tex');
exec('mv ~/website/latexFiles/temp/file.pdf ~/website/pdfs/desiredFilename.pdf');
exec('rm ~/website/latexFiles/temp/file.*');

So the first line generates the latex file in the directory where I'm going to compile to PDF (where $latex_data is a string containing the LaTeX document I want to create).

Now, the part that makes this all work is creating a directory in ~/website/latexFiles/temp called $HOME. Then copying (or maybe linking, but I copied) ~/.texmf-var into ~/website/latexFiles/temp/$HOME. This is the path that latexmk will look into for the fonts it needs. Use the -cd switch so that latexmk moves into ~/website/latexFiles/temp before trying to compile the LaTeX file, where it will then be able to fine the fonts.

The third line copies the .pdf file out to where I eventually wanted the output file and the last line removes all the remaining files from the temp directory with the same filename. I make sure I use unique filenames for the .tex files so I'm not removing files that another user may be generating.

Hopefully someone else finds this useful.



回答4:

I run with similar problem and it was solved by setting $HOME environment variable to /var/www in a shell-script which invoked pdflatex (or I believe any folder writeable to www-data is ok here). See the details on TeX.SX.