miercuri, 28 mai 2014

Wkhtmltopdf on Windows: The input line is too long

Trying to run wkhtmltopdf from within PHP, on Windows.
While on Linux, the same command used to run perfectly, on Windows I've been getting:
The input line is too long

I've found some answers and this workaround worked for me too:
Placing my command within a temporary .bat file, executing it and removing it afterwards.
For example:
$myLongCMD = '"C:\program files (x86)\wkhtmltopdf\wkhtmltopdf.exe" -d 600 --margin-bottom 24 --margin-left 0 --margin-right 0 --margin-top 13  --orientation Landscape --page-size A4 "E:\www\tmp\1003832673.html" --footer-html "E:\www\tmp\1957525255.html" --footer-html "E:\www\tmp\2038722390.html" "E:\www\tmp\2082679417.html" --footer-html "E:\www\tmp\2038722390.html" "E:\www\tmp\output.pdf"';

$tmpf = 'D:\\www\\myfile.bat';
$tmpp = fopen($tmpf,'w');
fwrite($tmpp, $myLongCMD);
fclose($tmpp);

$descriptors = array(
    0 => array("pipe", "r"), // stdin
    1 => array("pipe", "w"),  // stdout
    2 => array('pipe', 'a') // stderr - Open this as 'Append' as stream_get_contents gets blocked infinitely and the process never ends just as Luceo (many thanks to him) mentions here: http://www.php.net/manual/en/function.proc-open.php
);


$proc=proc_open('"'.$tmpf.'"', $descriptors, $pipes);
fwrite($pipes[0],$input);
fclose($pipes[0]);
$stdout=stream_get_contents($pipes[1]);
fclose($pipes[1]);
$stderr=stream_get_contents($pipes[2]);
fclose($pipes[2]);
$rtn=proc_close($proc);

@unlink($tmpf);


System config:
PHP ver 5.3.9 , Apache 2.4.9, Windows 2008 x64

Niciun comentariu:

Trimiteți un comentariu