国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > PHP > 正文

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

2019-11-06 08:06:48
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

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

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

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

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

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

二,測(cè)試使用效果直接在cmd里輸入:wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf第一個(gè)是:運(yùn)行軟件名稱(這個(gè)是不變的) 第二個(gè)是網(wǎng)址 第三個(gè)是生成后的路徑及文件名。回車后是不是看生一個(gè)生成進(jìn)度條的提示呢,恭喜您已經(jīng)成功了,到你的生成目錄里看看是不是有一個(gè)剛生成的pdf文件呢。

三,php里調(diào)用php里調(diào)用是很簡(jiǎn)單的,用shell_exec這個(gè)函數(shù)就可以了,如果shell_exec函數(shù)不能用看看php.ini里是否補(bǔ)禁用了。舉例:<?php shell_exec(“wkhtmltopdf http://www.shwzzz.cn/ 1.pdf”) ?>

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

[html] view plain copy print?<!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>  [html] view plain copy print?<!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>  
<!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>

當(dāng)我把它生成pdf的時(shí)候我想讓每個(gè)塊都是一頁(yè),經(jīng)過(guò)無(wú)數(shù)次調(diào)試pdf的一頁(yè)大約是1362px,但是越往后值就不對(duì)了,目前還不知道pdf一頁(yè)是多少像素。

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

[html] view plain copy print?<!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><span style=“font-family:arial,verdana,sans-serif”><span style=“white-space:normal”> </span></span>  [html] view plain copy print?<!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><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/這個(gè)是wkhtmltopdf問(wèn)題交流平臺(tái),但是英文的。

wkhtmltopdf 中文參數(shù)詳解  

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>常規(guī)選項(xiàng)  –allow <path>  允許加載從指定的文件夾中的文件或文件(可重復(fù))–book*  設(shè)置一會(huì)打印一本書的時(shí)候,通常設(shè)置的選項(xiàng)   –collate  打印多份副本時(shí)整理   –cookie <name> <value>  設(shè)置一個(gè)額外的cookie(可重復(fù))   –cookie-jar <path>  讀取和寫入的Cookie,并在提供的cookie jar文件   –copies <number>  復(fù)印打印成pdf文件數(shù)(默認(rèn)為1)   –cover* <url>  使用HTML文件作為封面。它會(huì)帶頁(yè)眉和頁(yè)腳的TOC之前插入   –custom-header <name> <value>  設(shè)置一個(gè)附加的HTTP頭(可重復(fù))   –debug-javascript  顯示的Javascript調(diào)試輸出 –default-header*  添加一個(gè)缺省的頭部,與頁(yè)面的左邊的名稱,頁(yè)面數(shù)到右邊,例如: –header-left ‘[webpage]’ –header-right ‘[page]/[toPage]’  –header-line   –disable-external-links*  禁止生成鏈接到遠(yuǎn)程網(wǎng)頁(yè)  –disable-internal-links*  禁止使用本地鏈接–disable-javascript  禁止讓網(wǎng)頁(yè)執(zhí)行JavaScript   –disable-pdf-compression*  禁止在PDF對(duì)象使用無(wú)損壓縮   –disable-smart-shrinking*  禁止使用WebKit的智能戰(zhàn)略收縮,使像素/ DPI比沒(méi)有不變   –disallow-local-file-access  禁止允許轉(zhuǎn)換的本地文件讀取其他本地文件,除非explecitily允許用 –allow –dpi <dpi>  顯式更改DPI(這對(duì)基于X11的系統(tǒng)沒(méi)有任何影響)   –enable-plugins  啟用已安裝的插件(如Flash  –encoding <encoding>  設(shè)置默認(rèn)的文字編碼   –extended-help  顯示更廣泛的幫助,詳細(xì)介紹了不常見(jiàn)的命令開(kāi)關(guān)   –forms*  打開(kāi)HTML表單字段轉(zhuǎn)換為PDF表單域 –grayscale  PDF格式將在灰階產(chǎn)生–help  Display help   –htmldoc  輸出程序HTML幫助  –ignore-load-errors  忽略claimes加載過(guò)程中已經(jīng)遇到了一個(gè)錯(cuò)誤頁(yè)面 –lowquality  產(chǎn)生低品質(zhì)的PDF/ PS。有用縮小結(jié)果文檔的空間   –manpage  輸出程序手冊(cè)頁(yè) –margin-bottom <unitreal>  設(shè)置頁(yè)面下邊距 (default 10mm) –margin-left <unitreal>  將左邊頁(yè)邊距 (default 10mm) –margin-right <unitreal>  設(shè)置頁(yè)面右邊距 (default 10mm) –margin-top <unitreal>  設(shè)置頁(yè)面上邊距 (default 10mm)   –minimum-font-size <int>  最小字體大小 (default 5)   –no-background  不打印背景–orientation <orientation>  設(shè)置方向?yàn)闄M向或縱向   –page-height <unitreal>  頁(yè)面高度 (default unit millimeter)   –page-offset* <offset>  設(shè)置起始頁(yè)碼 (default 1) –page-size <size>  設(shè)置紙張大小: A4, Letter, etc.     –page-width <unitreal>  頁(yè)面寬度 (default unit millimeter)   –passWord <password>  HTTP驗(yàn)證密碼   –post <name> <value>  Add an additional post field (repeatable)   –post-file <name> <path>  Post an aditional file (repeatable)   –print-media-type*  使用的打印介質(zhì)類型,而不是屏幕 –proxy <proxy>  使用代理 –quiet  Be less verbose –read-args-from-stdin  讀取標(biāo)準(zhǔn)輸入的命令行參數(shù) –readme  輸出程序自述–redirect-delay <msec>  等待幾毫秒為JS-重定向(default 200) –replace* <name> <value>  替換名稱,值的頁(yè)眉和頁(yè)腳(可重復(fù)) –stop-slow-scripts  停止運(yùn)行緩慢的JavaScripts –title <text>  生成的PDF文件的標(biāo)題(第一個(gè)文檔的標(biāo)題使用,如果沒(méi)有指定) –toc*  插入的內(nèi)容的表中的文件的開(kāi)頭–use-xserver*  使用X服務(wù)器(一些插件和其他的東西沒(méi)有X11可能無(wú)法正常工作) –user-style-sheet <url>  指定用戶的樣式表,加載在每一頁(yè)中–username <username>  HTTP認(rèn)證的用戶名 –version  輸出版本信息退出  –zoom <float>  使用這個(gè)縮放因子 (default 1) 頁(yè)眉和頁(yè)腳選項(xiàng)–header-center*    <text>    (設(shè)置在中心位置的頁(yè)眉內(nèi)容)  –header-font-name* <name>    (default Arial)  (設(shè)置頁(yè)眉的字體名稱)–header-font-size* <size>    (設(shè)置頁(yè)眉的字體大小)–header-html*  <url> (添加一個(gè)HTML頁(yè)眉,后面是網(wǎng)址)–header-left*  <text>   (左對(duì)齊的頁(yè)眉文本)–header-line*      (顯示一條線在頁(yè)眉下)–header-right* <text>    (右對(duì)齊頁(yè)眉文本)–header-spacing*   <real>    (設(shè)置頁(yè)眉和內(nèi)容的距離,默認(rèn)0)–footer-center*    <text>    (設(shè)置在中心位置的頁(yè)腳內(nèi)容)  –footer-font-name* <name>    (設(shè)置頁(yè)腳的字體名稱) –footer-font-size* <size>    (設(shè)置頁(yè)腳的字體大小default 11)–footer-html*  <url> (添加一個(gè)HTML頁(yè)腳,后面是網(wǎng)址)–footer-left*  <text>    (左對(duì)齊的頁(yè)腳文本)–footer-line*      顯示一條線在頁(yè)腳內(nèi)容上)–footer-right* <text>    (右對(duì)齊頁(yè)腳文本)–footer-spacing*   <real>    (設(shè)置頁(yè)腳和內(nèi)容的距離)./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf./wkhtmltopdf –header-center ‘報(bào)表’ –header-line –margin-top 2cm –header-line http://192.168.212.139/oma/  oma.pdf表內(nèi)容選項(xiàng)中 –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輪廓選項(xiàng) –dump-outline <file>  轉(zhuǎn)儲(chǔ)目錄到一個(gè)文件 –outline  顯示目錄(文章中h1,h2來(lái)定) –outline-depth <level>  設(shè)置目錄的深度(默認(rèn)為4)頁(yè)腳和頁(yè)眉 * [page]       由當(dāng)前正在打印的頁(yè)的數(shù)目代替 * [frompage]   由要打印的第一頁(yè)的數(shù)量取代 * [topage]     由最后一頁(yè)要打印的數(shù)量取代 * [webpage]    通過(guò)正在打印的頁(yè)面的URL替換 * [section]    由當(dāng)前節(jié)的名稱替換 * [subsection] 由當(dāng)前小節(jié)的名稱替換 * [date]       由當(dāng)前日期系統(tǒng)的本地格式取代 * [time]       由當(dāng)前時(shí)間,系統(tǒng)的本地格式取代 ./wkhtmltopdf –footer-right ‘[page]/[topage]’ http://www.baidu.com baidu.pdf ./wkhtmltopdf –header-center  ’報(bào)表’ –outline  –header-line –margin-top 2cm –header-line http://www.hao123.com/  hao123.pdf ./wkhtmltopdf –header-left ‘[webpage]’ –footer-center ‘測(cè)試([page]/[toPage])’ http://www.baidu.com baidu.pdf

內(nèi)容轉(zhuǎn)載自http://blog.csdn.net/mengke1124/article/details/43604889


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 惠来县| 彭泽县| 庆云县| 田林县| 汝阳县| 株洲县| 邹城市| 华坪县| 镇原县| 重庆市| 睢宁县| 涿州市| 太谷县| 宾阳县| 漳浦县| 皮山县| 晋江市| 青川县| 静海县| 鄂托克前旗| 诸暨市| 安溪县| 抚宁县| 宁蒗| 庆阳市| 敦煌市| 南充市| 乌拉特中旗| 广灵县| 东城区| 静宁县| 邵阳县| 嘉定区| 抚宁县| 龙游县| 巨野县| 河南省| 仙居县| 肇源县| 葫芦岛市| 漳州市|