Wkhtmltopdf是一個(gè)非常有用的應(yīng)用程序,用于從html(網(wǎng)頁)創(chuàng)建pdf。本篇文章將介紹關(guān)于如何使用php腳本和Linux命令行工具創(chuàng)建網(wǎng)頁的pdf。
步驟1:在Linux中安裝wkhtmltopdf
從google code下載wkhtmltopdf并安裝到Linux系統(tǒng)。
- # 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:使用命令行創(chuàng)建PDF
首先檢查wkhtmltopdf腳本,它在命令行中正常工作。下面的命令將創(chuàng)建http://google.com網(wǎng)頁的pdf。
# /usr/bin/wkhtmltopdf http://google.com google.pdf
步驟3:使用wkhtmltopdf創(chuàng)建pdf的php代碼
使用下面的PHP代碼塊從HTML(網(wǎng)頁)生成PDF。此腳本需要為Apache啟用shell_exec函數(shù)。大多數(shù)共享主機(jī)不允許此函數(shù)。
使用下面的代碼創(chuàng)建一個(gè)文件名getpdf.php,并將其放到網(wǎng)站文檔根目錄中。
- <?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);
- ?>
打開以下網(wǎng)址生成網(wǎng)站的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
新聞熱點(diǎn)
疑難解答