在 laravel 中使用 DOMPDF 生成动态 pdf

generating dynamic pdf with DOMPDF in laravel

我正在尝试使用动态数据(主要取自表格)生成发票 pdf。这是代码:

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';
    }
}

但是,我收到此错误:

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

这是因为在 invoice() 函数中,我调用了 invoice_form() 函数并且没有获取任何参数。我认为这不是正确的方法。我应该如何处理它?

由于您的 invoice_form(Request $request) 需要 Request 模型作为参数,您可以更改 invoice() 方法以将 $request 作为参数并将该 $request 传递给你的 invoice_form() 方法。变化

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 invoice(Request $request) {
        $institution = $this->institution();
        $user = $this->user();
        $invoice = $this->invoice_form($request);

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