首頁  >  文章  >  後端開發  >  如何用php將html轉pdf文件

如何用php將html轉pdf文件

藏色散人
藏色散人原創
2020-07-06 09:18:284642瀏覽

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

如何用php將html轉pdf文件

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

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

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

它就是: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裡是否補禁用了。
範例:b2c7ff02e5174be99b172ec4ea3db754

三,解決分頁問題
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文件的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn