S
S
sondor2017-10-25 01:24:47
PHP
sondor, 2017-10-25 01:24:47

Why does wkhtmltopdf generate curve pdf via proc_open?

Good afternoon, I'm testing wkhtmltopdf on Win via openserver.
Pdf is correctly formed if I execute:

exec("C:\wkhtmltopdf\bin\wkhtmltopdf.exe -q http://example.com/ my.pdf");

When I try to do it through streams, the pdf turns out to be broken
$html = file_get_contents("http://example.com/");
$descriptorspec = array(
   0 => array("pipe", "r"),  // stdin это канал, из которого потомок будет читать
   1 => array("pipe", "w"),  // stdout это канал, в который потомок будет записывать
   2 => array("pipe", "w"), // stderr это файл для записи
);
$process = proc_open("C:\wkhtmltopdf\bin\wkhtmltopdf.exe -q - -", $descriptorspec, $pipes, null, null, ['bypass_shell' => true]);
if (is_resource($process)) {
    fwrite($pipes[0], $html);
    fclose($pipes[0]);

    $pdf = stream_get_contents($pipes[1]);
    $errors = stream_get_contents($pipes[2]);

    fclose($pipes[1]);
    fclose($pipes[2]);
    
    $return_value = proc_close($process);
  
  if ($errors) {
        echo 'PDF generation failed: ' . $errors;
    } else {
        header('Content-Type: application/pdf');
        header('Content-Length: ' . strlen($pdf));
        echo $pdf;
    }
}

I took examples:
from here and from here
Which way to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sondor, 2017-10-25
@sondor

The problem was in
header('Content-Length: ' . strlen($pdf));

K
krypt3r, 2017-10-25
@krypt3r

There are wrappers in PHP, for example, https://github.com/mikehaertl/phpwkhtmltopdf

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question