Return to Snippet

Revision: 66360
at April 23, 2014 20:14 by fedek6


Initial Code
// content var
$content = 'content';

// generate pdf from contents
$file_name = 'example.pdf';

$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("file", "./generated/$file_name", 'a'),  // stdout is a pipe that the child will write to
);

// wkhtmltopdf must be in PATH var!
$process = proc_open('wkhtmltopdf - -', $descriptorspec, $pipes );

if (is_resource($process)) {
    // $pipes now looks like this:
    // 0 => writeable handle connected to child stdin
    // 1 => readable handle connected to child stdout
    // Any error output will be appended to /tmp/error-output.txt

    fwrite($pipes[0], $content);
    fclose($pipes[0]);

    // echo stream_get_contents($pipes[1]);
    // fclose($pipes[1]);

    // It is important that you close any pipes before calling
    // proc_close in order to avoid a deadlock
    $return_value = proc_close($process);

    echo "command returned $return_value\n";
}

Initial URL

                                

Initial Description
Wkhtmltopdf (http://code.google.com/p/wkhtmltopdf/) is a open source html to pdf converter. This snippet shows how to use it in a php script.

Initial Title
Use wkhtmltopdf to print html into pdf (PHP level)

Initial Tags

                                

Initial Language
PHP