Reputation:
I am using domPDF for generating PDF's after a user submit a button. I noticed that if I use
<link rel="stylesheet" href="{{asset('health/css/bootstrap.min.css')}}">
it will not generate PDF and it is always loading forever. I tried to use file_get_contents
but seems it is not also working. Can someone tell me what should I do about this? If you know.
Controller
$css = 'health/css/bootstrap.min.css';
$data_type = pathinfo($css, PATHINFO_EXTENSION);
$css_data = file_get_contents($css);
//I can get the image by using this code
$path = 'health/img/logo.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
//for testing view
return view('emails.form',compact('base64','css_data'));
Blade
<link rel="stylesheet" href="{{$css_data}}">
<img src="{{$base64}}" class="img img-fluid img-center">
Upvotes: 1
Views: 12682
Reputation: 41
if you are using Laravel and loading external CSS links inside a Blade file, then follow these steps.
In the blade file load external CSS this way.
In your controller use the following code add to load HTML with an external CSS link.
$html = View::make('your view fill path')->render(); $dompdf = new Dompdf(); $dompdf->set_option('isHtml5ParserEnabled', true); $dompdf->set_option('isRemoteEnabled', true); $dompdf->loadHtml($html); //re $dompdf->setPaper('A4', 'portrait'); $dompdf->render(); return $dompdf->stream('example.pdf');
Upvotes: 0
Reputation: 21
For anyone having frustration with this, Sn0wCrack's answer was close, but for me, using the public facing URL worked best.
This didn't work:
<link rel="stylesheet" href="{{ public_path('reports/css/bootstrap.css') }}">
This however did work:
<link rel="stylesheet" href="{{asset('reports/css/bootstrap.css')}}">
The ASSET_URL variable must be set in your .env for this to work correctly.
**Note: You can also use {{url('path')}} for this but it doesn't work when hosting on serverless like Vapor, if you use asset, it will rewrite the asset url by injecting the ASSET_URL for you on the deployment. You do not have to inject the ASSET_URL yourself when using Vapor.
Upvotes: 1
Reputation: 73
DomPDF requires using the absolute local path to the CSS file in your link element's href attribute, rather than the external URL to the file.
You'd want to use something similar to the following:
<link rel="stylesheet" href="{{ public_path('health/css/bootstrap.min.css') }}">
This is the same for any external resource you wish to use (CSS, Image, etc.)
You can find more information about this here from their GitHub wiki.
If you are using DomPDF directly you can also set the base path that DomPDF loads from, see here for another answer about this.
Upvotes: 3