2016-04-08 5 views
1

Ich brauche pdf aus meiner Sicht zu erzeugen, Im mit kartik mPDF,Yii2: Wie Modell und Modell-ID in mPDF hinzufügen

Controller:

public function actionInvoicesPrint() 
{ 
    $pdf = new Pdf([ 
     'mode' => Pdf::MODE_CORE, // leaner size using standard fonts 
     'content' => $this->renderPartial('view', ['model' => $model, 'mode' => Pdf::MODE_CORE, 
      'format'=> Pdf::FORMAT_A4, 
      'orientation'=> Pdf::ORIENT_POTRAIT, 
      'destination'=> Pdf::DEST_BROWSER]), 
    ]); 
    return $pdf->render(); 
} 

Fehler Erste Undefined variable: model. Ich habe versucht, wie oben gezeigt, aber immer den gleichen Fehler.

Ausblick:

public function actionView($id) 
    { 
     $searchModel = new InvoicesSearch(); 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
     $data   = Invoices::findOne($id); 
     return $this->render('view', [ 
      'model'  => $this->findModel($id), 
      'dataProvider' => $dataProvider, 
      'searchModel' => $searchModel, 
      'data'   => $data, 
     ]); 
    } 

Antwort

1

In Ihrem actionInvocePrint Sie nicht ein $ Modell

unten zugewiesen wird eine Probe eines meiner Arbeits pdf (kartik mPDF für Ihre)

$model = $this->findModel($id); 

    // get your HTML raw content without any layouts or scripts 
    //$this->layout = 'pdfmain'; 

    $content = $this->renderPartial('_mpdf_report_scheda', [ 
      'model' => $model, 
      'imglink' => $imglink, 
      ]); 


    if ($print) { 
     $setJS = 'this.print();'; 
    } 
    else { 
     $setJS =''; 
    } 
    // setup kartik\mpdf\Pdf component 
    $pdf = new Pdf([ 
    // set to use core fonts only 
    'mode' => Pdf::MODE_BLANK, 
    // A4 paper format 
    'format' => Pdf::FORMAT_A4, 
    // portrait orientation 
    'orientation' => Pdf::ORIENT_PORTRAIT, 
    // stream to browser inline 
    'destination' => Pdf::DEST_BROWSER, 
    // your html content input 
    'content' => $content, 

Wie Sie in diesem Teil des Codes sehen können. Das Modell wird vor allem erhalten und dann mit diesem $ Modell ist ein renderPartial mit der richtigen Ansicht definiert.

für die $ id vorbei Aktion sollte

public function actionInvoicesPrint($id) 

sein und dies für Sie sollten URL-Aufruf

Url::to(['/your-controller/Invoices-print' , 'id' => $your_id]); 
+0

kann ich das in meinem 'actionView()' verwenden, ohne 'actionInvoicesPrint() 'zu erstellen? – JKLM

+0

wie diese Vorschläge bereits von Ihnen in ähnlicher Frage erwähnt .. Ich konnte Modell 'id' in' actionInvoicesPrint() 'nicht zuweisen wegen dieser Fehler bekommen' Undefinierte Variable: ID', wie ID für 'actionInvoicesPrint zuweisen() ' – JKLM

+0

Ich habe die Antwort aktualisieren – scaisEdge

1

komplette Arbeitslösung

public function actionInvoicesPrint($id) 
    { 
     $model = $this->findModel($id); 
     $searchModel = new InvoicesSearch(); 
     $dataProvider = $searchModel->search(Yii::$app->request->queryParams); 
     $data   = Invoices::findOne($id); 
     $content = $this->renderPartial('view', ['model' => $model,'dataProvider' => $dataProvider,'searchModel' => $searchModel,'data'=> $data]); 
     $pdf  = new Pdf([ 
      // set to use core fonts only 
      'mode'  => Pdf::MODE_BLANK, 
      // A4 paper format 
      'format'  => Pdf::FORMAT_A4, 
      // portrait orientation 
      'orientation' => Pdf::ORIENT_PORTRAIT, 
      // stream to browser inline 
      'destination' => Pdf::DEST_BROWSER, 
      // your html content input 
      'content'  => $content, 
     ]); 
     return $pdf->render(); 
    } 

in Sicht sein auslösen Controller actionInvoicesPrint()

<?php 
             echo Html::a('<i class="fa glyphicon glyphicon-hand-up"></i> Privacy Statement', ['/invoices/invoices-print', 'id' => $model->id], [ 
    'class'=>'btn btn-danger', 
    'target'=>'_blank', 
    'data-toggle'=>'tooltip', 
    'title'=>'Will open the generated PDF file in a new window' 
]); 
?>