Home > Software engineering >  Why the generated pdf doesn't is not CSS styled?
Why the generated pdf doesn't is not CSS styled?

Time:09-22

I have a view welcome.blade.php with the headear like this:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

    <title>Blog</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <link href="{{ asset('css/app.css') }}" rel="stylesheet" media="print">
    <link href="{{ asset('css/example.css') }}" rel="stylesheet" media="print">
    <link rel="preconnect" href="https://fonts.gstatic.com">
</head>

<body class="font-sans antialiased w-full">

....

I'm using tailwindcss, this page has a responsive layout with many colors, svgs, etc. And it's working fine, however I'm trying to generate a pdf of this page with barryvdh/laravel-dompdf. But when the pdf is downloaded it doesn't have any style, no colors, it has a serif font, and the layout is all unorganized.

Route for the pdf functionality:

Route::get('/', [PDFController::class, 'generatePDF']);

PDFController:

use Illuminate\Http\Request;
use PDF;

class PDFController extends Controller
{
    public function generatePDF()
    {
        $data = [
          'title' => 'PDF Title',
          'author' => 'Author'
        ];

        $pdf = PDF::loadView('welcome', $data);
        return $pdf->download('test.pdf');
    }
}

Do you know what can be the issue?

CodePudding user response:

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/

  • Related