MPDF通过https破碎的图像

时间:2018-10-04 20:15:54

标签: php ssl curl 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);
  }

1 个答案:

答案 0 :(得分:1)

在这里找到了解决方案:

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