Why DomPDF Ignores Your Bootstrap CSS Link
You are likely encountering a classic rendering discrepancy where your PDF view looks perfect in the browser but renders as plain, unstyled text when generated by barryvdh/laravel-dompdf. The issue isn't that DomPDF is broken; it's that the library operates under a fundamentally different set of rules regarding external resources and CSS loading.
The most immediate red flag in your setup is the usage of <link> tags to load an external stylesheet:
<link href="{{public_path('css/pdf.css') }}" rel="stylesheet" type="text/css" />
DomPDF, by default, does not follow relative paths or attempt to resolve URLs for external stylesheets in the same way a browser does. Even if you have copied your bootstrap.min.css (or your renamed pdf.css) into the public directory, DomPDF will often fail to locate it unless the path is absolute and explicitly handled by the library's configuration.
However, relying solely on fixing the CSS path is often insufficient. The real culprit is usually how the DOM is parsed. DomPDF relies heavily on the html5lib parser (in newer versions) or its own internal parser (in older versions), which strips out many modern HTML elements and attributes that standard browsers handle gracefully. Specifically, it struggles with complex layouts involving Bootstrap's grid system, flexbox utilities, and external font loading via Google Fonts.
Configuring the Loader
To make DomPDF recognize your custom CSS file located at public/css/pdf.css, you must tell the engine exactly where to look. You can achieve this by modifying the options passed to PDF::loadView().
Instead of relying on the <link> tag's relative path, inject an absolute URL directly into the options array. This forces DomPDF to load the stylesheet before rendering the body content. Update your controller as follows:
use PDF;
use Barryvdh\DomPDF\Facade\Pdf;
public function infor(News $news)
{
// ... data gathering logic
$pdf = PDF::loadView('admin.news_events.pdf', [
'news' => $news,
]);
// Explicitly set the CSS path as an absolute URL
$pdf->setOptions([
'defaultFont' => 'sans-serif',
'isHtml5ParserEnabled' => true, // Use HTML5 parser for better compatibility
'isRemoteEnabled' => false, // Disable remote access to external CDNs if not needed
'enableSmartFonts' => true, // Optional: Helps with specific font handling
// Force the specific CSS file path DomPDF needs to load
'defaultStylesheet' => public_path('css/pdf.css'),
]);
return $pdf->stream('pdfview.pdf');
}
Note that enableSmartFonts is crucial if your PDF relies on custom fonts defined in Bootstrap. Without it, the layout might collapse because font families are resolved to fallbacks.
The "Public Path" Trap
A common mistake developers make is assuming that {{public_path('css/pdf.css')}} inside a Blade view will always resolve correctly for DomPDF. While Laravel's Blade compiler handles this fine for browser output, DomPDF treats the rendered HTML as an internal document. If you are using relative paths or if the base URL isn't explicitly set during the rendering process, the parser might interpret css/pdf.css as a path relative to the file system root rather than the web server root, resulting in a 404 error that DomPDF silently ignores.
Using public_path() in the PHP controller ensures you are passing a valid filesystem path to the library's loader configuration, bypassing any ambiguity in the HTML tag itself.
Handling Bootstrap Classes
Even with the CSS loaded correctly, Bootstrap classes like .container, .row, and .col might not render as expected. DomPDF has historically had difficulty with the Bootstrap Grid system because it uses fixed widths and percentage-based layouts that conflict with the PDF's flow layout engine.
If you find that your columns are stacking vertically instead of side-by-side, consider simplifying the HTML structure for the report. Instead of relying entirely on Bootstrap's grid classes, use inline styles or CSS Grid/Flexbox defined directly in your pdf.css file. DomPDF handles standard CSS3 properties well when the parser is set to HTML5 mode (isHtml5ParserEnabled: true).
Font Handling
Finally, ensure that any fonts used in your PDF are actually installed on the server and not just referenced via a URL. If you are using Google Fonts or custom assets, DomPDF will fail to render them unless isRemoteEnabled is set to true (which poses a security risk) or you have downloaded the font files and registered them in your DOMPDF installation directory (public/fonts).
By explicitly configuring the stylesheet path and ensuring the parser handles HTML5 standards, you bridge the gap between the browser's rendering engine and DomPDF's limitations. This approach ensures that your pdf.css is respected without relying on fragile relative paths or unsupported Bootstrap grid behaviors.