richard's avatar

Generating an Invoice in PDF - Laravel

I want to generate an invoice with client details and services offered to that particular client. I have managed to design the invoice template using this https://htmlpdfapi.com/blog/free_html5_invoice_templates. My problem is converting the content into PDF. I have managed to pull https://github.com/barryvdh/laravel-dompdf into my vendors folder. I have also managed to create a sample PDF file using

get('test', function(){
    $pdf = \PDF::loadHTML('<h1>Test</h1>');
    return $pdf->stream();
});

My problem is, how do I save save a foreach loop data into a variable so that I can pass it as an argument this way?

get('test', function(){
    $pdf = \PDF::loadHTML($data);
    return $pdf->stream();
});

The $data variable will contain all the data(services offered) I want to be displayed in PDF file.

0 likes
6 replies
Penderis's avatar
Level 3

To use the data variable it must be a associative array 'CustomerName'=>'John Doe' so you would build it similar in most cases like when you build the validation $data variable.

So then you can use $CustomerName directly in your view.

test route from a project where i used dom-pdf

Route::get('pdf/po/{id}',function($currentBidId){
    /*
     * pass data through to invoice view
     */
    $invoiceData = \AcceptedAuctionBid::where('transaction_code','=',$currentBidId)
        ->with('withBid','belongsToSeller','belongsToBidder','belongsToBidWithAnimal')
        ->first();
       $invoiceInfo = [
           'invoice_number'=>$invoiceData->transaction_code,
           'bid_amount'=>$invoiceData->withBid->bid_amount,
           'timestamp'=>$invoiceData->withBid->created_at,
           //sellers details
           'sellers_company'=>$invoiceData->belongsToSeller->businessInformation->company_name,
           'sellers_email'=>$invoiceData->belongsToSeller->businessInformation->email,
           'sellers_tel'=>$invoiceData->belongsToSeller->businessInformation->tel,
           'sellers_vat_nr'=>$invoiceData->belongsToSeller->businessInformation->vat_nr,
           'sellers_address'=>explode(',',$invoiceData->belongsToSeller->businessInformation->address),
               ];


    //end data
    $invoice = PDF::loadView('pdf.purchaseorder',$invoiceInfo);
    return $invoice->stream();
});

then in my view I can use the keys as variables directly.

2 likes
richard's avatar

@Penderis Thanks, but my case is slightly different from yours. Your display doesnt require foreach loop because of Model::where('foo', $bar)->first(). For me I have to loop through some data in the views because I'm displaying like 4 or 5 services in that particular invoice.

Penderis's avatar

either use the toArray() and nice thing is in the view you still use blade so dd() to see variable structure and start building the view like you usually would. or

you either pass a associative array and get to the data the same way you would using blade, $data is then 'data'=>$objectFromDatabase , and in your view try dumping the $data variable to get a sense of structure assuming it will then be $data[0]->objectFromDatabase. something first array key of variable then you have access to the database object you passed it. I built mine before hand so in have the variable names in my view exactly where I want it, the actual array I pass as you see in my database call is 5 relations over 40 values.

even with a foreach loop you can still build an array up by declaring the array before hand and adding keys and values as your loop goes.

richard's avatar

Thanks! That works perfectly. Am just blonde in these PDF packages :)

Please or to participate in this conversation.