【问题标题】:How to pass LaTeX code through a variable to pdflatex in PHP?如何通过变量将 LaTeX 代码传递给 PHP 中的 pdflatex?
【发布时间】:2017-05-05 11:06:14
【问题描述】:

我试过这个: (.php 文件)

<?php
$texscript = "
\documentclass[12pt,a4paper]{article}
\usepackage{helvet}
\usepackage{times}
\usepackage{multido}
\usepackage{fancyhdr}
\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}

\fancyhf{} % clear all header and footer fields
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
%\rhead{{\large\bfseries\thepage}}
\rhead{{\fbox{\large\bfseries\thepage}}}
\setcounter{page}{1}
\multido{}{1383}{\vphantom{x}\newpage}
\end{document}
";

$pdflatex = shell_exec("echo \$PATH > myenv; pdflatex ".$texscript." ");
?>

我得到一个错误:

pdflatex :这是 pdfTeX,版本 3.14159265-2.6-1.40.16 (TeX Live 2015/Debian)(预加载格式=pdflatex)已启用 \write18。 **!终端上的文件结束...

为什么?

上面给出的代码是在PHP变量中编写LaTeX并将其传递给pdflatex命令的正确方法吗? 如果不是,那么请建议更正! 我不想调用外部 .tex 文件,而是在 PHP 文件本身中编写 LaTeX 代码并从那里运行。

【问题讨论】:

  • 1. ""(双引号)中的代码需要转义,或者您使用 ''(单引号)代替。 2. pdflatex 需要一个文件或通过管道输入,如cat t.tex | pdflatex 3. 尝试将输出引导到另一个您有权使用-output-directory 命令的-output-directory 选项写入的目录。或授予在同一目录上写的权利
  • 如果可能,也考虑更新,因为您的版本已经超过一年了
  • 我试过单引号..仍然给出同样的错误
  • 实际上,如果我提供一个包含相同乳胶代码的外部 .tex 文件,它运行良好。
  • 参数文件和字符串不一样。试着在cli上做:pdflatex $(cat t.tex )

标签: php variables escaping shell-exec pdflatex


【解决方案1】:

转义可能是一部分,另一部分是调用命令pdflatex;我绕过(第一个)通过在之前写入文件和(第二个)通过替换变量上的 cmets 和换行符来避免pdflatex 不再有问题......

<?php

error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

$texscript = '\documentclass[12pt,a4paper]{article}
\usepackage{helvet}
\usepackage{times}
\usepackage{multido}
\usepackage{fancyhdr}
\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry}
\renewcommand{\familydefault}{\sfdefault}
\begin{document}

\fancyhf{} % clear all header and footer fields
\renewcommand{\headrulewidth}{0pt}
\pagestyle{fancy}
%\rhead{{\large\bfseries\thepage}}
\rhead{{\fbox{\large\bfseries\thepage}}}
\setcounter{page}{1}
\multido{}{1383}{\vphantom{x}\newpage}
\end{document}
';

//write code to file
$fn="texfile.tex";
$myfile = fopen($fn, "w");
fwrite($myfile, $texscript);
fclose($myfile);

//then execute pdflatex with parameter
//didnt need
//echo $PATH > myenv;
$pdflatex = shell_exec('pdflatex ' .$fn .'' );

//var_dump( $pdflatex );
//test for success
if (!file_exists("texfile.pdf")){
    print_r( file_get_contents("texfile.log") );
} else {
    echo "PDF crated from file\n";
}

//trying same without file
//removed comments and newlines
$texscript = preg_replace('/[ \t]*\%.*[ \t]*[\r\n]+/', '', $texscript);
$texscript = preg_replace('/[\r\n]+/', '', $texscript);
//ahve a look on the variable
//var_dump($texscript);

$pdflatex = shell_exec('pdflatex "' .$texscript.'"' );
if (!file_exists("article.pdf")){
    print_r(  file_get_contents("article.log") );
} else {
    echo "PDF created from String\n";
}    

?>

编写 tex 文件并不重要,因为 pdflatex 会写入所需 PDF 之外的文件。

还添加了用于替换 cmets/newline 的正则表达式。

我使用 GNU/Linux Debian 8 在 cli 上调用脚本:php test.php

这会产生输出:

PDF crated from file
string(405) "\documentclass[12pt,a4paper]{article}\usepackage{helvet}\usepackage{times}\usepackage{multido}\usepackage{fancyhdr}\usepackage[hmargin=.8cm,vmargin=1.5cm,nohead,nofoot]{geometry}\renewcommand{\familydefault}{\sfdefault}\begin{document}\fancyhf{}\renewcommand{\headrulewidth}{0pt}\pagestyle{fancy}\rhead{{\fbox{\large\bfseries\thepage}}}\setcounter{page}{1}\multido{}{1}{\vphantom{x}\newpage}\end{document}"
PDF created from String

并且 PDF 文件都可以工作并且具有相同的文件大小:

-rw-r--r-- 1 vv01f vv01f 3360 Mai  5 article.pdf
-rw-r--r-- 1 vv01f vv01f 3360 Mai  5 texfile.pdf

【讨论】:

  • 嗯,谢谢.. 它可以工作.. 但我不想将它写入 .tex 文件,而是直接通过 php var 传递乳胶代码..
  • 在删除 cmets 和换行符并为调用 shell_exec 添加双引号之后,即使之前没有写入文件也可以工作。
  • 好吧,输出中的许多空格告诉我:你没有运行上面的代码。没有那个空格;你也滥用了回答功能。
  • 好吧,我确实运行了上面的代码,并且得到了与我在下面发布的相同的输出.. 还有 var_dump($texscript);也给出了带空格的输出,而不是像你得到的那样..
  • 我也无意滥用任何东西..只是我第一次使用stackoverflow..并且拼命想让事情正常工作..就是这样..将尝试使用下一次就好了..
猜你喜欢
  • 2018-03-05
  • 2016-05-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 2018-08-05
  • 2013-03-05
  • 2015-04-28
相关资源
最近更新 更多