php - Symfony - Process run throws exception - Executing on command line works
Get the solution ↓↓↓I have an issue with generating PDF files with the help of LaTeX. The solution is created with the Laravel framework (Vers. 8).
I installed a new "Windows 2019 Datacenter" server, where the website should later be hosted on. On the server the command to generate the PDF files fails. On my local developing machine (Windows 10) the command works fine. I'm running on both machines PHP 7.4 withsymfony/process
v5.0.
This is the code where the command is being created:
public function createPDFFromLaTexFile(string $fileName, string $outputDirectory, string $laTexFilePath) {
$executionFile = str_replace('/','\\',env('PDFLATEXENGINE', 'C:/Program Files (x86)/MiKTeX 2.9/miktex/bin/pdflatex.exe'));
$execute = [$executionFile, '-jobname=' .str_replace('.pdf','',$fileName), '-output-directory=' .str_replace("\\", "/", $outputDirectory), str_replace("\\", "/", str_replace(".tex","",$laTexFilePath))];
$this->executeProcess($execute);
}
public function executeProcess($executionProcess) {
$process = new \Symfony\Component\Process\Process($executionProcess);
$process->run();
if (!$process->isSuccessful()) {
throw new \Symfony\Component\Process\Exception\ProcessFailedException($process);
}
}
This is what the error log says:
[2020-11-16 06:13:55] Live.ERROR: The command "C:_Tools\texlive\2020\bin\win32\pdflatex.exe -jobname=m7EkMDQFggshi4gGpsDD "-output-directory=C:/_App/Live/public/Berichte/" "C:/_App/Live/app/Classes/Bericht/Tex/Working/m7EkMDQFggshi4gGpsDD"" failed.
Exit Code: 1(General error)
Working directory: C:_App\Live\public
Output: == ==============
Error Output:
================ {"userId":1770,"exception":"[object] (Symfony\Component\Process\Exception\ProcessFailedException(code: 0): The command "C:\_Tools\texlive\2020\bin\win32\pdflatex.exe -jobname=m7EkMDQFggshi4gGpsDD "-output-directory=C:/_App/Live/public/Berichte/" "C:/_App/Live/app/Classes/Bericht/Tex/Working/m7EkMDQFggshi4gGpsDD"" failed.
Exit Code: 1(General error)
Working directory: C:\_App\Live\public
Output: == ==============
Error Output:
================ at C:\_App\Live\app\Classes\Documents\PDF.php:21) [stacktrace] #0 C:\_App\Live\app\Classes\Documents\PDF.php(13): App\Classes\Documents\PDF->executeProcess() #1 C:\_App\Live\app\Classes\Bericht\Bericht.php(239): App\Classes\Documents\PDF->createPDFFromLaTexFile() #2 C:\_App\Live\app\Http\Controllers\Bericht\BerichtController.php(51): App\Classes\Bericht\Bericht->pdfErzeugen() #3 [internal function]: App\Http\Controllers\Bericht\BerichtController->erzeugen() ...
The strange thing is, if I run the command in the command window itself, all works fine and the PDF is created successfully.
I'm not sure, why the first parameter is not enclosed by"
. Maybe this could be the reason why the command fails? Or could it be a authorization issue to run the command?
"C:\_Tools\texlive\2020\bin\win32\pdflatex.exe -jobname=m7EkMDQFggshi4gGpsDD "-output-directory=C:/_App/Live/public/Berichte/" "C:/_App/Live/app/Classes/Bericht/Tex/Working/m7EkMDQFggshi4gGpsDD""
UPDATE
If tried to create the file using the PHPproc_open()
command.
This is the code:
$command = '';
$count = 0;
foreach ($executionProcess as $value) {
if($count != 0) {
$command .= " ";
}
$command .= '"' .$value .'"';
$count++;
}
$command .= " > nul";
\Log::info('Command: ' .$command);
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", env('PROCOPENERRORLOG') ."procOpenErrors.log", "a")
);
$cwd = str_replace("\\", "/", base_path()) .'/app/Classes/Bericht/Tex/Working';
$env = null;
$process = proc_open($command, $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);
}
Again the same behaviour. If run the command in$command
variable in the command window manually, the PDF is being generated. But when I just run the code, the PDF file is not being generated. So far I can say, the command itself is correct. Maybe it is really an issue of permissions on the server system? Another idea I had, if it could be an issue of using PHP 7.4 32-bit or 64-bit? Currently is installed the x64 version of PHP 7.4.
Answer
Solution:
Unfortunately I was not able to find out why Symfony process has a problem with the generation of the PDF file, but I found a workaround.
I was able to get myproc_open
command to work. This is the code that works now:
$command = '';
$count = 0;
foreach ($executionProcess as $value) {
if($count != 0) {
// parameters quoted:
$command .= " ";
$command .= '"' .$value .'"';
}
else {
// exe
$command .= $value;
}
$count++;
}
$command .= " > nul";
\Log::info('Command: ' .$command);
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", env('PROCOPENERRORLOG') ."procOpenErrors.log", "a")
);
$cwd = str_replace("\\", "/", base_path()) .'/app/Classes/Bericht/Tex/Working';
$env = null;
$process = proc_open($command, $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);
}
But I am still interested, why Symfony is not working.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: fastcgi sent in stderr: "primary script unknown" while reading response header from upstream
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.