How to Convert HTML/CSS to PDF in PHP on Linux-Collection of common programming errors

I have been trying to convert an html page to PDF with out any luck, I have tried to use wkhtmltopdf (http://code.google.com/p/wkhtmltopdf/) and the PHP class provided by the same developers (http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp) although the following command executed correctly in terminal:

wkhtmltopdf http:/www.googl.com test.pdf

I was unable to execute the command in PHP because of the following error: cannot connect to X server.

Can any one provide me with a step-by-step tutorial in order to be able to convert html/css to PDF file, whether using wkhtmltopdf or any other decent solution.

Thanks in advance

here is the class that i have used http://code.google.com/p/wkhtmltopdf/wiki/IntegrationWithPhp

and below is my code

$html = file_get_contents("test.html");

$pdf = new WKPDF();

$pdf->set_html($html);

$pdf->render();

$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');

Below is the error:

Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return any data.
/usr/bin/wkhtmltopdf: /opt/lampp/lib/libxml2.so.2: no version information available
(required by /usr/lib/libgstreamer-0.10.so.0) No protocol specified wkhtmltopdf: cannot
connect to X server :0 

' in /var/www/c4/components/com_wkhtmltopdf /wkhtmltopdf.class.php:211 Stack trace: #0 /var/www/c4/components/com_wkhtmltopdf /wkhtmltopdf.php(68): WKPDF->render() #1 /var/www/c4/components/com_wkhtmltopdf /wkhtmltopdf.php(50): html2pdf() #2 /var/www/c4/index2.php(114): require_once('/var/ww...') #3 {main} thrown in /var/www/c4/components/com_wkhtmltopdf/wkhtmltopdf.class.php on line 211

Originally posted 2013-11-27 05:08:28.