2

I'm using mpdf to generate pdfs. All fine, until I switched to https. After that, pdfs are still correctly generated, but images are broken. Their sources are correctly written with https protocol on the php template. And I also tried to use only relative paths. Nothing.

The following is sample code form my class:

  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);
  }
Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
  • Can you check the logs to see if the images are even being requested from the server? – ivanivan Oct 04 '18 at 20:25
  • add some debugging: `$this->mpdf->showImageErrors = true;` –  Oct 04 '18 at 20:37
  • Hi, I did. What I get is `PHP Fatal error: Uncaught Mpdf\MpdfImageException: Could not find image file (https://www.[mydomain].com/wp-content/themes/[mytheme]/images/[myimage].jpg)` but, in fact, this very url exists and it's visible: if I copy-paste it right into the browser I'll see the image. So I really can't understand... – Luca Reghellin Oct 05 '18 at 06:15

4 Answers4

4

Found a solution here:

Images with https in mpdf

We can set this

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

And automagically it will solve, at least with my program above. Obviously, this is not a 'secure' solution especially if the images and/or contents are unknown/unpredictable. You should otherwise strive to get a working certificate. Two good articles about cert settings are these:

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

This last in particular also gives links to https://curl.haxx.se/ and its certificates. I didn't tested though.

Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
  • Upgrading the server's version of OpenSSL, cURL and CACertificates should also resolve the issue. If PHP was compiled manually, it would need to be recompiled. – Jake Jackson Oct 22 '18 at 23:12
1

I find in mpdf class function file_get_contents_by_curl. Add this 2 lines after curl_init function.

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

After save mpdf work fine, and no issues with images.

0

This question is kinda old (so is the mpdf version I am stuck with atm), but there is another recent answer, and for me none of the existing ones worked (website/server has a valid SSL certificate, and relative paths were unsuccessful).

I was lucky in that there were only 2 static images in my pdf and I control the HTML directly, so I solved my issue by converting the images to base64 (with a tool like https://www.base64-image.de/), rather than linking to them.

Not an ideal solution, but a valid one nonetheless, so I figured I'd drop this here in case someone else needs a quick fix.

krokador
  • 39
  • 6
-1

curlAllowUnsafeSslRequests didn't helped me. But I found that image variables works fine even over https..

https://mpdf.github.io/what-else-can-i-do/images.html#image-data-as-a-variable

Essentialy you get the content of an image file outside of a template and then pass it there as a variable:

$mpdf->imageVars['myvariable'] = file_get_contents('alpha.png');

In template:

<img src="var:myvariable" />
Ono Tosamo
  • 31
  • 6
  • A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it is there, then quote the most relevant part of the page you are linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](/help/deleted-answers) – Luca Kiebel Mar 01 '22 at 14:14
  • I didn't realize, sorry. Still, I think that link only answer is better than none, if someone is looking for a solution.. – Ono Tosamo Mar 03 '22 at 13:44