首頁 新聞動態(tài) 網站建設 史上最強php生成pdf文件,html轉pdf文件方法

史上最強php生成pdf文件,html轉pdf文件方法

來源:網站建設 | 時間:2013-09-18 | 瀏覽:

之前有個客戶需要把一些html頁面生成pdf文件,然后我就找一些用php把html頁面圍成pdf文件的類。方法是可謂是找了很多很多,什么html2pdf,pdflib,FPDF這些都試過了,但是都沒有達到我要的求。

pdflib,FPDF這兩個方法是需要編寫程序去生成pdf的,就也是講不支持直接把html頁面轉換成pdf;html2pdf這個雖然可以把html頁面轉換成pdf文件,但是它只能轉換一般簡單的html代碼,如果你的html內容要的是通過后臺新聞編輯器排版的那肯定不行的。

糾結了半天,什么百度,谷歌搜索都用了,搜索了半天,功夫不負有心人,終于找到一個非常好用的方法了,下面就隆重介紹。

它就是:wkhtmltopdf,wkhtmltopdf可以直接把任何一個可以在瀏覽器中瀏覽的網頁直接轉換成一個pdf,首先說明一下它不是一個php類,而是一個把html頁面轉換成pdf的一個軟件,但是它并不是一個簡單的桌面軟件,而且它直接cmd批處理的。而且php有個shell_exec()函數。下面就一步一步介紹如何用php來讓它生成pdf文件的方法。

一,下載并安裝pdf
下載地址:http://code.google.com/p/wkhtmltopdf/downloads/list
上面有各種平臺下安裝的安裝包,英文不好的直接谷歌翻譯一下。下面以 windows平臺上使用舉例,我的下載的是wkhtmltopdf-0.9.9-installer.exe這個版本,我在win7 32位64位和windows 2003上安裝測試都沒有問題的。下載好以后直接安裝就可以了,注意安裝路徑要知道,下面會用到的。
安裝好以后需要在系統(tǒng)環(huán)境變量變量名為"Path"的后添加:;C:Program Files (x86)wkhtmltopdf 也就是你安裝的目錄。安裝好以后重啟電腦。

二,測試使用效果
直接在cmd里輸入:wkhtmltopdf http://bdsjkw.cn/ F:website1.pdf
第一個是:運行軟件名稱(這個是不變的) 第二個是網址 第三個是生成后的路徑及文件名?;剀嚭笫遣皇强瓷粋€生成進度條的提示呢,恭喜您已經成功了,到你的生成目錄里看看是不是有一個剛生成的pdf文件呢。

三,php里調用
php里調用是很簡單的,用shell_exec這個函數就可以了,如果shell_exec函數不能用看看php.ini里是否補禁用了。
舉例:<?php shell_exec("wkhtmltopdf http://bdsjkw.cn/ 1.pdf") ?>

三,解決分頁問題
wkhtmltopdf 很好用,但也有些不盡人意。就是當一個html頁面很長我需要在指定的地方分頁那怎么辦呢? wkhtmltopdf 開發(fā)者在開發(fā)的時候并不是沒有考慮到這一點,
例如下面這個html頁面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pdf</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<style type="text/css">
*{ margin:0px; padding:0px;}
div{ width:800px; height:1362px;margin:auto;}
</style>
<body>
<div style=" background:#030"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#F3C"></div>
<div style=" background:#F0F"></div>
<div style=" background:#0FF"></div>
<div style=" background:#FF0"></div>
<div style=" background:#00F"></div>
<div style=" background:#0F0"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#030"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#F3C"></div>
<div style=" background:#F0F"></div>
<div style=" background:#0FF"></div>
<div style=" background:#FF0"></div>
<div style=" background:#00F"></div>
<div style=" background:#0F0"></div>
</body>
</html>

當我把它生成pdf的時候我想讓每個塊都是一頁,經過無數次調試pdf的一頁大約是1362px,但是越往后值就不對了,目前還不知道pdf一頁是多少像素。

但是wkhtmltopdf 有個很好的方法,就是在那個div的樣式后添加一個:page-break-inside:avoid;就ok了。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>pdf</title>
<link href="css/style.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<style type="text/css">
*{ margin:0px; padding:0px;}
div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;}
</style>
<body>
<div style=" background:#030"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#F3C"></div>
<div style=" background:#F0F"></div>
<div style=" background:#0FF"></div>
<div style=" background:#FF0"></div>
<div style=" background:#00F"></div>
<div style=" background:#0F0"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#030"></div>
<div style=" background:#033"></div>
<div style=" background:#369"></div>
<div style=" background:#F60"></div>
<div style=" background:#F3C"></div>
<div style=" background:#F0F"></div>
<div style=" background:#0FF"></div>
<div style=" background:#FF0"></div>
<div style=" background:#00F"></div>
<div style=" background:#0F0"></div>
</body>
</html> 

http://code.google.com/p/wkhtmltopdf/這個是wkhtmltopdf問題交流平臺,但是英文的。

TAG:php生成pdf文件html轉pdf文件
在線咨詢
服務熱線
服務熱線:021-61554458
TOP