Search code examples
phpdompdf

domPDF not downloading in Chrome


Generated domPDF can't be downloaded in Chrome. It works fine in other browsers but giving this error in Chrome:

Mixed Content: The site at 'https://xxx.io' was loaded over a secure connection, but the file at 'https://xxx.io/pdf/?doc=auAjvjYi8yyOJIfPq3t4prhrYdWsDDxXIG85XiRyNGo' was redirected through an insecure connection. This file should be served over HTTPS. This download has been blocked. See https://blog.chromium.org/2020/02/protecting-users-from-insecure.html for more details.

And this is the code block where I stream the PDF:

$domPdf = new Dompdf([
'isRemoteEnabled' => true,
]);
$domPdf->loadHtml($pdfHTML);
$domPdf->setPaper('A4');
$domPdf->render();
ob_end_clean();
$domPdf->stream($documentName, [
    'compress' => true,
    'Attachment' => true,
]);
exit();

And also the link to the PDF download route:

<a href="https://xxx.io/pdf?doc=auAjvjYi8yyOJIfPq3t4prhrYdWsDDxXIG85XiRyNGo" onclick="downloadPDF()" class="mobile-hide no-ul "><span class="icon-pdf"></span></a>

Solution

  • This is so stupid. Since I didn't specified exactly the route like https://xxx.io/pdf/index.php?doc=auAjvjYi8yyOJIfPq3t4prhrYdWsDDxXIG85XiRyNGo (Notice the index.php after /pdf/) Chrome was automatically making an internal 307 redirect request using http to the route and then decides it was not safe to operate...