2

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 </pre>' 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
halfer
  • 19,824
  • 17
  • 99
  • 186
Yazan Malkawi
  • 501
  • 4
  • 10
  • Have a look at http://stackoverflow.com/questions/391005/convert-html-css-to-pdf-with-php And try the search function. ;) – Paul Apr 26 '12 at 07:44
  • As you're asking that broad and you're TIA type of guy: Please use the duplicate, K THX TIA! - Go through the answers there step by step. – hakre Apr 26 '12 at 07:51
  • Paul, the thread that you have sent is the same thread that lead me to wkhmltopdf library, and after trying the steps above i decided to ask for help – Yazan Malkawi Apr 26 '12 at 08:20
  • Please post the code (PHP) you have tried. We can't help without seeing your code. Incidentally, the "cannot connect to X server" error may be because the user running the PHP script does not have permission to do so. What is your environment? Is X even running, or is this a headless server that you connect to remotely? Is this hosted, or in-house (ie, do you have complete control over the box)? – daiscog Apr 26 '12 at 08:38
  • daiscog: i have added the code and yes i do have full control over the server. thanks – Yazan Malkawi Apr 26 '12 at 08:48
  • You have two errors in `http:/www.googl.com` - it's missing a forward slash and the 'e'! – halfer Apr 26 '12 at 10:36
  • This question is far too broad, and requires more research, imo. See the questions in _Related_ to see how this is done? – halfer Apr 26 '12 at 10:38

1 Answers1

2

I found the solution, The following error "No protocol specified wkhtmltopdf: cannot connect to X server :0" was basically caused by on old version of "wkhtmltopdf" which was installed using apt-get. The solution was to download the static binary from the code.google using the following link: http://code.google.com/p/wkhtmltopdf/downloads/list

and then perform the following actions:

tar -jxvf wkhtmltopdf-0.11.0-static.tar.bz2
sudo aptitude install ia32-libs

Next, you’ll have to make a symbolic link pointing to WKHTMLTOPDF in /usr/bin.

sudo ln -s /full_path/WKHTMLTOPDF /usr/bin/WKHTMLTOPDF
Yazan Malkawi
  • 501
  • 4
  • 10