Laravel - Barryvdh Pdf - How Include External CSS File into HTML View

All of a sudden external links not working in barryvdh/laravel-dompdf

I've managed to solve this issue.

Posting here incase it can help someone.


Replaced barryvdh/laravel-dompdf with barryvdh/laravel-snappy



Package Link : https://github.com/barryvdh/laravel-snappy



My external styles, images & fonts are not loaded.

For styles i have created a blade file, updated it with css codes and included it in the pdf template blade.

For images i have created an absolute path

<img src="{{ public_path('images/img1.jpg')}}" alt="No Image" />

For font i have installed the font on the server,
I am using ubuntu 16.04, I have installed open sans and SimHei (Chinese font) and updated the font-family to open sans & simhei in my application.

Why the generated pdf doesn't is not CSS styled?

The style and view are not automatically inherited, domPdf needs to be fed a specific view and css in order to work properly.

Have a look here:

How to include the external style sheet in dom pdf

and this is a very detailed guide on how to achieve that:

https://www.positronx.io/laravel-pdf-tutorial-generate-pdf-with-dompdf-in-laravel/

Laravel barryvdh/laravel-dompdf multiple components of view in one pdf

There are certain things that are wrong in the code snippet you have provided, I will explain those first then provide correct way of doing it(for which I get results).

  • Firstly

you have used with & compact together, I don't know if it gets correct results but you should use any one of them or use array syntax of view method.

  • Secondly

what you are doing it you are rendering the view to html & then concating it, so , your html would look like,

<html>
.... content
</html>
<html>
.... content
</html>
& so on.
  • Thirdly

You css is messed up & not working as you want because of inline css you have added.

Solution

I would have used View Components to create similar views with different data.
So we would create a component in resources/views/components/single.blade.php (here I have named it single).
I have added your repeative code in the component single from your view. Now your coupon div class is in the component single.

<div class="coupon">
<div class="page">
<div class="subpage">
<div>
<h2>{{ $kuponas->kupono_nr }}</h2>
<h3>
Dovanų kupono numeris:
<span></span>
</h3>
</div>
</div>
</div>
</div>

Then in your view,

<!DOCTYPE html>
<html lang="en">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
<title>Dovanų kuponas</title>
<style>
.page-break {
page-break-after: always;
}
</style>
</head>
<body>
@php $count = 0;@endphp
@foreach ($kuponai as $kuponas)
@php $count++ @endphp
<div class="container {{ (count($kuponai)-1 >= $count) ? 'page-break' : '' }}">
@component('pdf.components.single', ['kuponas' => $kuponas])
@endcomponent
</div>
@endforeach
</body>

</html>

use page-break class to create breaks, I have used count condition to remove one extra page break. I am passing the data of kuponas to the component.

Finally change your controller,

public function multiplePagesPdf(Request $request)
{
$kuponai = Kuponas::all();
//dd($kuponas);

$view = view('pdf.multiple', ['kuponai' => $kuponai]);
$html = $view->render();
$pdf = PDF::loadHTML($html)->setPaper('a4', 'landscape');
return $pdf->stream('sugeneruoti.pdf');
}


Related Topics



Leave a reply



Submit