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
7
  • Have a look at stackoverflow.com/questions/391005/… And try the search function. ;) Commented Apr 26, 2012 at 7: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. Commented Apr 26, 2012 at 7: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 Commented Apr 26, 2012 at 8: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)? Commented Apr 26, 2012 at 8:38
  • daiscog: i have added the code and yes i do have full control over the server. thanks Commented Apr 26, 2012 at 8:48

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.