2016-12-06 76 views
0

我已經使用mpdf kartik創建了表單和發票。我想當用戶插入表單並存儲到數據庫時,它的表單中的數據值將被傳遞給發票mpdf kartik。但我不知道如何將價值傳遞給pdf,請指教我。如何從yii2中的活動表單傳遞到pdf

控制器:

<?php 

namespace app\controllers; 

use Yii; 
use app\models\TandaTerima; 
use yii\web\Controller; 
use yii\web\NotFoundHttpException; 
use yii\filters\VerbFilter; 
use yii\filters\AccessControl; 
use app\models\Donatur; 
use app\models\Pengurus; 
use app\models\User; 
use yii\helpers\Json; 
use kartik\mpdf\Pdf; 

class TandaTerimaController extends Controller 
{ 
    public function behaviors() 
    { 
     return [ 
      'access' => [ 
       'class' => AccessControl::className(), 
       'only' => ['create'], 
       'rules' => [ 
        [ 
         'actions' => ['create'], 
         'allow' => true, 
         'roles' => ['@'], 
        ], 
       ], 
      ], 
      'verbs' => [ 
       'class' => VerbFilter::className(), 
       'actions' => [ 
        'delete' => ['POST'], 
       ], 
      ], 
     ]; 
    } 

    public function actionReport() 
    { 
     $tandaTerima = new TandaTerima(); 
     if (!$tandaTerima) { 
      throw new NotFoundHttpException("The tanda terima was not found."); 
     } 

     $tandaTerima->tgl_terima = date('Y-m-d'); 

     $donatur = new Donatur(); 
     if (!$donatur) { 
      throw new NotFoundHttpException("The donatur has tanda terima."); 
     } 

     $user = new User(); 
     if (!$user) { 
      throw new NotFoundHttpException("The donatur has tanda terima."); 
     } 

     // Your SQL query here 
     $content = $this->renderPartial('coba', [ 
      'tandaTerima' => $tandaTerima, 
      'donatur' => $donatur, 
     ]); 

     // setup kartik\mpdf\Pdf component 
     $pdf = new Pdf([ 
     // set to use core fonts only 
     'mode' => Pdf::MODE_CORE, 
     // 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, 
     // format content from your own css file if needed or use the 
     // enhanced bootstrap css built by Krajee for mPDF formatting 
     'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
     // any css to be embedded if required 
     'cssInline' => '.kv-heading-1{font-size:18px}', 
     // set mPDF properties on the fly 
     'options' => ['title' => 'Surat Tanda Terima'], 
     // call mPDF methods on the fly 
     'methods' => [ 
     // 'SetHeader'=>['Surat Tanda Terima'], 
      'SetFooter'=>['{PAGENO}'], 
      ] 
     ]); 

     /*------------------------------------*/ 
     Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; 
     $headers = Yii::$app->response->headers; 
     $headers->add('Content-Type', 'application/pdf'); 
     /*------------------------------------*/ 

     // return the pdf output as per the destination setting 
     return $pdf->render('coba', [ 
      'tandaTerima' => $tandaTerima, 
      'donatur' => $donatur, 
     ]); 
    } 

    public function actionIndex() 
    { 
     return $this->redirect(['create']); 
    } 


    public function actionGetDonatur($kodeId) 
    { 
     $donatur = Donatur::findOne($kodeId); 
     echo Json::encode($donatur); 
    } 

    public function actionCreate() 
    { 
     $tandaTerima = new TandaTerima(); 
     if (!$tandaTerima) { 
      throw new NotFoundHttpException("The tanda terima was not found."); 
     } 

     $donatur = new Donatur(); 
     if (!$donatur) { 
      throw new NotFoundHttpException("The donatur has tanda terima."); 
     } 

     $user = new User(); 
     if (!$user) { 
      throw new NotFoundHttpException("The donatur has tanda terima."); 
     } 

     // Display datetime in form and save to table 
     $tandaTerima->tgl_terima = date('Y-m-d'); 

     // Dispaly kode pengurus in form and save to table 
     $tandaTerima->kode_pengurus = \Yii::$app->user->identity->id; 

     if ($tandaTerima->load(Yii::$app->request->post()) && $tandaTerima->save()) { 
       Yii::$app->session->setFlash('success', 'Data berhasil disimpan!'); 
       return $this->redirect(['report']); 
       return $this->refresh(); 
     } 

     return $this->render('create', [ 
      'tandaTerima' => $tandaTerima, 
      'donatur' => $donatur, 
     ]); 

    } 
} 

查看PDF:

