laravel-8laravel-dompdf

How can I add bookmarks on my laravel-dompdf generated file?


I have a laravel 8.75 application that use barryvdh/laravel-dompdf 2.0.1.

It creates pdf file from database and save it to the storage file. I need to add bookmarks on each dynamic data on the pdf file, I read that it's not possible with this package (laravel-dompdf) but this answer is aged (2012).

I tried to add ancres into my blade view and enabled php into dompdf configuration but it doesn't work.

This is my create method :

    public function create() 
    {
        $baseUrl = config('app.url') . '/site/public/';
        ini_set('memory_limit', '256M');
        $datas = Compatibilite::all()->groupBy('marque');

        foreach ($datas as $marque => $group) {
            $datas[$marque] = $group->sortBy('ordre');
        };
        try {
            $pdf = Pdf::loadView('pdf.compatibilityTablePdf', [
                'datas' => $datas,
                'baseUrl' => $baseUrl,
            ]);
        } catch (\Throwable $th) {
            Log::error($th);
            return;
        }

        if($pdf) {
            try {
                $content = $pdf->download()->getOriginalContent();
            } catch (\Throwable $th) {
                Log::error($th);
                return;
            }
        } else {
            Log::error('Erreur de création du pdf');
        }
        if($content) {
            try {
                Storage::put('public/docs/doc.pdf', $content);
            } catch (\Throwable $th) {
                Log::error($th);
                return;
            }
        }
    }

And my blade view for pdf :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Compatibilites</title>
</head>
<body>
        <header>
          [...]
        </header>
        <footer>
          [...]
        </footer>
        <main>
            @foreach($datas as $brand => $elements)
            <div>
                <h2><a name="{{$brand}}"></a>{{$brand}}</h2>
                <table>
                    <thead>
                      [...]
                    </thead>
                    <tbody>
                      [...]
                    </tbody>
                </table>
            </div>
            @endforeach
        </main>
</body>
</html>

Does anybody know if it's possible now to add bookmarks on laravel-dompdf generated file?

And if not which package can I use instead of barryvdh/laravel-dompdf to do this?


Solution

  • Answer

    -> It is still not possible to add bookmarks to PDF files generated using barryvdh/laravel-dompdf. The package does not support this feature, and there is no known workaround.

    -> If you need to add bookmarks to your PDF files, you can use a different PDF generation library, such as mpdf or snappy. Both of these libraries support bookmarks, and they are both compatible with Laravel.