4

I am using wkhtmltopdf to generate pdf's from html, I am trying to run the below command through PHP exec() function, but its does not generate the pdf, I does not show any error though. But when i ran the same command through command line it works.

Below is my PHP code taht I am using to execute the pdf generate command.

$command = "wkhtmltopdf -s A4 /inputhtmlfilepath/test.html /outputfilepath/output.pdf";
exec($command);

Some info about my setup:

Operating system - Ubuntu 12.04

PHP Version - 5.4.3

Any help would be appreciated, Thanks.

Tapaswi Panda
  • 1,041
  • 7
  • 13

1 Answers1

6

If you are getting 'cannot connect to X server error' then please try this.

1.Install the xvfb package

 sudo apt-get install xvfb

2.Create a file as 'wkhtmltopdf.sh' and add the following code to that and save

xvfb-run -a -s "-screen 0 640x480x16" wkhtmltopdf "$@"

3.Then move this file to /usr/local/bin and give permissions

sudo chmod 555 /usr/local/bin/wkhtmltopdf.sh

4.Then try creating pdf by running the command.

/usr/local/bin/wkhtmltopdf.sh input.html output.pdf