如何从 Yii2 中的 activeform 传递到 PDF

How to pass from activeform in Yii2 to PDF

我使用 mpdf kartik 创建了表格和发票。我希望当用户插入表单并存储到数据库时,其表单中的数据值将传递给 invoice mpdf kartik。如何将值传递给 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>

假设您将模型的 ID 传递给 actionReport,使用此 ID 检索您需要的模型

public function actionReport($myId) 
{

   $my_model = MyModel::findOne($myid)

  ....

当你构建 $conte

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

pdf 使用了 $content ..

只需使用正确的

 $my_model in your view