php生成pdf文件,html轉pdf文件的最佳方法(轉載)

之前有個客戶需要把一些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頁面:

  1. <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4. <title>pdf</title>  
  5. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />  
  6. </head>  
  7. <style type=“text/css”>  
  8. *{ margin:0px; padding:0px;}  
  9. div{ width:800px; height:1362px;margin:auto;}  
  10. </style>  
  11. <body>  
  12. <div style=“ background:#030”></div>  
  13. <div style=“ background:#033”></div>  
  14. <div style=“ background:#369”></div>  
  15. <div style=“ background:#F60”></div>  
  16. <div style=“ background:#F3C”></div>  
  17. <div style=“ background:#F0F”></div>  
  18. <div style=“ background:#0FF”></div>  
  19. <div style=“ background:#FF0”></div>  
  20. <div style=“ background:#00F”></div>  
  21. <div style=“ background:#0F0”></div>  
  22. <div style=“ background:#033”></div>  
  23. <div style=“ background:#369”></div>  
  24. <div style=“ background:#F60”></div>  
  25. <div style=“ background:#030”></div>  
  26. <div style=“ background:#033”></div>  
  27. <div style=“ background:#369”></div>  
  28. <div style=“ background:#F60”></div>  
  29. <div style=“ background:#F3C”></div>  
  30. <div style=“ background:#F0F”></div>  
  31. <div style=“ background:#0FF”></div>  
  32. <div style=“ background:#FF0”></div>  
  33. <div style=“ background:#00F”></div>  
  34. <div style=“ background:#0F0”></div>  
  35. </body>  
  36. </html>  
  1. <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4. <title>pdf</title>  
  5. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />  
  6. </head>  
  7. <style type=“text/css”>  
  8. *{ margin:0px; padding:0px;}  
  9. div{ width:800px; height:1362px;margin:auto;}  
  10. </style>  
  11. <body>  
  12. <div style=“ background:#030”></div>  
  13. <div style=“ background:#033”></div>  
  14. <div style=“ background:#369”></div>  
  15. <div style=“ background:#F60”></div>  
  16. <div style=“ background:#F3C”></div>  
  17. <div style=“ background:#F0F”></div>  
  18. <div style=“ background:#0FF”></div>  
  19. <div style=“ background:#FF0”></div>  
  20. <div style=“ background:#00F”></div>  
  21. <div style=“ background:#0F0”></div>  
  22. <div style=“ background:#033”></div>  
  23. <div style=“ background:#369”></div>  
  24. <div style=“ background:#F60”></div>  
  25. <div style=“ background:#030”></div>  
  26. <div style=“ background:#033”></div>  
  27. <div style=“ background:#369”></div>  
  28. <div style=“ background:#F60”></div>  
  29. <div style=“ background:#F3C”></div>  
  30. <div style=“ background:#F0F”></div>  
  31. <div style=“ background:#0FF”></div>  
  32. <div style=“ background:#FF0”></div>  
  33. <div style=“ background:#00F”></div>  
  34. <div style=“ background:#0F0”></div>  
  35. </body>  
  36. </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了。

  1. <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4. <title>pdf</title>  
  5. <link href=“css/style.css” rel=“stylesheet” type=“text/css” />  
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />  
  7. </head>  
  8. <style type=“text/css”>  
  9. *{ margin:0px; padding:0px;}  
  10. div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;}  
  11. </style>  
  12. <body>  
  13. <div style=“ background:#030”></div>  
  14. <div style=“ background:#033”></div>  
  15. <div style=“ background:#369”></div>  
  16. <div style=“ background:#F60”></div>  
  17. <div style=“ background:#F3C”></div>  
  18. <div style=“ background:#F0F”></div>  
  19. <div style=“ background:#0FF”></div>  
  20. <div style=“ background:#FF0”></div>  
  21. <div style=“ background:#00F”></div>  
  22. <div style=“ background:#0F0”></div>  
  23. <div style=“ background:#033”></div>  
  24. <div style=“ background:#369”></div>  
  25. <div style=“ background:#F60”></div>  
  26. <div style=“ background:#030”></div>  
  27. <div style=“ background:#033”></div>  
  28. <div style=“ background:#369”></div>  
  29. <div style=“ background:#F60”></div>  
  30. <div style=“ background:#F3C”></div>  
  31. <div style=“ background:#F0F”></div>  
  32. <div style=“ background:#0FF”></div>  
  33. <div style=“ background:#FF0”></div>  
  34. <div style=“ background:#00F”></div>  
  35. <div style=“ background:#0F0”></div>  
  36. </body>  
  37. </html><span style=“font-family:arial,verdana,sans-serif”><span style=“white-space:normal”> </span></span>  
  1. <!DOCTYPE html PUBLIC ”-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>  
  2. <html xmlns=“http://www.w3.org/1999/xhtml”>  
  3. <head>  
  4. <title>pdf</title>  
  5. <link href=“css/style.css” rel=“stylesheet” type=“text/css” />  
  6. <meta http-equiv=“Content-Type” content=“text/html; charset=utf-8” />  
  7. </head>  
  8. <style type=“text/css”>  
  9. *{ margin:0px; padding:0px;}  
  10. div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;}  
  11. </style>  
  12. <body>  
  13. <div style=“ background:#030”></div>  
  14. <div style=“ background:#033”></div>  
  15. <div style=“ background:#369”></div>  
  16. <div style=“ background:#F60”></div>  
  17. <div style=“ background:#F3C”></div>  
  18. <div style=“ background:#F0F”></div>  
  19. <div style=“ background:#0FF”></div>  
  20. <div style=“ background:#FF0”></div>  
  21. <div style=“ background:#00F”></div>  
  22. <div style=“ background:#0F0”></div>  
  23. <div style=“ background:#033”></div>  
  24. <div style=“ background:#369”></div>  
  25. <div style=“ background:#F60”></div>  
  26. <div style=“ background:#030”></div>  
  27. <div style=“ background:#033”></div>  
  28. <div style=“ background:#369”></div>  
  29. <div style=“ background:#F60”></div>  
  30. <div style=“ background:#F3C”></div>  
  31. <div style=“ background:#F0F”></div>  
  32. <div style=“ background:#0FF”></div>  
  33. <div style=“ background:#FF0”></div>  
  34. <div style=“ background:#00F”></div>  
  35. <div style=“ background:#0F0”></div>  
  36. </body>  
  37. </html><span style=“font-family:arial,verdana,sans-serif”><span style=“white-space:normal”> </span></span>  
