Wkhtmltopdf是一个非常有用的应用程序,用于从html(网页)创建pdf。本篇文章将介绍关于如何使用php脚本和Linux命令行工具创建网页的pdf。
步骤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 = <website url>&pdf = <pdf name>
示例:
https : //tecadmin.net/getPdf.php?url = http : //google.com&pdf=google.pdf
新闻热点
疑难解答