Search code examples
phpwindowsprocessconsole

Hide output from proc_open command


I'm using PHP proc_open command to run pdflatex.exe to produce a PDF file from a tex file. The code looks like this:

$descriptorspec = array(
    0 => array("pipe", "r"),  
    1 => array("pipe", "w"), 
    2 => array("file", "D:/Freigabe/error.txt", "a")
);

$cwd = str_replace("\\", "/", __DIR__) .'/Tex/Working';
$env = null;
$this->execute = '"' .str_replace('/','\\',env('PDFLATEXENGINE', 'C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/pdflatex.exe')) .'" -jobname="' .str_replace('.pdf','',$this->publicPdfFile) .'" -output-directory="' .str_replace("\\", "/", $this->pdfTargetPath) .'" "' .str_replace("\\", "/", str_replace(".tex","",$this->workingFilePath));
$process = proc_open('"' .$this->execute .'"', $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
    fwrite($pipes[0], '<?php print_r($_ENV); ?>');
    fclose($pipes[0]);
    echo stream_get_contents($pipes[1]);
    fclose($pipes[1]);
    $return_value = proc_close($process);
}

One example of a command is this one:

$this->execute = '"D:\Program Files\TeXLive\texlive\2019\bin\win32\pdflatex.exe" -jobname="Rechnung_000036-19" -output-directory="D:/Files/Company/Tool/app/Classes/Rechnung/LaTex/Output/" "D:/Files/Company/Tool/app/Classes/Rechnung/LaTex/Output/Rechnung_FUmtC1P7lknTzvDfstMM_000036-19"';

This works fine, but the command itself produces some output when it generates the PDF file. With this code above, the output of the command is printed to my webpage and it looks like this:

enter image description here

I also know, why this is being printed. It is just because of this code line:

fwrite($pipes[0], '<?php print_r($_ENV); ?>');

But when I comment out the print_r() command the PDF file is no longer being produced. I checked for a solution to suppress the output of the command window. I only found > /dev/null, but this I also get not to work.

What can I try next to resolve this?


Solution

  • I've found the answer. I can suppress the console output with simply adding >nul to the command. So this is my solution:

    $process = proc_open('"' .$this->execute .'" >nul', $descriptorspec, $pipes, $cwd, $env);