laraveldompdflaravel-9laravel-dompdf

DOMPDF: How to customize the name of the single pdf, relating to a single client, with the information present in the object


I am trying to create the pdf, using the domPDF library, for each single client present in the table of my application. So far everything ok!

When I go to save the pdf I want the name of the pdf document to be saved as:

name_client_surname_client.pdf,

so if for example I have a client named Alex Haiold, the document must be saved as Alex_Haiold.pdf

To do this, since I am passing the single client in the controller, as shown below, I tried to write

return $pdf->download('client->surname client->name.pdf');

but it is saved as

client-_surname client-_name.pdf (then printing client-_surname client-_name).

Here my code:

    public function generatePDF(Client $client)
    {
  
        $data = [
            'title' => 'Welcome to LaravelTuts.com',
            'date' => date('m/d/Y'),
            'client' => $client
        ]; 
            
        //dd($data);
        $pdf = PDF::loadView('client.clientPDF', $data);
     
        return $pdf->download('client->surname client->name.pdf');
    }

Can anyone kindly help me?


Solution

  • $pdf = PDF::loadView('client.clientPDF', $data);
    return $pdf->download($client->surname . ' ' . client->name . '.pdf');
    

    On the first line, we are just making a view file. The file is resources/views/client.clientPDF.blade.php and we are feeding data to the view file by passing the variable $data to it.

    After making the view, our next step is to download the pdf file. For doing this, we need to call the download method of the instance.

    On the first parameter, we are passing the filename with extension(pdf).