LaTeX 在 php 中的执行和路径问题

发布于 2024-10-24 08:34:37 字数 410 浏览 2 评论 0原文

我正在尝试使用 exec 从 php 编译乳胶源文件:

echo shell_exec("/usr/texbin/pdflatex source.tex");

不幸的是,当通过 php 调用乳胶时,乳胶似乎看不到所有包

例如,

LaTeX Error: File `customclass.cls' not found

当我尝试使用安装在本地 texmf 文件夹中的 customclass 时,就会出现此问题。安装在其他地方的一些软件包也存在同样的问题。

这肯定与路径变量或类似的设置有关,但我一个小时都找不到什么。

有人有主意吗?

I am trying to compile a latex source file from php, using exec:

echo shell_exec("/usr/texbin/pdflatex source.tex");

Unfortunately, latex doesn't seem to see all packages when it is called via php.

For example, I get

LaTeX Error: File `customclass.cls' not found

when I try to use customclass, installed in my local texmf folder. There is also the same problem for some packages installed elsewhere.

This has certainly something to do with a path variable or something like that to set up, but I haven't been able to find what for an hour.

Has someone an idea ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

北斗星光 2024-10-31 08:34:37

PHP 解释器可能以其他用户身份运行,例如 www-data 或相关的东西:这意味着它看不到安装在您常用用户的 texmf 目录中的包(我假设这就是您所说的本地),因为仅当 pdflatex 以该用户身份运行时,才会加载用户的 texmf。

这似乎是一个潜在的解决方案,可以根据 shell 变量将 LaTeX 路径扩展到本地 texmf 所在的位置: 临时安装(La)TeX 文件(来自 TeX FAQ)。

The PHP interpreter is probably running as some other user, like www-data or something related: this means that it can't see the packages installed in your usual user's texmf directory (I'm assuming that's what you mean by local), because the user's texmf is only loaded when pdflatex is run as that user.

This seems like a potential solution to extending the LaTeX path to wherever your local texmf is, based on shell variables: Temporary installation of (La)TeX files (from the TeX FAQ).

你的呼吸 2024-10-31 08:34:37

您可以将 *.cls 文件放入与 source.tex 相同的目录中。如果您随后将目录更改为“当前目录”,则在启动 Latex 时,Latex 解释器也会找到该目录并用于编译 Latex 文件。

这也是与 php 一起使用的更好的解决方案,因为您不希望应用程序的用户将某些内容安装到 www-data 用户的主目录中。出于安全原因,这可能会被禁止。

因此,解决方案是:

  • 将 source.tex 放入名为 Latexfiles 的目录(或您选择的名称)中
  • 将您的 *.cls 文件放入 Latexfiles
  • 使用以下代码编译您的 Latex 文档:
passthru('cd /path/to/latexfiles/; pdflatex source.tex', $r);
回声 $r; 

You can put your *.cls files into the same directory as source.tex. If your then change the directory to be the "current directory", when starting latex, then it also will be found by the latex interpreter and used for compiling your Latex file.

This is also a better solution for using with with php, as you would not want to make the user of your application install something into the home directory of www-data user. This might be forbidden for security reasons.

So, the solution is:

  • put source.tex into directory called latexfiles (or a name of your choice)
  • put your *.cls files to latexfiles
  • use the following code to compile your latex document:
passthru('cd /path/to/latexfiles/; pdflatex source.tex', $r);
echo $r; 
凉宸 2024-10-31 08:34:37

/Users/My/Sites/tex/index.php 文件的来源如下。例如,让它可以通过 http://localhost/~My/tex/index.php 链接访问。

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>PDF file compillation</title>
</head>
<body>
<?php 
ini_set('safe_mode', 'Off');
$output = array();
/*
   /usr/texbin/ - directory, where the pdftex exists
   /Users/My/Sites/tex/output - directory for test.pdf and everything else. This directory have to have permissions to write.
   /Users/My/Sites/tex/test.tex - source .tex file
*/
exec("/usr/texbin/pdftex --shell-escape --synctex=1 -output-directory=/Users/My/Sites/tex/output /Users/My/Sites/tex/test.tex", $output);
if($output){
    echo("<h3>Console output</h3><pre>".implode("\n", $output)."</pre>");
/*
    /Users/My/Sites/tex/output/test.pdf - the result file after compilling
*/
    echo('<p>Go to compilled <a href="http://localhost/~My/tex/output/test.pdf">PDF file</a></p>');
}else{
    echo('<h3>Error</h3><p>Shell script execution failed.</p>');
}
?>  
</body>
</html>

The source of /Users/My/Sites/tex/index.php file is bellow. For example let it be reachable by http://localhost/~My/tex/index.php link.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
        <meta http-equiv="content-type" content="text/html;charset=utf-8" />
        <title>PDF file compillation</title>
</head>
<body>
<?php 
ini_set('safe_mode', 'Off');
$output = array();
/*
   /usr/texbin/ - directory, where the pdftex exists
   /Users/My/Sites/tex/output - directory for test.pdf and everything else. This directory have to have permissions to write.
   /Users/My/Sites/tex/test.tex - source .tex file
*/
exec("/usr/texbin/pdftex --shell-escape --synctex=1 -output-directory=/Users/My/Sites/tex/output /Users/My/Sites/tex/test.tex", $output);
if($output){
    echo("<h3>Console output</h3><pre>".implode("\n", $output)."</pre>");
/*
    /Users/My/Sites/tex/output/test.pdf - the result file after compilling
*/
    echo('<p>Go to compilled <a href="http://localhost/~My/tex/output/test.pdf">PDF file</a></p>');
}else{
    echo('<h3>Error</h3><p>Shell script execution failed.</p>');
}
?>  
</body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文