Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

$result = shell_exec("pdftotext file.pdf -");

The - will instruct pdftotext to return the result to stdout instead to a file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top