tags:

views:

138

answers:

1

pdftotext takes a PDF file and converts the text into a .txt file.

How would I go about getting pdftotext to send the results 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 results back?

+1  A: 

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;
}
altCognito
Sorry for being dense but...what should I be using the the 2nd and 3rd arguments?right now I have echo cmd_exec('/usr/local/bin/pdftotext /users/jmr/downloads/test.pdf -'); which returns 1 but when I run the same command normally I get the PDF text onscreen
Jason
Use the variables you want to capture the input into for the second and third argument.
altCognito
Thanks, that did the trick
Jason