Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 男女激情网址,一区二区视频,国产一区二区精品

          整合營銷服務商

          電腦端+手機端+微信端=數據同步管理

          免費咨詢熱線:

          PHP轉換HTML為PDF文檔的方法和常見問題

          司的某項業務需要與用戶線上簽訂協議,即用戶在線手寫一個簽名,后臺將公司公章信息和用戶的簽名以及合同信息生成一份PDF文件,供用戶查看和下載。



          比對了一些插件,我們最終決定使用dompdf這個插件,插件的github在這里:https://github.com/dompdf/dompdf。

          1. 使用方法

          • 安裝可以使用composer或者直接下載源代碼,使用require或者include引入。
          • 具體的使用方式,可以參考以下示例代碼。
          // 引入命名空間
          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。

          使用步驟:

          • 下載或者復制load_font.php文件,放到dompdf文件夾內,與src和test文件夾同級
          • 修改load_font.php文件中引入的autoload.php為項目實際的位置
          • 在命令行中執行php load_font.php simkai /path/to/simkai.ttf

          這樣,我們就可以在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的詳細內容,更多請關注其它相關文章!

          更多技巧請《轉發 + 關注》哦!

          詳細]php調用python腳本,將word轉為html代碼及調用失敗處理

          起因:因為公司遇到發稿問題,很多人喜歡用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:各個腳本盡量使用全路徑


          主站蜘蛛池模板: 午夜福利无码一区二区| 国产一区二区三区福利| 中文字幕一区二区三区在线观看| 人妻无码第一区二区三区| 在线中文字幕一区| 日产精品久久久一区二区| 中文字幕一区二区三区在线不卡| 国产日韩精品一区二区在线观看| 日韩精品一区二区三区老鸭窝 | 无码成人一区二区| 亚洲AV日韩AV天堂一区二区三区 | 亚洲第一区香蕉_国产a| 蜜桃视频一区二区三区在线观看| 国产精品资源一区二区| 亚洲一区二区三区亚瑟| 国产品无码一区二区三区在线| 精品福利一区3d动漫| 免费无码一区二区三区| 精品一区二区三区四区电影| 国产人妖视频一区二区| 日韩在线一区视频| 国产精品无圣光一区二区| 中文字幕无线码一区2020青青| 日韩精品一区二区三区视频| 日本不卡免费新一区二区三区| 精品国产一区二区三区| 精品伦精品一区二区三区视频| 日本午夜精品一区二区三区电影 | 国产高清在线精品一区二区| 亚洲AV无码一区二区三区久久精品| 免费视频精品一区二区| 中文字幕人妻AV一区二区| 国产在线观看一区二区三区精品 | 中文无码精品一区二区三区| 精品一区二区三区在线视频观看 | 2020天堂中文字幕一区在线观 | 精品视频一区二区三区四区| 精品国产一区二区三区不卡| 波多野结衣AV一区二区三区中文 | 国产在线精品观看一区| 国产伦精品一区二区三区不卡|