代码之家  ›  专栏  ›  技术社区  ›  Luca Reghellin

mpdf通过https中断图像

  •  1
  • Luca Reghellin  · 技术社区  · 6 年前

    我用mpdf生成pdf。一切都很好,直到我换成https。之后,PDF仍然正确生成,但图像被破坏。它们的源代码是用php模板上的https协议正确编写的。我也试着只用相对路径。没有什么。

      public function save_pdf($translate = false){
        $this->mpdf = new \Mpdf\Mpdf();
        $this->mpdf->CSSselectMedia='mpdf';
        //$this->mpdf->showImageErrors = true; // this will log errors into the php log file
    
        $ch = curl_init($this->pdf_css_path . '/configurator-pdf.css');
        // this disables ssl check: unsafe
        if($this->disable_ssl_check) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        $css = curl_exec($ch);
        curl_close($ch);
    
        $template = ($translate) ? $this->pdf_template_url : $this->pdf_template_url_it;
        $filename = ($translate) ? $this->pdf_name : $this->pdf_it_name;
        $_POST['cid'] = $this->cid;
    
        $json = json_encode($_POST);
    
        $ch = curl_init($template);
        // this disables ssl check: unsafe
        if($this->disable_ssl_check) curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen($json) ]);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
        $html = curl_exec($ch);
        curl_close($ch);
    
        $this->mpdf->WriteHTML($css, 1);
        $this->mpdf->WriteHTML($html, 2);
    
        $pdf = $this->pdf_destination_path . DS . $filename;
        $this->mpdf->Output($pdf, \Mpdf\Output\Destination::FILE);
      }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Luca Reghellin    6 年前

    Images with https in mpdf

    我们可以设定这个

    //note: using $this only because in my case mpdf is a class prop
    $this->mpdf->curlAllowUnsafeSslRequests = true;
    

    它会自动解决,至少用我上面的程序。显然,这不是一个“安全”的解决方案,尤其是在图像和/或内容未知/不可预测的情况下。否则你应该努力获得工作证书。两篇关于证书设置的好文章如下:

    https://welaunch.io/plugins/woocommerce-pdf-catalog/faq/images-pdf-displays-red-cross-https-mpdf/

    https://medium.com/@f.h.ferreira/file-get-contents-ssl-operation-failed-php-4297ad92977f

    特别是最后一个链接 https://curl.haxx.se/