<!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問題交流平臺,但是英文的。


wkhtmltopdf 中文參數詳解  

php使用事例:shell_exec(“wkhtmltopdf –orientation Landscape http://front.dfzx12.com/?r=report/studentgene/id/2 1234567.pdf”);
shell_exec(“wkhtmltopdf –header-center ‘baobiao’ –page-width 50cm –header-line –margin-top 1cm –header-line 1.html oma11.pdf”);
wkhtmltopdf [OPTIONS]… <input file> [More input files] <output file>
常規選項
  –allow <path>  允許加載從指定的文件夾中的文件或文件(可重複)
–book*  設置一會打印一本書的時候,通常設置的選項 
  –collate  打印多份副本時整理 
  –cookie <name> <value>  設置一個額外的cookie(可重複) 
  –cookie-jar <path>  讀取和寫入的Cookie,並在提供的cookie jar文件 
  –copies <number>  複印打印成pdf文件數(默認爲1) 
  –cover* <url>  使用HTML文件作爲封面。它會帶頁眉和頁腳的TOC之前插入 
  –custom-header <name> <value>  設置一個附加的HTTP頭(可重複) 
  –debug-javascript  顯示的javascript調試輸出 
–default-header*  添加一個缺省的頭部,與頁面的左邊的名稱,頁面數到右邊,例如: –header-left ‘[webpage]’ –header-right ‘[page]/[toPage]’  –header-line 
  –disable-external-links*  禁止生成鏈接到遠程網頁
  –disable-internal-links*  禁止使用本地鏈接
–disable-javascript  禁止讓網頁執行JavaScript 
  –disable-pdf-compression*  禁止在PDF對象使用無損壓縮 
  –disable-smart-shrinking*  禁止使用WebKit的智能戰略收縮,使像素/ DPI比沒有不變 
  –disallow-local-file-access  禁止允許轉換的本地文件讀取其他本地文件,除非explecitily允許用 –allow 
–dpi <dpi>  顯式更改DPI(這對基於X11的系統沒有任何影響) 
  –enable-plugins  啓用已安裝的插件(如Flash
  –encoding <encoding>  設置默認的文字編碼 
  –extended-help  顯示更廣泛的幫助,詳細介紹了不常見的命令開關 
  –forms*  打開HTML表單字段轉換爲PDF表單域 
–grayscale  PDF格式將在灰階產生
–help  Display help 
  –htmldoc  輸出程序HTML幫助
  –ignore-load-errors  忽略claimes加載過程中已經遇到了一個錯誤頁面 
–lowquality  產生低品質的PDF/ PS。有用縮小結果文檔的空間 
  –manpage  輸出程序手冊頁 
–margin-bottom <unitreal>  設置頁面下邊距 (default 10mm) 
–margin-left <unitreal>  將左邊頁邊距 (default 10mm) 
–margin-right <unitreal>  設置頁面右邊距 (default 10mm) 
–margin-top <unitreal>  設置頁面上邊距 (default 10mm) 
  –minimum-font-size <int>  最小字體大小 (default 5) 
  –no-background  不打印背景
–orientation <orientation>  設置方向爲橫向或縱向 
  –page-height <unitreal>  頁面高度 (default unit millimeter) 
  –page-offset* <offset>  設置起始頁碼 (default 1) 
–page-size <size>  設置紙張大小: A4, Letter, etc. 
    –page-width <unitreal>  頁面寬度 (default unit millimeter) 
  –password <password>  HTTP驗證密碼 
  –post <name> <value>  Add an additional post field (repeatable) 
  –post-file <name> <path>  Post an aditional file (repeatable) 
  –print-media-type*  使用的打印介質類型,而不是屏幕 
–proxy <proxy>  使用代理 
–quiet  Be less verbose 
–read-args-from-stdin  讀取標準輸入的命令行參數 
–readme  輸出程序自述
–redirect-delay <msec>  等待幾毫秒爲JS-重定向(default 200) 
–replace* <name> <value>  替換名稱,值的頁眉和頁腳(可重複) 
–stop-slow-scripts  停止運行緩慢的JavaScripts 
–title <text>  生成的PDF文件的標題(第一個文檔的標題使用,如果沒有指定) 
–toc*  插入的內容的表中的文件的開頭
–use-xserver*  使用X服務器(一些插件和其他的東西沒有X11可能無法正常工作) 
–user-style-sheet <url>  指定用戶的樣式表,加載在每一頁中
–username <username>  HTTP認證的用戶名 
–version  輸出版本信息退出
  –zoom <float>  使用這個縮放因子 (default 1) 

頁眉和頁腳選項
–header-center*    <text>    (設置在中心位置的頁眉內容)  
–header-font-name* <name>    (default Arial)  (設置頁眉的字體名稱)
–header-font-size* <size>    (設置頁眉的字體大小)
–header-html*  <url> (添加一個HTML頁眉,後面是網址)
–header-left*  <text>   (左對齊的頁眉文本)
–header-line*      (顯示一條線在頁眉下)
–header-right* <text>    (右對齊頁眉文本)
–header-spacing*   <real>    (設置頁眉和內容的距離,默認0)
–footer-center*    <text>    (設置在中心位置的頁腳內容)  
–footer-font-name* <name>    (設置頁腳的字體名稱) 
–footer-font-size* <size>    (設置頁腳的字體大小default 11)
–footer-html*  <url> (添加一個HTML頁腳,後面是網址)
–footer-left*  <text>    (左對齊的頁腳文本)
–footer-line*      顯示一條線在頁腳內容上)
–footer-right* <text>    (右對齊頁腳文本)
–footer-spacing*   <real>    (設置頁腳和內容的距離)
./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf
./wkhtmltopdf –header-center ‘報表’ –header-line –margin-top 2cm –header-line http://192.168.212.139/oma/  oma.pdf
表內容選項中
 –toc-depth* <level>  Set the depth of the toc (default 3) 
 –toc-disable-back-links*  Do not link from section header to toc 
 –toc-disable-links*  Do not link from toc to sections 
 –toc-font-name* <name>  Set the font used for the toc (default Arial) 
 –toc-header-font-name* <name>  The font of the toc header (if unset use –toc-font-name) 
 –toc-header-font-size* <size>  The font size of the toc header (default 15) 
 –toc-header-text* <text>  The header text of the toc (default Table Of Contents) 
 –toc-l1-font-size* <size>  Set the font size on level 1 of the toc (default 12) 
 –toc-l1-indentation* <num>  Set indentation on level 1 of the toc (default 0) 
 –toc-l2-font-size* <size>  Set the font size on level 2 of the toc (default 10) 
 –toc-l2-indentation* <num>  Set indentation on level 2 of the toc (default 20) 
 –toc-l3-font-size* <size>  Set the font size on level 3 of the toc (default 8) 
 –toc-l3-indentation* <num>  Set indentation on level 3 of the toc (default 40) 
 –toc-l4-font-size* <size>  Set the font size on level 4 of the toc (default 6) 
 –toc-l4-indentation* <num>  Set indentation on level 4 of the toc (default 60) 
 –toc-l5-font-size* <size>  Set the font size on level 5 of the toc (default 4) 
 –toc-l5-indentation* <num>  Set indentation on level 5 of the toc (default 80) 
 –toc-l6-font-size* <size>  Set the font size on level 6 of the toc (default 2) 
 –toc-l6-indentation* <num>  Set indentation on level 6 of the toc (default 100) 
 –toc-l7-font-size* <size>  Set the font size on level 7 of the toc (default 0) 
 –toc-l7-indentation* <num>  Set indentation on level 7 of the toc (default 120) 
 –toc-no-dots*  Do not use dots, in the toc
輪廓選項
 –dump-outline <file>  轉儲目錄到一個文件
 –outline  顯示目錄(文章中h1,h2來定)
 –outline-depth <level>  設置目錄的深度(默認爲4)
頁腳和頁眉
 * [page]       由當前正在打印的頁的數目代替
 * [frompage]   由要打印的第一頁的數量取代
 * [topage]     由最後一頁要打印的數量取代
 * [webpage]    通過正在打印的頁面的URL替換
 * [section]    由當前節的名稱替換
 * [subsection] 由當前小節的名稱替換
 * [date]       由當前日期系統的本地格式取代
 * [time]       由當前時間,系統的本地格式取代
 ./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf
 ./wkhtmltopdf –header-center  ’報表’ –outline  –header-line –margin-top 2cm –header-line http://www.hao123.com/  hao123.pdf
 ./wkhtmltopdf –header-left ‘[webpage]’ –footer-center ‘測試([page]/[toPage])’ http://www.baidu.com baidu.pdf


內容轉載自http://blog.csdn.net/mengke1124/article/details/43604889

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章