Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中将LaTeX代码通过变量传递给pdflatex?_Php_Variables_Escaping_Shell Exec_Pdflatex - Fatal编程技术网

如何在PHP中将LaTeX代码通过变量传递给pdflatex?

如何在PHP中将LaTeX代码通过变量传递给pdflatex?,php,variables,escaping,shell-exec,pdflatex,Php,Variables,Escaping,Shell Exec,Pdflatex,我试过这个: (.php文件) 我得到一个错误: pdflatex:这是pdfTeX,版本3.14159265-2.6-1.40.16(TeX Live 2015/Debian)(预加载格式=pdflatex)受限\write18已启用。**!终端上的文件结束 为什么? 上面给出的代码是在PHP变量中编写LaTeX并将其传递给pdflatex命令的正确方法吗?? 如果没有,请建议更正! 我不想调用外部.tex文件,而是将LaTeX代码写入PHP文件本身并从中运行。转义可能是一部分,另一部分是

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


我得到一个错误:

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

为什么?

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

转义可能是一部分,另一部分是调用命令
pdflatex
;我(首先)在变量之前写入文件,然后(第二)替换变量上的注释和换行符,这样
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";
}    

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

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

1.
(双引号)中的代码需要转义,或者使用
(单引号)。2.pdflatex需要一个文件或通过管道输入,如
cat t.tex | pdflatex
3。尝试将输出转向另一个目录,在该目录中,您可以使用
pdflatex
命令的
-output directory
选项进行写入。如果你的版本是一年多以前的AdRead尝试单引号……仍然给出相同的错误,如果我提供一个包含相同的胶乳代码的外部.TEX文件,它运行良好。参数文件和字符串是不一样的。只需在cli上尝试即可:
pdflatex$(cat t.tex)
好的,谢谢。。它起作用了。。但我不想将其写入.tex文件,而是直接将latex代码通过php var.works传递。在删除注释和换行符并为调用
shell_exec
添加双引号后,即使之前没有写入文件,输出中的许多空格都告诉我:您没有运行上面的代码。没有空白;你还滥用了应答函数。我确实运行了上面的代码,得到了与我在下面发布的相同的输出。。还包括var_dump($texscript);也就是用空格来输出,而不是像你得到的一样。而且我也没有滥用任何东西的意图。。只是我第一次使用stackoverflow。。我拼命想让事情正常运转就这样下次我会好好利用的。。
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
-rw-r--r-- 1 vv01f vv01f 3360 Mai  5 article.pdf
-rw-r--r-- 1 vv01f vv01f 3360 Mai  5 texfile.pdf