PHP------wkhtmltopdfの使い方

3813 ワード

一.問題の説明
phpでhtmlをpdfに生成する必要があります.php拡張を探しました.HTML 2 PDFは、簡単なHTMLコードしか生成できません.複雑なHTML+cssは生成できません.ネット上で強力なwkhtmltopdfを見つけた.
二.wkhtmltopdf紹介
ツールのフルネームは「wkhtmltopdf」です.Qt WebKitエンジンを使用してレンダリングされ、htmlドキュメントをpdfドキュメントまたはピクチャに変換できるコマンドラインツールです.(強調:コマンドラインツール)は複数のプラットフォームをサポートし、win、linux、os xなどのシステムで実行できます.
三.wkhtmltopdfインストール(linux環境)
インストール手順は簡単ですが、ここでは詳しく説明しません(https://blog.csdn.net/assasin...://blog.csdn.net/qq_2662641/article/details/72875344)を参照してください.
  1:     ,   pdf         (       ,     )?

  :        ,   linux    ,        (https://yq.aliyun.com/ziliao/269854)    ,            (http://www.font5.com.cn/font_download.php?id=150&part=1237886897)


                             ***      !!!!!!!***    
                             

三.php操作wkhtmltopdf
  :1.   exec  

     2.     (    ,           ,       php    ,    composer     
     
         mikehaertl/phpwkhtmltopdf )
         

四.mikehaertl/phpwkhtmltopdfパッケージの具体的な使用
1.パッケージのインストール(composerが便利)
2.具体的な使用は当該パッケージの使用説明を参照することができる
3.使用中に発生した問題:
    q.              ,   linux      ?
    a.php.ini      proc_open()   ,    (       )
    
    q.php.ini         ,    ,    ,          (    :       )?
    a.      ,       proc_open()---php    
    
        proc_open():      ,        /       。
        
        
        wkhtmltopdf     ,   pdf     &    (  &        ),     &         ,    ,       ,        ,      。   (  :vendor/mikehaertl/php-shellcommand/Command.php    execute    ):
        
        $process = proc_open($command, $descriptors, $pipes, $this->procCwd, $this->procEnv, $this->procOptions);
        if (is_resource($process)) {
            if ($this->_stdIn!==null) {
                if (is_resource($this->_stdIn) &&
                    in_array(get_resource_type($this->_stdIn), array('file', 'stream'), true)) {
                    stream_copy_to_stream($this->_stdIn, $pipes[0]);
                } else {
                    fwrite($pipes[0], $this->_stdIn);
                }
                fclose($pipes[0]);
            }
            $this->_stdOut = stream_get_contents($pipes[1]);
            $this->_stdErr = stream_get_contents($pipes[2]);
            fclose($pipes[1]);
            fclose($pipes[2]);

            $this->_exitCode = proc_close($process);

            if ($this->_exitCode!==0) {
                $this->_error = $this->_stdErr ? $this->_stdErr : "Failed without error message: $command";
                return false;
            }
        } else {
            $this->_error = "Could not run command $command";
            return false;
        }
        
        
            $descriptors,                    。            ,                。       :pipe (        : r             ,w             ),    file(         )。

実際のファイル記述子を表すストリームリソースタイプ(たとえば、開いているファイル、socketポート、stdINなど).
                ,       ,       w, linux     r,          。      :
     $descriptors = array(
            1   => array('pipe','w'),
            2   => array('pipe', $this->getIsWindows() ? 'a' : 'w'),

        );
     public function getIsWindows()
     {
    return strncasecmp(PHP_OS, 'WIN', 3)===0;
    }
    
      2   => array('pipe', $this->getIsWindows() ? 'a' : 'w'),=》     2   => array('pipe', 'a'),    。