Getting pdftotext results in php variable, not text file
pdftotext takes a PDF file and converts the text to a .txt file.
How do I get the pdftotext to send the result to a PHP variable instead of a text file?
I assume I need to run exec('pdftotext /path/file.pdf')
, but how do I return the result?
+2
Jason
source
to share
2 answers
You need to take 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;
}
+2
cgp
source
to share
$result = shell_exec("pdftotext file.pdf -");
-
will instruct pdftotext to return the result to stdout instead of a file.
+7
Ak
source
to share