發表文章

目前顯示的是有「輸出」標籤的文章

再試 php 輸出 pdf

PHP 在輸出 pdf 的時侯有幾個網站上很有名的函式或說是工具,一個是 tcpdf ,姑且把它和 fpdf、dompdf、html2pdf 之類的工具歸在同一類,雖然除了 tcpdf 之外的我都沒試,因為太多人提到有字碼支援問題了;另一個是 wkhtmltopdf 這是 cli 終端機下的指令,用來將網頁轉成 pdf ,雖然個人並不喜歡這種工具用在網頁程式中,但是它的效果用起來真的很不錯,所以,這次先裝它來用,程式能動之後要改別的方式還不遲。 照著股溝大神指示的網站,很快就裝好這個 wkhtmltopdf ,指令很簡單, wkhtmltopdf.sh Input.html Output.pdf ,馬上就得到想要的 pdf 檔,眼都還沒眨呢! But... 又是那個 But ...,是的,中文亂碼又來攪局了,好在這次不是 tcpdf 上的空白,也不是方格,看起來就是字型的問題,檢查一下網頁,有指定是 utf-8 編碼(content="text/html; charset=utf-8"),問過大神之後,說要給個正確的字型檔,試著餵了幾個中文繁體的字型檔到 /usr/share/fonts/truetype/ 底下,挑個網頁長得美的就行。 終於收工了! 在被 tcpdf 搞了一天和 wkhtmltopdf 搞了半天之後…

將當前的 php 網頁輸出

php 一般來說用 ob_* 的函式可以處理當前的檔案輸出 ob_start — Turn on output buffering ob_get_contents — Return the contents of the output buffer ob_end_clean — Clean (erase) the output buffer and turn off output buffering 通常是: ob_start(); echo '要輸出的內容!'; $html1 = ob_get_contents(); echo '還有一些要輸出的內容!'; $html2 = ob_get_contents(); ob_end_clean(); var_dump($html1, $html2); // 輸出:    要輸出的內容!    要輸出的內容! 還有一些要輸出的內容! 但是我用了古早的 SMARTY 這個模版函式,所以 // 原本的顯示內容的網頁 $smarty -> display('show_me_my_page.tpl'); // 抓取原本的顯示內容的網頁的內容 $contents = $smarty -> fetch('show_me_my_page.tpl'); // 把抓取下來的內容存到 show_me_my_page.html file_put_contents('show_me_my_page.html', $contents); ============================ FAIL ============================ SMARTY: $contents = $smarty -> display('show_me_my_page.tpl'); file_put_contents('show_me_my_page.html', $contents);