Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sisa95ita's avatar

How to print some part of blade file with domPDF

Hi. I have to create a pdf file in Laravel. So I'm using domPDF. But I have to generate the pdf with only some parts of my blade file. I've already tried to create a new blade file with the part that I'm interested and I include it in the old blade file, but this return to me errors (Undefined variable) as the new blade file can't get the data I passed with the controller, so what can I do?

This is the function I've created in my controller:

public function printPDF(){
$pdf = PDF::loadView('pages.newblade');
return $pdf->download('document.pdf');
}

Thanks

0 likes
4 replies
Yapiyo's avatar

you could use:

$viewhtml = View::make('pages.newblade', [variables])->render();

$dompdf = new Dompdf();
$dompdf->loadHtml($viewhtml);
$dompdf->render();
// Output the generated PDF to Browser
$dompdf->stream();

edit:

is just saw when you use the: barryvdh/laravel-dompdf

$pdf = PDF::loadView('pages.newblade', $data);

if you do not want of can't pass data to that function maybe use a composer: https://laravel.com/docs/5.7/views#view-composers

minjon's avatar

@sisa95ita It took me 3 tries to find out how to do it. :) Let's say you have a blade file with printPdf button, and a blade partial you want to export in pdf with your model data passed to it.

routes\web.php

Route::get('myAddressOne/{myModel}', 'MyController@show');

Route::get('myAddressTwo/{myModel}', 'MyController@myPdfMethod')->name('pdf');

MyController.php

use App\MyModel;
use PDF;

class MyController extends Controller
{
    public function show(myModel $model)
    {
        return view('myBladeFile', compact('model'));
    }

    public function myPdfMethod(myModel $model)
    {
        $pdf = PDF::loadView('myBladePartial', compact('model'))->stream();
        
        return $pdf;
    }
}

myBladeFile.blade.php

<a href="{{ route('pdf', $model) }}">

    Print Pdf

</a>
sisa95ita's avatar

Thanks @minjon for your reply, this is a great solution and in fact it works, but I forgot to specify that I haven't to use a partial blade but only the original blade that I have to extract the part that I'm interested. How can I do this with your code? Thanks

minjon's avatar

@sisa95ita Sorry, I don't understand the question. Just extract any part of your code to a separate file and use that separate file in the loadView() method.

Please or to participate in this conversation.