You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中在Blade文件显示SVG图片及Dompdf生成PDF时加载SVG Logo的问题

Got it, let's break this down into two parts to solve your problems step by step:

1. Displaying SVG in Blade Templates

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.

2. Rendering the SVG Logo in Dompdf-Generated PDF

Your current approach with asset('layouts.inc.logo') fails for two key reasons:

  • asset() targets files in the public directory, but your logo partial lives in resources/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.01 01:32:43