错误的视图创建PDF laravel

时间:2018-10-23 09:44:37

标签: php laravel laravel-5

在按钮上单击我想要PDF。但是我没有PDF,我只是从数据库中获取数据。

我的代码是:

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

我只得到以下消息输出:

  

{“ 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ße9”,“ plz”:47906,“ wohnort”:“ Krefeld”,“ mail”:“ p-dierig @ sdsd”。 de“,” telefon“:21514462,” geburtsdatum“:” 2013-10-30“}

路线如下:

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

当我在控制器中使用此代码时:

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

我将在表中获取所有客户的PDF。但是我只想要特定用户的数据...

2 个答案:

答案 0 :(得分:1)

如果要将GET方法发布到

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

然后,您将在function get_customer_data(){}中打DynamicPDFController

这会打上这段代码:

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

所以您只会获取客户数据。

尝试更改此内容:

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

对此:

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

现在尝试将您的get_customer_data更改为此:

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

这应该只给您一个客户。 现在我们必须使它动态化,以便您可以传递一个ID或类似的函数:

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

记住要更改索引功能:

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

现在相应地编辑索引,以便将ID传递给索引功能以进行动态设置。

答案 1 :(得分:0)

由于您的代码调用了用户信息,因此请检查此路线:

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

在此路由中,您将在DynamicPDFController类中调用get_customer_data()函数。

如果我们看那里:

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

这些代码响应json。

您可以替换类似的功能:

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