Laravel PDF report

By | October 12th 2018 04:07:10 PM | viewed 20 times

Run the following command to install pdf library

D:\xampp_726\htdocs\ptm> composer require barryvdh/laravel-dompdf

So go to the config >> app.php and add the following configuration.

'providers' => [
    ....
    Barryvdh\DomPDF\ServiceProvider::class,
],
'aliases' => [
    ....
    'PDF' => Barryvdh\DomPDF\Facade::class,
],

Create any controller and add the following code:

<?php namespace App\Http\Controllers\Ecommerce;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Valtitle_enator;
use App\Models\Ecommerce\EcColor;
use App\Models\Ecommerce\EcOrderProductDtl;
use App\Models\Ecommerce\EcSroomProduct;
use App\Models\Ecommerce\EcOrder;
use App\Models\Ecommerce\EcSale;
use App\Models\Ecommerce\EcProdAmtPrice;
use App\Models\Ecommerce\EcAddrese;
use App\Models\Ecommerce\EcLocation;
use App\Models\Ecommerce\EcGuest;
use App\Models\Auth\Role;
use App\User;
use Carbon\Carbon;
use Session;
use DB;
use Auth;
use Illuminate\Support\Facades\Input;
use App\Models\Ecommerce\EcInvoice;
use PDF;                                                  // active dompdf

class EcReportController extends Controller {
     
	 public function invoiceQuery($id){
	 
	       $orderProduct = EcSale::orderBy('ec_sales.id','desc')
                    ->select(
                             
                              "ec_sales.sroom_product_id",
                              "ec_sales.id as productDtlId",
                              "ec_sroom_products.prod_amt_price_meta_name",
                              "ec_prod_amt_prices.detail_name",
                              "ec_prod_amt_prices.label",
                              "ec_prod_amt_prices.barcode",
                              DB::raw("IFNULL(sum(ec_sales.quantity),0) as total_qnt"),
                              DB::raw("IFNULL(sum(ec_sales.quantity),0)*ec_measure_units.munit as total_unit_qnt"),     
                              "ec_measure_units.title_en as measure_title_en",
                              "ec_measure_units.munit",
                              "ec_measure_units.dratio",
                              "ec_price_units.title_en as price_title_en",
                              "ec_product_images.thumb_file_1",
                              "ec_orders.user_meta_name",
                              "ec_orders.id as order_id",
                              "ec_orders.order_no",
                              "ec_orders.user_meta_name",
                              "ec_orders.guest_id",
                              "ec_orders.order_date",
                              "ec_orders.is_delevery",
                              "ec_orders.process_date",
                              "ec_invoices.invoice_date",
                              "ec_invoices.invoice_no"
							  )
                     ->join('ec_invoices','ec_invoices.id','=','ec_sales.invoice_id')
                     ->join('ec_orders','ec_orders.id','=','ec_invoices.order_id')
                     ->join('ec_sroom_products','ec_sroom_products.id','=','ec_sales.sroom_product_id')
                     ->join('ec_prod_amt_prices','ec_prod_amt_prices.meta_name','=','ec_sroom_products.prod_amt_price_meta_name')
                     ->join('ec_product_images','ec_product_images.prod_amt_price_meta_name','=','ec_sroom_products.prod_amt_price_meta_name')
                     ->join('ec_measure_units','ec_measure_units.meta_name','=','ec_prod_amt_prices.measure_unit_meta_name')
                     ->join('ec_price_units','ec_price_units.meta_name','=','ec_prod_amt_prices.price_unit_meta_name')
                     ->join('ec_business_names','ec_business_names.meta_name','=','ec_sroom_products.bus_name_meta_name')
                     ->join('ec_showrooms','ec_showrooms.meta_name','=','ec_sroom_products.showroom_meta_name')
                     ->join('users','users.business_name_meta_name','=','ec_sroom_products.bus_name_meta_name')
                     ->join('ec_addreses','ec_addreses.user_meta_name','=','users.meta_name')
                     ->join('ec_locations','ec_locations.id','=','ec_addreses.location_upazila_id')
                     ->leftJoin('ec_payment_types','ec_payment_types.meta_name','=','ec_orders.payment_type_meta_name')
                     ->where('ec_addreses.address_type','business')
                     ->where('ec_invoices.id',$id)
                     ->groupBy('ec_sroom_products.prod_amt_price_meta_name')
                     ->get();
					 
				 
		 return $orderProduct;
                 
	 }
    
