Monday, 13 May 2019

PHP -- Running shell_exec() does NOT return all output

I am using pdfgrep to search all appearances of a keyword in a PDF Document.

Now, I want to do this via PHP so I can use this in my Web Site.

However, when I run:

$output = shell_exec("pdfgrep -i $keyword $file");
$var_dump($output);

Where $keyword is the keyword and $file is the file, I don't get the entire output.

The PDF is made up of a table of product codes, product names, and product prices.

When I execute the command via Terminal, I'm able to see the entire row of data:

product code 1    product name with keyword substring    corresponding price
product code 2    product name with keyword substring    corresponding price
product code 3    product name with keyword substring    corresponding price

However, when I ran it via PHP, I got something like:

name with keyword substring with keyword substring product code 1 
product name with keyword substring product name with keyword substring 
corresponding price

It just does not get all the data. It doesn't always get the product code and the price, and there has been a lot of instances where it doesn't get the entire product name as well.

I view the output via browser and put in header('Content-Type: text/plain'); but it only prettifies the output, the data is still incomplete.

I've tried to run the exact same shell script via Python3.6 and that gave me the output I desired.

Now, I've tried to run the same Python script via PHP but I still get the same broken output.

I've tried to run a keyword that I know would return a shorter output, but I still don't get the entire data line that I need.

Is there any way to reliably get all the data thrown by the shell_exec() command?

Are there alternatives available such as a different command, or running a Python script from a server (since the Python script doesn't have any issues anyway).



from PHP -- Running shell_exec() does NOT return all output

No comments:

Post a Comment