Laravel中在Blade文件显示SVG图片及Dompdf生成PDF时加载SVG Logo的问题
Got it, let's break this down into two parts to solve your problems step by step:
Since your SVG logo is stored as a Blade partial (resources/layouts/inc/logo.blade.php), you don't need to use asset() here. Blade partials are meant to be included directly in other views to render their content inline. Just use the @include directive:
@include('layouts.inc.logo')
This will inject the raw SVG code straight into your Blade file, so it renders as a proper image instead of a URL string.
Your current approach with asset('layouts.inc.logo') fails for two key reasons:
asset()targets files in thepublicdirectory, but your logo partial lives inresources/views—so the URL it generates is invalid.- Even if the URL were correct, Dompdf often struggles to load external/relative resources reliably without extra configuration.
Here are two reliable fixes:
Solution 1: Embed SVG content directly into the PDF HTML
This is the simplest approach. Render your Blade partial to a raw SVG string in your controller, then inject that string into the HTML you pass to Dompdf:
// In PdfController.php // Render the Blade partial to get the raw SVG code $logoSvg = view('layouts.inc.logo')->render(); // Build your PDF HTML with the inline SVG $output = '<div class="col-xs-12">' . $logoSvg . '</div>'; // Pass $output to Dompdf as you normally would
Dompdf handles inline SVG natively, so this will display your logo correctly in the final PDF.
Solution 2: Use a Base64-encoded data URI (for <img> tag lovers)
If you prefer using an <img> tag instead of inline SVG, convert the SVG content to a base64 data URI. This embeds the image directly into the HTML, so Dompdf doesn't need to fetch external files:
// In PdfController.php $logoContent = view('layouts.inc.logo')->render(); $base64Svg = base64_encode($logoContent); $svgDataUri = 'data:image/svg+xml;base64,' . $base64Svg; $output = '<div class="col-xs-12"><img src="' . $svgDataUri . '" alt="Logo"></div>';
Quick Check: Dompdf SVG Support
Make sure you're using a recent version of Dompdf (older versions had limited SVG support). If you still run into issues, confirm the enable_remote config option is set to true (though our solutions above don't require this, it's a good fallback for other external resources).
内容的提问来源于stack exchange,提问作者Sam Leumas

