如何用php將html轉pdf檔案

2020-07-16 10:06:20

用php將html轉pdf檔案的方法:首先下載並安裝pdf;然後測試使用效果;接著用「shell_exec」這個函數在php裡呼叫;最後解決分頁問題即可。

之前有個客戶需要把一些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上安裝測試都沒有問題的。下載好以後直接安裝就可以了,注意安裝路徑要知道,下面會用到的。
安裝好以後需要在系統環境變數變數名為"Path"的後新增:;C:Program Files (x86)wkhtmltopdf 也就是你安裝的目錄。安裝好以後重新啟動電腦。

二,測試使用效果
直接在cmd裡輸入:wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf
第一個是:執行軟體名稱(這個是不變的) 第二個是網址 第三個是生成後的路徑及檔名。回車後是不是看生一個生成進度條的提示呢,恭喜您已經成功了,到你的生成目錄裡看看是不是有一個剛生成的pdf檔案呢。

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

三,解決分頁問題
wkhtmltopdf 很好用,但也有些不盡人意。就是當一個html頁面很長我需要在指定的地方分頁那怎麼辦呢? wkhtmltopdf 開發者在開發的時候並不是沒有考慮到這一點,
例如下面這個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 有個很好的方法,就是在那個p的樣式後新增一個: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問題交流平台,但是英文的。

很多相關知識,請存取PHP中文網

以上就是如何用php將html轉pdf檔案的詳細內容,更多請關注TW511.COM其它相關文章!