
Question:
I'm using wkhtmltopdf on my server to transfer HTML document to PDF. It works very well when I'm using short URL like :
exec("/opt/wkhtmltopdf/bin/wkhtmltopdf --page-size 'Letter' --orientation 'Portrait' 'http://myurl.com/myPHPfile.php?id=12' '/tmp/myfile.pdf'")
The problem occurs when I'm using long command, like :
exec("/opt/wkhtmltopdf/bin/wkhtmltopdf --title 'The name of my file' --page-size 'Letter' --orientation 'Portrait' 'http://myurl.com/myPHPfile.php?phpsid=d8dbfbb91c0748d91426441e67aaf2b6&id=436' '/tmp/The name of my file.pdf'")
Note that when I run this long command directly from Putty it works perfectly.
The problem is that when I use exec (or shell_exec() or system() or passthru()) the page keep loading forever and my webserver doesn't respond anymore. I have to close the process from Putty (ps -x and then kill PID) myself.
Note that if I remove the ?phpsid= it works well, which is why I'm saying that the problem only occurs with long command. If I remove ?phpsid=d8dbfbb91c0748d91426441e67aaf2b6 and replace it by ?anything=ImAmAVeryLongStringThatDoNothing it doesn't work too.
I'm on CentOS 5 using WHM/cPanel. Thanks in advance for any help!
<strong>Edit:</strong>
I tried urlencode(), doesn't work.<br /> I tried escapeshellarg(), the command is correctly passed but doesn't work.<br /> I tried to use short parameters, the command is correctly passed but doesn't work.
<strong>Edit 2:</strong>
Is there a string length limit while using exec(), system() or passthru()?
<strong>Edit 3:</strong>
Finally, thanks to <a href="https://stackoverflow.com/users/358679/wrikken" rel="nofollow">Wrikken</a>, the problem was that I was passing the session_id() in the URL, and then I was re-using it in the exec(). I had to add session_write_close(); before my exec() so PHP unlocks the current session to make it redable by the script in exec(). See comments below for more informations.
Answer1:Let's update the comment to an answer: <strong>any and all</strong> variable arguments passed to the command line should be escaped with <a href="http://www.php.net/escapeshellarg" rel="nofollow">escapeshellarg
</a>
If the command line is too long, you can use the short version of each parameter. For example, instead of:
--page-size 'Letter' --orientation 'Portrait'
you can use
-s 'Letter' --O 'Portrait'