首页 > 解决方案 > https上的mpdf损坏的图像

问题描述

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

标签: phpsslcurlhttpsmpdf

解决方案


在这里找到了解决方案:

mpdf中带有https的图像

我们可以设置这个

//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/@fhferreira/file-get-contents-ssl-operation-failed-php-4297ad92977f

最后一个特别是还提供了指向https://curl.haxx.se/及其证书的链接。虽然我没有测试。


推荐阅读