【问题标题】:mpdf broken images over httpshttps上的mpdf损坏的图像
【发布时间】:2019-03-10 07:19:54
【问题描述】:

我正在使用 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);
  }

【问题讨论】:

  • 您能否检查日志以查看是否正在从服务器请求图像?
  • 添加一些调试:$this->mpdf->showImageErrors = true;
  • 嗨,我做到了。我得到的是PHP Fatal error: Uncaught Mpdf\MpdfImageException: Could not find image file (https://www.[mydomain].com/wp-content/themes/[mytheme]/images/[myimage].jpg),但事实上,这个 url 存在并且它是可见的:如果我将它复制粘贴到浏览器中,我会看到图像。所以我真的无法理解……

标签: php ssl curl https mpdf


【解决方案1】:

这个问题有点老了(我坚持使用 atm 的 mpdf 版本也是如此),但是最近有另一个答案,对我来说,现有的没有一个有效(网站/服务器具有有效的 SSL 证书和相对路径不成功)。

我很幸运,因为我的 pdf 中只有 2 个静态图像并且我直接控制 HTML,所以我通过将图像转换为 base64(使用 https://www.base64-image.de/ 之类的工具)解决了我的问题,而不是链接到它们.

不是一个理想的解决方案,但仍然是一个有效的解决方案,所以我想我会把它放在这里以防其他人需要快速修复。

【讨论】:

    【解决方案2】:

    我在 mpdf 类函数 file_get_contents_by_curl 中找到。 在 curl_init 函数之后添加这 2 行。

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    

    保存 mpdf 后工作正常,图像没有问题。

    【讨论】:

      【解决方案3】:

      在这里找到了解决方案:

      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/ 及其证书的链接。不过我没有测试。

      【讨论】:

      • 升级服务器版本的 OpenSSL、cURL 和 CACertificates 也应该可以解决问题。如果 PHP 是手动编译的,则需要重新编译。
      猜你喜欢
      • 2011-01-08
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-08
      • 1970-01-01
      • 2014-04-26
      相关资源
      最近更新 更多