步骤1:在linux中安装wkhtmltopdf
从google code下载wkhtmltopdf并安装到linux系统。
# cd /opt# wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2# tar xjf wkhtmltopdf-0.9.9-static-i386.tar.bz2# mv wkhtmltopdf-i386 /usr/bin/wkhtmltopdf# chown apache:apache /usr/bin/wkhtmltopdf# chmod +x /usr/bin/wkhtmltopdf
步骤2:使用命令行创建pdf
首先检查wkhtmltopdf脚本,它在命令行中正常工作。下面的命令将创建http://google.com网页的pdf。
# /usr/bin/wkhtmltopdf http://google.com google.pdf
步骤3:使用wkhtmltopdf创建pdf的php代码
使用下面的php代码块从html(网页)生成pdf。此脚本需要为apache启用shell_exec函数。大多数共享主机不允许此函数。
使用下面的代码创建一个文件名getpdf.php,并将其放到网站文档根目录中。
<?php $url = $_get['url']; // website url to create pdf$name = $_get['pdf']; // output pdf name $command = "/usr/bin/wkhtmltopdf ";$pdf_dir = "/var/www/html/pdfs/"; // pdf files will be saved here$ex_cmd = "$command $url " . $pdf_dir . $name;$output = shell_exec($ex_cmd); ?>
打开以下网址生成网站的pdf(html)。
语法:http:
//youdomain.com/getpdf.php?url = 1c320cceacdac4839dc64009972d675d&pdf = 9eb6950f701679fe533bd2ac461ffe89
示例:
https : //tecadmin.net/getpdf.php?url = http : //google.com&pdf=google.pdf
本篇文章到这里就已经全部结束了,更多其他精彩内容可以关注的linux视频教程栏目!
以上就是使用qtwebkit和php将html转换为pdf的详细内容。
