Dierig Patrick
Dierig Patrick

Reputation: 186

Wrong View create PDF laravel

On a button click I want a PDF. But I dont get a PDF I just get the data from the database.

My code is:

<?php

namespace MVS\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use PDF;
use MVS\Kunden;

class DynamicPDFController extends Controller
{
    function index()
    {
     $customer_data = $this->get_customer_data();
     $finance_data = $this->get_finance_data();
     return view('dynamic_pdf')->with('customer_data', $customer_data);
    }

    function get_customer_data($id)
    {
     $customer_data = Kunden::find($id);
     return $customer_data;
    }

    function pdf()
    {
     $pdf = \App::make('dompdf.wrapper');
     $pdf->loadHTML($this->convert_customer_data_to_html());
     return $pdf->stream();
    }

    function convert_customer_data_to_html()
    {
     $customer_data = $this->get_customer_data();
     $output = '
     <h3 align="center">Angebot</h3>
     <table width="100%" style="border-collapse: collapse; border: 0px;">
      <tr>
    <th style="border: 1px solid; padding:12px;" width="20%">Vorname</th>
    <th style="border: 1px solid; padding:12px;" width="30%">Nachname</th>
    <th style="border: 1px solid; padding:12px;" width="15%">Stadt</th>
    <th style="border: 1px solid; padding:12px;" width="15%">PLZ</th>
   </tr>
     ';  
     foreach($customer_data as $kunden)
     {
      $output .= '
      <tr>
       <td style="border: 1px solid; padding:12px;">'.$kunden->vorname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->nachname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->wohnort.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->plz.'</td>
      </tr>
      ';
     }
     $output .= '</table>';
     return $output;
    }
}

I just get the following message output:

{"id":1,"created_at":"2018-10-15 06:56:15","updated_at":"2018-10-15 06:56:15","user_id":2,"vorname":"Patrick","nachname":"Marks","strasse":"teststraße 9","plz":47906,"wohnort":"Krefeld","mail":"[email protected]","telefon":21514462,"geburtsdatum":"2013-10-30"}

The route is the following:

Route::get('/admin/kunden/pdf/{id}', 'DynamicPDFController@get_customer_data');

When i use this code in the controller:

function index()
    {
     $customer_data = $this->get_customer_data();
     return view('dynamic_pdf')->with('customer_data', $customer_data);
    }

    function get_customer_data()
    {
     $customer_data = DB::table('kundens')
         ->limit(10)
         ->get();
     return $customer_data;
    }

I will get a PDF with ALL customers in the table. But i only want from a specific User the data...

Upvotes: 0

Views: 155

Answers (2)

Patrick
Patrick

Reputation: 821

If you are posting a GET method onto

Route::get('/admin/kunden/pdf/{id}', 'DynamicPDFController@get_customer_data');

Then you will hit function get_customer_data(){} in your DynamicPDFController

This hits onto this code:

$customer_data = DB::table('kundens')
         ->limit(10)
         ->get();
return $customer_data;

So you would only get the customers data.

Try changing this:

Route::get('/admin/kunden/pdf/{id}', 'DynamicPDFController@get_customer_data');

To this:

Route::get('/admin/kunden/pdf/{id}', 'DynamicPDFController@index');

Now try chaning your get_customer_data to this:

function get_customer_data()
    {
     $customer_data = DB::table('kundens')->first();
     return $customer_data;
 }

This should give you only one customer. Now we have to make it dynamic, so you can pass an ID or similar to the function:

function get_customer_data($id)
    {
     $customer_data = DB::table('kundens')->whereId($id)->first();
     return $customer_data;
    }

Remember to change your index function:

function index()
    {
     $id = 1;
     $customer_data = $this->get_customer_data($id);
     return view('dynamic_pdf')->with('customer_data', $customer_data);
    }

Now edit your index accordingly, so you can pass the ID to the index function for a dynamic setup.

Upvotes: 1

Murad Shukurlu
Murad Shukurlu

Reputation: 457

Because your codes calling users information check this route :

Route::get('/admin/kunden/pdf/{id}', 'DynamicPDFController@get_customer_data');

in this route you are calling get_customer_data() function in DynamicPDFController class .

if we look at there :

$customer_data = Kunden::find($id);
     return $customer_data;

those codes response the json .

You can replace the functions like that :

function get_customer_data($id)
    {
     $customer_data = Kunden::find($id);


     return $this->pdf($customer_data);
    }

    function pdf($customer_data)
    {
     $pdf = \App::make('dompdf.wrapper');
     $pdf->loadHTML($this->convert_customer_data_to_html($customer_data));
     return $pdf->stream();
    }

    function convert_customer_data_to_html($customer_data)
    {

     $output = '
     <h3 align="center">Angebot</h3>
     <table width="100%" style="border-collapse: collapse; border: 0px;">
      <tr>
    <th style="border: 1px solid; padding:12px;" width="20%">Vorname</th>
    <th style="border: 1px solid; padding:12px;" width="30%">Nachname</th>
    <th style="border: 1px solid; padding:12px;" width="15%">Stadt</th>
    <th style="border: 1px solid; padding:12px;" width="15%">PLZ</th>
   </tr>
     ';  
     foreach($customer_data as $kunden)
     {
      $output .= '
      <tr>
       <td style="border: 1px solid; padding:12px;">'.$kunden->vorname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->nachname.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->wohnort.'</td>
       <td style="border: 1px solid; padding:12px;">'.$kunden->plz.'</td>
      </tr>
      ';
     }
     $output .= '</table>';
     return $output;
    }

Upvotes: 0

Related Questions