司的某項業務需要與用戶線上簽訂協議,即用戶在線手寫一個簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份PDF文件,供用戶查看和下載。
比對了一些插件,我們最終決定使用dompdf這個插件,插件的github在這里:https://github.com/dompdf/dompdf。
1. 使用方法
// 引入命名空間 use Dompdf\Dompdf; // 初始化dompdf對象 $dompdf = new Dompdf(); // 加載html文檔內容 $dompdf->loadHtml('hello world'); // 設置紙張類型和方向 $dompdf->setPaper('A4', 'landscape'); // 渲染HTML為PDF $dompdf->render(); // 流輸出 $dompdf->stream();
2. 常見問題和解決辦法
2.1 中文亂碼的問題
插件對于字體和編碼問題是這樣形容的:
PDF documents internally support the following fonts: Helvetica, Times-Roman, Courier, Zapf-Dingbats, & Symbol. These fonts only support Windows ANSI encoding. In order for a PDF to display characters that are not available in Windows ANSI, you must supply an external font. Dompdf will embed any referenced font in the PDF so long as it has been pre-loaded or is accessible to dompdf and reference in CSS @font-face rules. See the font overview for more information on how to use fonts.The DejaVu TrueType fonts have been pre-installed to give dompdf decent Unicode character coverage by default. To use the DejaVu fonts reference the font in your stylesheet, e.g. body { font-family: DejaVu Sans; } (for DejaVu Sans). The following DejaVu 2.34 fonts are available: DejaVu Sans, DejaVu Serif, and DejaVu Sans Mono.
嘗試了一下,默認帶的字體是無法渲染中文的,使用CSS的@font-face引入會報錯(也可能是我打開方式不對)。這樣就只好自己引入一個字體了。
插件給了一個安裝語言文件的工具,地址再這里:https://github.com/dompdf/utils。
使用步驟:
這樣,我們就可以在html文檔的css中使用font-family屬性來指定字體了。
html { font-family: simkai; }
2.2 圖片無法展示
插件應該是無法直接顯示網絡圖片,所以需要將圖片轉換為BASE64格式才能顯示。
將HTML文檔中的所有圖片轉換為BASE64的方式:
function imgToBase64($html) { $html = preg_replace_callback('/<img(?:.*?)src="(.*?)"(?:.*?)\/?>/', function($matches) { $imageInfo = getimagesize($matches[1]); $base64 = "" . chunk_split(base64_encode(file_get_contents($matches[1]))); $base64_image = 'data:' . $imageInfo['mime'] . ';base64,' . $base64; return str_replace($matches[1], $base64_image, $matches[0]); }, $html); return $html; }
這樣轉換其實性能影響挺大的,感覺性能不太好可以加一下緩存。
khtmltopdf是一個非常有用的應用程序,用于從html(網頁)創建pdf。本篇文章將介紹關于如何使用php腳本和Linux命令行工具創建網頁的pdf。
步驟1:在Linux中安裝wkhtmltopdf
從google code下載wkhtmltopdf并安裝到Linux系統。
# cd /opt
# wget https://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-i386.tar.bz2
# tar xjf wkhtmltopdf-0.9.9-static-i386.tar.bz2
# mv wkhtmltopdf-i386 /usr/bin/wkhtmltopdf
# chown apache:apache /usr/bin/wkhtmltopdf
# chmod +x /usr/bin/wkhtmltopdf
步驟2:使用命令行創建PDF
首先檢查wkhtmltopdf腳本,它在命令行中正常工作。下面的命令將創建http://google.com網頁的pdf。
# /usr/bin/wkhtmltopdf http://google.com google.pdf
步驟3:使用wkhtmltopdf創建pdf的php代碼
使用下面的PHP代碼塊從HTML(網頁)生成PDF。此腳本需要為Apache啟用shell_exec函數。大多數共享主機不允許此函數。
使用下面的代碼創建一個文件名getpdf.php,并將其放到網站文檔根目錄中。
<?php
$url = $_GET['url']; // Website URL to Create pdf
$name = $_GET['pdf']; // Output pdf name
$command = "/usr/bin/wkhtmltopdf ";
$pdf_dir = "/var/www/html/pdfs/"; // Pdf files will be saved here
$ex_cmd = "$command $url " . $pdf_dir . $name;
$output = shell_exec($ex_cmd);
?>
打開以下網址生成網站的pdf(html)。
語法:http:
//youdomain.com/getPdf.php?url = <website url>&pdf = <pdf name>
示例:
https : //tecadmin.net/getPdf.php?url = http : //google.com&pdf=google.pdf
本篇文章到這里就已經全部結束了,更多其他精彩內容可以關注的Linux視頻教程欄目!
以上就是使用Qtwebkit和PHP將HTML轉換為PDF的詳細內容,更多請關注其它相關文章!
更多技巧請《轉發 + 關注》哦!
起因:因為公司遇到發稿問題,很多人喜歡用word編碼,然后再發布到網站上。PHP的包中雖然有部分可以使用的類庫,但是對于圖片始終處理不好,我就想到了python。研究了下,python將word轉為html還真是方便。但是,怎么結合到服務器上呢?我們的服務器是用PHP開發的。
1:python腳本
#!/usr/bin/python# -*- coding: UTF-8 -*-import sysfrom pydocx import PyDocXreload(sys)sys.setdefaultencoding('utf8')FileName = sys.argv[1] #獲取文件名參數ShortName = sys.argv[2] #獲取文件名參數html = PyDocX.to_html(FileName) # f = open("/www/wwwroot/micuer.com/pythoncode/runtime/99.txt", 'w') #服務器的全路徑# f.write(html)# f.close()print(html)
2:php處理腳本
public function uploadword(){ try { $file = request()->file("file"); // 上傳到本地服務器 $savename = \think\facade\Filesystem::disk('upload')->putFile( 'word', $file); $shotrname = time().".txt"; // 短名稱 $savename = "/www/wwwroot/micuer.com/data/upload/".$savename; //Request::domain(). $python_file_name = "/www/wwwroot/micuer.com/pythoncode/WordToHtml.py"; //組裝命令 $cmd = "python {$python_file_name} ".$savename." {$shotrname} 2>error.txt 2>&1"; $res = exec($cmd,$array, $ret); return json(["code"=>200,"msg"=>"成功","data"=>$savename,"cmd"=>$cmd,"array"=>$array]); } catch (think\exception\ValidateException $e) { return json(["code"=>40000,"msg"=>$e->getMessage()]); } }
上傳界面如下:
實現的功能就是利用PHP的exec函數,調用py腳本,將html代碼返回給前臺服務器。
返回數據如下
其實,再處理這個方案中,也遇到了很多問題,比如在命令行下只能成功,但是exec函數執行不成功等等。
參考了資料:https://my.oschina.net/u/4427610/blog/3155816
也就是
exec("python python_test.py 2>error.txt 2>&1", $array, $ret);
在bash中0,1,2三個數字分代表STDIN_FILENO、STDOUT_FILENO、STDERR_FILENO,即標準輸入(一般是鍵盤),標準輸出(一般是顯示屏,準確的說是用戶終端控制臺),標準錯誤(出錯信息輸出)。
也可以通過以下方式將標準錯誤重定向到標準輸出保存到$array中:
打印之后,發現是沒有權限調用。于是就直接改為輸出了,也就是 py的print(html)函數。
注意幾點:
1:執行權限問題
2:exec(“python python_test.py 2>error.txt 2>&1”, $array, $ret); 中 $array就接受到了 print(html)的值
3:各個腳本盡量使用全路徑
*請認真填寫需求信息,我們會在24小時內與您取得聯系。