<html> 
<head> 
<style type="text/css"> 
<!-- 
#apDiv1 { 
    position:absolute; 
    left:136px; 
    top:16px; 
    width:431px; 
    height:116px; 
    z-index:1; 
} 
#apDiv2 { 
    position:absolute; 
    left:635px; 
    top:180px; 
    width:63px; 
    height:32px; 
    z-index:2; 
} 
.style3 { 
    font-size: 20px; 
    font-weight: bold; 
} 
.style9 {font-size: 15px} 
.style10 {font-size: 15px} 
#apDiv3 { 
    position:absolute; 
    left:607px; 
    top:205px; 
    width:86px; 
    height:35px; 
    z-index:2; 
} 
#apDiv4 { 
    position:absolute; 
    left:78px; 
    top:163px; 
    width:534px; 
    height:111px; 
    z-index:3; 
} 
#apDiv5 { 
    position:absolute; 
    left:527px; 
    top:204px; 
    width:73px; 
    height:57px; 
    z-index:4; 
} 
--> 
</style> 
</head> 
<body style="color:#000066;"> 
<div id="apDiv1"> 
<p align="center"><span class="style9">PANTI ASUHAN DAN SANTUNAN KELUARGA </span><BR> 
    <span class="style3">MUHAMMADIYAH DAN AISYIYAH </span><BR> 
    <span class="style10">CABANG RAWAMANGUN PULOGADUNG</span><br> 
    <span>Jalan Rukem II/3 Rawamangun Jakarta 13220 Telepon 471 829<br> 
    BRI KCP Sunan Giri Rek. Britama No.0530.01.002755.50.9 
    <span style="text-decoration: underline;"></span></p> 
</div> 
<br> 
<hr style="color:#000066;"> 
<p align="center"><strong>TANDA&nbsp;TERIMA</strong></p> 

<p><strong><em>&nbsp;</em></strong></p> 
<table style="height: 116px;" width="271"> 
<tbody> 
<tr> 
<td>Sudah&nbsp;Terima&nbsp;dari :</td> 
<td></td> 
</tr> 
<tr> 
<td>Alamat :</td> 
<td></td> 
</tr> 
<tr> 
<td>Banyaknya&nbsp;Uang&nbsp; :</td> 
<td></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td></td> 
</tr> 
<tr> 
<td>Untuk&nbsp;Pembayaran :</td> 
<td></td> 
</tr> 
<tr> 
<td>&nbsp;</td> 
<td></td> 
</tr> 
</tbody> 
</table> 
<p>Selanjutnya akan kami salurkan kepada mereka yang berhak menerimanya.</p> 
<p>Dengan nama Allah yang Maha pengasih lagi maha penyayang semoga Allah SWT. Memberi pahala kepada anda, atas barang yang anda berikan dan mudah-mudahan Allah SWT. Memberi berkat kepada anda atas apa saja yang masih tinggal. Muda-mudahan dijadikanNya kesucian bagi anda.</p> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
<table> 
<tbody> 
<tr> 
<td width="197"><p></p> 
<p>Yang&nbsp;Menyerahkan,</p> 
</td> 
<td width="197"> 
<p>&nbsp;</p> 
</td> 
<td width="197"> 
    <p style="text-align: right;">Jakarta, <?php 
     $tandaTerima = new TandaTerima(); 
     $tandaTerima->tgl_terima = date('Y-m-d'); 
    ?></p> 
<p>Yang&nbsp;menerima,</p> 
</td> 
</tr> 
<tr> 
<td width="197"> 
    <br><br><br> 
    </td> 
<td width="197">&nbsp;</td> 
<td width="197"> 
    <br><br><br> 
    </td> 
</tr> 
</tbody> 
</table> 
<p>&nbsp;</p> 
<p>&nbsp;</p> 
</body> 
</html> 

This invoice in pdf..

回答

0

假設你傳遞給actionReport模型的ID,用這個ID你找回你所需要的模型

public function actionReport($myId) 
{ 

    $my_model = MyModel::findOne($myid) 

    .... 

當你建立了$康特

$content = $this->renderPartial('your_partial_vire', [ 
     'my_model' => $my_model, 
     ..... 
    ]); 

的$內容所使用的PDF ..

簡單地使用適當的

$my_model in your view 
+0

我做過嘗試,但結果是「缺少必要的參數:身份識別碼」。 – CrashBurn

+0

當你調用actionReport你傳遞一個正確的ID? ..告訴我你如何調用行動.. – scaisEdge

+0

我使用return $ this-> redirect(['report'])調用動作。 in actionCreate .. – CrashBurn

0

我在我的控制器試圖從scaisEdge代碼,但結果是

缺少所需的參數:myId

而且在我的控制器這個新代碼:

public function actionReport($myId) 
    { 
     $my_model = TandaTerima::findOne($myid); 

     // Your SQL query here 
     $content = $this->renderPartial('coba', [ 
      'my_model' => $my_model, 
     ]); 

     // setup kartik\mpdf\Pdf component 
     $pdf = new Pdf([ 
     // set to use core fonts only 
     'mode' => Pdf::MODE_CORE, 
     // 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, 
     // format content from your own css file if needed or use the 
     // enhanced bootstrap css built by Krajee for mPDF formatting 
     'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css', 
     // any css to be embedded if required 
     'cssInline' => '.kv-heading-1{font-size:18px}', 
     // set mPDF properties on the fly 
     'options' => ['title' => 'Surat Tanda Terima'], 
     // call mPDF methods on the fly 
     'methods' => [ 
     // 'SetHeader'=>['Surat Tanda Terima'], 
      'SetFooter'=>['{PAGENO}'], 
      ] 
     ]); 

     /*------------------------------------*/ 
     Yii::$app->response->format = \yii\web\Response::FORMAT_RAW; 
     $headers = Yii::$app->response->headers; 
     $headers->add('Content-Type', 'application/pdf'); 
     /*------------------------------------*/ 

     // return the pdf output as per the destination setting 
     return $pdf->render('coba', [ 
      'tandaTerima' => $tandaTerima, 
      'donatur' => $donatur, 
     ]); 
    } 
相關問題