Skip to content
Advertisement

Too few arguments to function when downloading a PDF

I am trying to generate a PDF with some details of an individual user using the Barryvdh DomPDF library but having some problems trying to generate it.

Controller method:

public function downloadPDF(Card $card)
{
    $user = User::find($card->user_id);

    $pdf = (new BarryvdhDomPDFPDF)->loadView('pdf/cardsReport', $user);

    return $pdf->download('cards.pdf');
}

Here is how I am referencing the route.

<a href="{{ route('get::admin.download-pdf', ['card' => $profile->card->display()]) }}">Download PDF</a>

Route:

$router->get(
    '/downloadPDF/{card}',
    [
        'as' => 'get::admin.download-pdf',
        'uses' => 'EditCardController@downloadPDF',
    ]
);

I get this error:

Type error: Too few arguments to function BarryvdhDomPDFPDF::__construct(), 0 passed in EditCardController.php and exactly 4 expected.

I’m confused by this as I’ve seen many samples of using pdf and laravel where you don’t need to pass four arguments so was wondering why this would be?

Advertisement

Answer

According to documentation, you should use facade instead of constructor:

public function downloadPDF(Card $card)
{
    $user = User::find($card->user_id);

    $pdf = PDF::loadView('pdf/cardsReport', $user);

    return $pdf->download('cards.pdf');
}
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement