duanfu3634 2012-04-26 07:38
浏览 58
已采纳

如何在Linux上用PHP将HTML / CSS转换为PDF

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
  • 写回答

1条回答 默认 最新

  • doushijiao0679 2012-04-30 05:32
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