'Laravel - base64 to pdf

I have a base64 text, I can convert it to pdf as I want with the following codes.

$decoded = base64_decode($r->labelData);
$file = 'label.pdf';
file_put_contents($file, $decoded);
if (file_exists($file)) {
     header('Content-Description: File Transfer');
     header('Content-Type: application/octet-stream');
     header('Content-Disposition: attachment; filename="'.basename($file).'"');
     header('Expires: 0');
     header('Cache-Control: must-revalidate');
     header('Pragma: public');
     header('Content-Length: ' . filesize($file));
     readfile($file);
     exit;
    }

But I want to use it with DOMPDF library. When I use DOMPDF the pdf page returns empty. Where am I missing?

    $pdf = PDF::loadView('myPDF', ['data' => $decoded]);
    return $pdf->setPaper('a4')->download('labels.pdf');

myPDF.blade.php

<body>
    {{$data}}
</body>


Solution 1:[1]

DomPDF is a html to pdf generator, if you already have the pdf content there is no need for using DomPDF.

Just return the content as PDF:

$filename = 'some-filename.pdf';

return response()->make($data, 200, [
    'Content-Type' => 'application/pdf',
    'Content-Disposition' => 'inline; filename="'.$filename.'"'
]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Zwarthoed