Reputation: 15335
pdftotext takes a PDF file and converts the text into a .txt file.
How would I go about getting pdftotext to send the result to a PHP variable instead of a text file?
I'm assuming I have to run exec('pdftotext /path/file.pdf')
, but how do I get the result back?
Upvotes: 3
Views: 3894
Reputation: 91
$result = shell_exec("pdftotext file.pdf -");
The -
will instruct pdftotext to return the result to stdout instead to a file.
Upvotes: 9
Reputation: 41381
You need to capture stdout/stderr:
function cmd_exec($cmd, &$stdout, &$stderr)
{
$outfile = tempnam(".", "cmd");
$errfile = tempnam(".", "cmd");
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("file", $outfile, "w"),
2 => array("file", $errfile, "w")
);
$proc = proc_open($cmd, $descriptorspec, $pipes);
if (!is_resource($proc)) return 255;
fclose($pipes[0]); //Don't really want to give any input
$exit = proc_close($proc);
$stdout = file($outfile);
$stderr = file($errfile);
unlink($outfile);
unlink($errfile);
return $exit;
}
Upvotes: 2