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 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分