Home > Enterprise >  generating dynamic pdf with DOMPDF in laravel
generating dynamic pdf with DOMPDF in laravel

Time:05-22

I am trying to generate an invoice pdf with dynamic data (mostly taken from a form). This is the code:

class PDFController extends Controller
{
    public function invoice() {
        $institution = $this->institution();
        $user = $this->user();
        $invoice = $this->invoice_form();

        return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
    }

    public function institution() {
        $institution = Institution::where('id', 1)->get()->first();

        return $institution;
    }

    public function user() {
        $user = Auth::user();

        return $user;
    }

    public function invoice_form(Request $request) {

        $this->validate($request, array(
            'furnizor-select' => 'required',
            'document-number' => 'required',
            'document-date' => 'required',
            'due-date' => 'required',
            'discount-procent' => 'required',
            'discount-value' => 'required',
            'total-value' => 'required',
            'nir-number' => 'nullable'
        ));

        $invoice = new \App\Models\Invoice();
        $invoice->provider_id = $request->input('furnizor-select');
        $invoice->number = $request->input('document-number');
        $invoice->document_date = $request->input('document-date');
        $invoice->due_date = $request->input('due-date');
        $invoice->discount_procent = $request->input('discount-procent');
        $invoice->discount_value = $request->input('discount-value');
        $invoice->total = $request->input('total-value');
        $invoice->save();

        $invoices = Invoice::all();
        $invoice_id = $invoices->last()->id;

        $old_date = $request->input('document-date');
        $new_date = date("d-m-Y", strtotime($old_date));  
        $provider_id = $request->input('furnizor-select');
        $provider = Provider::where('id', $provider_id)->get();
        $invoice_number = $request->input('document-number');
        $old_due_date = $request->input('due-date');
        $new_due_date = date("d-m-Y", strtotime($old_due_date));  

        $filename = 'pdfs/nir'.$invoice_id.'.pdf';
    }
}

However, I am getting this error:

Too few arguments to function App\Http\Controllers\PDFController::invoice_form(), 0 passed in /Users/cristimamota/NEWSAJ BACKUP PRE FINAL/app/Http/Controllers/PDFController.php on line 17 and exactly 1 expected

And this is because in the invoice() function, I am calling the invoice_form() function and is getting no parameter.. I think this is not the correct way to do it. How should I approach it?

CodePudding user response:

Since your invoice_form(Request $request) requires a Request model as parameter you can change your invoice() method to take $request as parameter and pass that $request to your invoice_form() method. Change

public function invoice() {
        $institution = $this->institution();
        $user = $this->user();
        $invoice = $this->invoice_form();

        return view('pdf-generation.invoice')->with(['institution' => $institution, 'user' => $user]);
}

to

public function invoice(Request $request) {
        $institution = $this->institution();
        $user = $this->user();
        $invoice = $this->invoice_form($request);

        return view('pdf-generation.invoice')->with([
            'institution' => $institution, 
            'user' => $user
        ]);
    }
  • Related