如果您的所有图像和其他图像都在执行脚本的同一台服务器上,您实际上并不需要打开isRemoteEnabled。
为什么它不加载你的图片
DomPdf 保护您免受通过它的攻击。根据documentation,以下内容不起作用:
$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);
您应该将 CHROOT 更改为您想要的绝对路径
$dompdf->getOptions()->setChroot("C:\\laragon\\www\\your-local-website\\public");
然后您可以将任何<img> 和src 从/public 文件夹内(可以嵌套)插入到HTML 中。
安全说明
将 Chroot 设置为您的应用根文件夹似乎很容易,但不要。它打开了一扇令人讨厌的门,你想关上它。据说/public里面没有关键脚本,只有图片、公共文档、路由等。
使用说明
请注意,我使用了与文档中使用的不同的目录分隔符。我相信最好的做法是按照以下方式做一些事情:
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);