Home > Software engineering >  Unable to retrieve data from database to a PDF generated by dompdf package
Unable to retrieve data from database to a PDF generated by dompdf package

Time:12-26

I cannot retrieve data from the database to a PDF generated by the dompdf package. The PDF file gets generated, but it is empty. Any ideas?

invoiceController

public function pdf_print()
{
    $invoices = Invoice::all();

    $pdf = PDF::loadView('invoice.pdf', compact('invoices'));
    
    return $pdf->download('invoice.pdf');
}

CodePudding user response:

There is a lot we don't know from your question, but my thought is that you may not be loading the PDF correctly. Is there a view file located within your invoice folder inside your views folder? IE DOMPDF is looking to create a view from a pre-made template and your data. It passes your data to the view and creates the PDF. So, inside loadview() should be a view, not the file name.

E.g.:

public function downloadPDF(){
    $data = [
       "full_name"=> "john do",
       "email"=> "[email protected]",
       "age"=>"23",
       "phone"=>"998877XXXX",
    ];
    $pdf = PDF::loadView('nameOfMyPdfVIEW',$data);
    return $pdf->download('nameOfYourFile.pdf');
}

Where nameOfMyPdfVIEW is the name of a blade file inside your views folder.

CodePudding user response:

Here is the code I've used once.

        $form5_data = FormFive::where('user_id', $user_id)->get();

        foreach ($form5_data as $form) {

            $form5[] = $form;
        }

        $path = Storage::disk('public')->path("forms/form5/$user_email.pdf");

        $pdf = PDF::loadView('pdf.form5_pdf',['data' => end($form5),'user_email'=>$user_email]);

        return $pdf->save($path)->download("$user_email.pdf");
  • Related