     public function invoicePdf($id){
	 
	  
         
      $orderProduct = null;
      $customerIns = null;
      $guestIns = null;
      $orderIns = null;
      $userAddress = null;
      $shippingAddress = null;

           $orderProduct = $this->invoiceQuery($id);
           $orderIns = @EcOrder::where('id',DB::table('ec_invoices')->select('order_id')->where('id',$id)->first()->order_id)->first();
           $invoiceIns = \App\Models\Ecommerce\EcInvoice::find($id);
           
           $getOrderPaymentD = \App\Services\Ecommerce\EcommerceServices::getOrderPaymentDtl(@$orderIns->id);
		   

        
         
         if(@$orderIns->user_meta_name){
             
             @$userAddress = User::select('users.*','ec_addreses.address_type','ec_locations.keyword','ec_locations.title_en','ec_addreses.address','ec_addreses.zip','ec_addreses.mobile as mobile2','ec_addreses.phone as phone2')
                          ->leftJoin('ec_addreses','ec_addreses.user_meta_name','=','users.meta_name')
                          ->leftJoin('ec_locations','ec_locations.id','=','ec_addreses.location_upazila_id') 
                          ->where('users.meta_name',$orderIns->user_meta_name)
                          ->get();
             
         }else if(@$orderIns->guest_id){
             $guestIns = EcGuest::select('ec_guests.*')
                          ->where('ec_guests.id',@$orderIns->guest_id)
                          ->get(); 
         }
         
         $subtotal = 0;
         $vat =0;
         $shipping = 0;
         $coupon = 0;
         $employee_discount = 0;
         $gift_voucher_discount = 0;
		 $cod_total_discount = 0;
         $payable = 0;
         
         if($getOrderPaymentD){
                        
                        foreach($getOrderPaymentD as $getOrderP){
                           
                            if($getOrderP['subtotal']){
                             
                              $subtotal = @$getOrderP['subtotal'];
                            }
                           
                            if($getOrderP['total_vat']) {
                              
                               $vat = round($getOrderP['total_vat']);
                               }
                             
                             if($getOrderP['total_charge']){
                              
                               $shipping =@$getOrderP['total_charge'];
                                }
                             
                              if($getOrderP['total_coupon_amount']){
                             
                              
                               $coupon =$getOrderP['total_coupon_amount'];
                               }
							   
			                  if($getOrderP['total_employee_discount_amount']){
                             
                              
                               $employee_discount =$getOrderP['total_employee_discount_amount'];
                               }
                               
                                if($getOrderP['total_gift_voucher_discount']){
                                   $gift_voucher_discount =$getOrderP['total_gift_voucher_discount'];
                               }
							   
							    if($getOrderP['cod_total_discount']){
                                   $cod_total_discount =$getOrderP['cod_total_discount'];
                               }
							   
							   
                               
                               
                             
                              if($getOrderP['calculate_total']){
                               
                               $payable =round($getOrderP['calculate_total']);
                               }
                            
                        }
              }
          
 
          return view('ecommerce.ecReport.invoice_pdf',compact('orderProduct','orderIns','getOrderPaymentD','invoiceIns','userAddress','guestIns','subtotal','vat','shipping','coupon','payable','employee_discount','gift_voucher_discount','cod_total_discount'));
     
    }
    
     public function downInvoicePdf($id){
	 
	
       $orderProduct= null; 
       $orderProduct = $this->invoiceQuery($id);
        $orderIns = @EcOrder::where('id',DB::table('ec_invoices')->select('order_id')->where('id',$id)->first()->order_id)->first();
         $invoiceIns = \App\Models\Ecommerce\EcInvoice::find($id);
         $getOrderPaymentD = \App\Services\Ecommerce\EcommerceServices::getOrderPaymentDtl(@$orderIns->id);
         
         
         if(@$orderIns->user_meta_name){
             
             @$userAddress = User::select('users.*','ec_addreses.address_type','ec_locations.keyword','ec_locations.title_en','ec_addreses.address','ec_addreses.zip','ec_addreses.mobile as mobile2','ec_addreses.phone as phone2')
                          ->leftJoin('ec_addreses','ec_addreses.user_meta_name','=','users.meta_name')
                          ->leftJoin('ec_locations','ec_locations.id','=','ec_addreses.location_upazila_id') 
                          ->where('users.meta_name',$orderIns->user_meta_name)
                          ->get();
             
         }else if(@$orderIns->guest_id){
             $guestIns = EcGuest::select('ec_guests.*')
                          ->where('ec_guests.id',@$orderIns->guest_id)
                          ->get(); 
         }
         
         $subtotal = null;
         $vat =null;
         $shipping = null;
         $coupon = null;
         $employee_discount = 0;
         $gift_voucher_discount = 0;
		 $cod_total_discount = 0;
         $payable = null;
         
         if($getOrderPaymentD){
                        
                        foreach($getOrderPaymentD as $getOrderP){
                           
                            if($getOrderP['subtotal']){
                             
                              $subtotal = @$getOrderP['subtotal'];
                            }
                           
                            if($getOrderP['total_vat']) {
                              
                               $vat = $getOrderP['total_vat'];
                               }
                             
                             if($getOrderP['total_charge']){
                              
                               $shipping =@$getOrderP['total_charge'];
                                }
                             
                              if($getOrderP['total_coupon_amount']){
                             
                              
                               $coupon =$getOrderP['total_coupon_amount'];
                               }
							   
			     if($getOrderP['total_employee_discount_amount']){
                             
                              
                               $employee_discount =$getOrderP['total_employee_discount_amount'];
                               }
                               
                                if($getOrderP['total_gift_voucher_discount']){
                                  $gift_voucher_discount =$getOrderP['total_gift_voucher_discount'];
                                }
								
								 if($getOrderP['cod_total_discount']){
                                  $cod_total_discount =$getOrderP['cod_total_discount'];
                                }
                             
                              if($getOrderP['calculate_total']){
                               
                               $payable =$getOrderP['calculate_total'];
                               }
                            
                        }
              }
           
         
         
         
       $pdf = PDF::loadView('ecommerce.ecReport.invoice_pdf',compact('orderProduct','orderIns','getOrderPaymentD','invoiceIns','userAddress','guestIns','subtotal','vat','shipping','coupon','payable','employee_discount','gift_voucher_discount','cod_total_discount'));
       return $pdf->download('invoice.pdf');
       
     }

}

Create html view file of ecommerce.ecReport.invoice_pdf as create in laravel and view html report

call downInvoicePdf($id) method from where we want and enjoy pdf.

bONEandALL