You don't really need isRemoteEnabled
turned on, if all your images and what not are on the same server that executes the script.
Why it doesn't load your image
DomPdf protects you from being attacked through it. As per documentation the following won't work:
$dompdf = new Dompdf();
$dompdf->getOptions()->getChroot(); // something like 'C:\\laragon\\www\\your-local-website\\vendor\\dompdf\\dompdf'
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<img src="C:\\laragon\\www\\your-local-website\\public\\img\\logo.png">
</body>
</html>
HTML;
$dompdf->loadHtml($html);
You should change CHROOT to your desired absolute path with
$dompdf->getOptions()->setChroot("C:\\laragon\\www\\your-local-website\\public");
and then you can insert any <img>
with src
from within (can be nested) that /public
folder into HTML.
Security note
It seems like an easy win to just set Chroot to your app root folder, but don't. It opens up a nasty gate, one you want to keep shut. Supposedly there are no critical scripts in /public
, only images, public documents, routing etc.
Usage note
Note that I used a different directory separator than the one being used in documentation. I believe the best practice would be to do something in lines of:
define('DS', DIRECTORY_SEPARATOR);
$public = ABSPATH . DS . 'public'; // ABSPATH is defined to be __DIR__ in root folder of your app
$image = $public . DS . 'logo' . DS . 'logo-md.png';
/* Optional */
$saveLocation = ABSPATH . DS . '..' . DS . 'private' . DS . 'invoices'; // Note that save location doesn't have to be in '/public' (or even in 'www')
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<style type="text/css">
* {
font-family: DejaVu Sans !important;
}
@page {
margin: 0;
padding: 0;
}
html, body {
margin: 0;
min-height: 100%;
padding: 0;
}
</style>
</head>
<body>
<img src="{$image}">
</body>
</html>
HTML;
$dompdf = new Dompdf();
$dompdf->getOptions()->setChroot($public);
$domPdf->loadHtml($html, 'UTF-8');
$domPdf->setPaper('A4', 'portrait');
$domPdf->render();
/* either */
$domPdf->stream($filename); // filename is optional
/* or */
$outputString = $domPdf->output();
$pdfFile = fopen($saveLocation, 'w');
fwrite($pdfFile, $outputString);
fclose($pdfFile);