How to print out a file pdf when after saved using dynamic form

[left]I’ve tried create dynamic form use extension wbraganca. I want user to input data in the form using extension wbraganca, after that the process print out a file pdf.

I’ve created variable for passing to display value in file pdf. And the result is success. But when I created for passing to display multiple values in file pdf and the result is failed.

My need is simple, I want multiple values in a file pdf displayed when user to input data in the form. And how to make for it? please tell me for that, I don’t know make like that.[/left]

in controllers :


<?php


namespace app\controllers;


use Yii;

use app\models\Voucher;

use app\models\DetilVoucher;

use app\models\AnakAsuh;

use app\models\Pengurus;

use app\models\User;

use app\models\Model;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

use yii\filters\AccessControl;

use kartik\mpdf\Pdf;


/**

 * PoController implements the CRUD actions for Po model.

 */

class VoucherController extends Controller

{

    /**

     * @inheritdoc

     */

    public function behaviors()

    {

        return [

            'access' => [

                'class' => AccessControl::className(),

                'only' => ['index'],

                'rules' => [

                    [

                        'actions' => ['index'],

                        'allow' => true,

                        'roles' => ['@'],

                    ],

                ],

            ],

            'verbs' => [

                'class' => VerbFilter::className(),

                'actions' => [

                    'delete' => ['POST'],

                ],

            ],

        ];

    }


    /**

     * Lists all Po models.

     * @return mixed

     */

    public function actionIndex()

    {

        return $this->redirect(['create']);

    }


    public function actionCreate()

    {

        $model = new Voucher();

        $modelsDetil = [new DetilVoucher];


        // Display datetime in form and save to table

        $model->tgl_voucher = date('Y-m-d');


        // Dispaly kode pengurus in form and save to table

        $model->kode_pengurus = \Yii::$app->user->identity->id;


        // Display value to view pdf 

        $this->view->params['tgl_voucher'] = date('d-m-Y');


        if ($model->load(Yii::$app->request->post()) && $model->save()) {


            $modelsDetil = Model::createMultiple(DetilVoucher::classname());

            Model::loadMultiple($modelsDetil, Yii::$app->request->post());


            // validate all models

            $valid = $model->validate();

            $valid = Model::validateMultiple($modelsDetil) && $valid;


            if ($valid) {

                $transaction = \Yii::$app->db->beginTransaction();

                try {

                    if ($flag = $model->save(false)) {

                        foreach ($modelsDetil as $modelsDetil) {

                            $modelsDetil->no_voucher_detil = $model->no_voucher;

                            if (! ($flag = $modelsDetil->save(false))) {

                                $transaction->rollBack();

                                break;

                            }

                        }

                    }

                    if ($flag) {

                        $transaction->commit();


                        // Your SQL query here

                        $content = $this->renderPartial('cetak', [

                            'model' => $model,

                            'modelsDetil' => $modelsDetil,

                        ]);


                        // 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('cetak', [

                            'model' => $model,

                            'modelsDetil' => $modelsDetil,

                        ]);

                    }

                } catch (Exception $e) {

                    $transaction->rollBack();

                }

            }

        } else {

            return $this->render('create', [

                'model' => $model,

                'modelsDetil' => (empty($modelsDetil)) ? [new DetilVoucher] : $modelsDetil

            ]);

        }

    }

}

in views/voucher/create.php (form)


<?php


use yii\helpers\Html;

use yii\bootstrap\ActiveForm;

use wbraganca\dynamicform\DynamicFormWidget;

use app\models\Voucher;

use app\models\DetilVoucher;

use app\models\User;

use app\models\Coba;

use yii\helpers\Url;

use yii\db\ActiveRecord;

use yii\helpers\ArrayHelper;

use kartik\widgets\Select2;

use kartik\widgets\DatePicker;


/* @var $this yii\web\View */

/* @var $model app\models\Po */

/* @var $form yii\widgets\ActiveForm */


$this->title = 'Cetak Voucher Pengeluaran Kas';

?>


<h1 align="center">Cetak Voucher Pengeluaran Kas</h1>

<?php

echo "&nbsp";

echo "&nbsp";

?>


<div class="voucher-form">


    <?php $form = ActiveForm::begin([

        'id' => 'dynamic-form',

        'layout' => 'horizontal', 

        'fieldConfig' => [

            'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",

                'horizontalCssClasses' => [

                'label' => 'col-sm-4',

                'offset' => 'col-sm-offset-4',

                'wrapper' => 'col-sm-8',

                'error' => '',

                'hint' => '',

                'button' => 'col-sm-4'

            ],

        ],

    ]); 

    ?>


    <div class="panel panel-default"> 


    <div class="panel-heading"><b><h4 align="center">Form Cetak Voucher Pengeluaran Kas</h4></b></div>




    <?php if (Yii::$app->session->hasFlash('success')): ?>

      <div class="alert alert-success alert-dismissable">

      <button aria-hidden="true" data-dismiss="alert" class="close" type="button">×</button>

      <h4><i class="icon fa fa-check"></i>Informasi!</h4>

      <?= Yii::$app->session->getFlash('success') ?>

    </div>

    <?php endif; ?>


    <div class="panel-body">  


    <table class="table table-striped"> 


    <?= $form->field($model, 'no_voucher')->textInput(['maxlength' => true, 'style' => 'width:100px', 'readOnly' => true]) 

    ?>


    <?= $form->field($model, 'tgl_voucher')->widget(DatePicker::classname(), [

        'options' => [

            'id' => 'tgl_voucher',

            'style' => 'width:350px',

            'disabled' => true

        ],

        'type' => DatePicker::TYPE_INPUT,

        'language' => 'id',

        'pluginOptions' => [

            'autoclose'=>true,

            'format' => 'yyyy-mm-dd',

            ]

        ]); 

    ?>


    <?php $kode_pengurus = \Yii::$app->user->identity->kode_pengurus ?>

    <?= $form->field($model, 'kode_pengurus')->textInput(['maxlength' => true, 'style' => 'width:100px', 'value' => $kode_pengurus, 'readOnly' => true]) 

    ?>


    <?php $nama_pengurus = \Yii::$app->user->identity->nama_pengurus ?>

    <?= $form->field($model, 'nama_pengurus')->textInput(['maxlength' => true, 'style' => 'width:350px', 'value' => $nama_pengurus, 'readOnly' => true]) 

    ?>


    <div class="row">


        <div class="panel-body">

            <?php DynamicFormWidget::begin([

                'widgetContainer' => 'dynamicform_wrapper', // required: only alphanumeric characters plus "_" [A-Za-z0-9_]

                'widgetBody' => '.container-items', // required: css class selector

                'widgetItem' => '.item', // required: css class

                'limit' => 10, // the maximum times, an element can be cloned (default 999)

                'min' => 1, // 0 or 1 (default 1)

                'insertButton' => '.add-item', // css class

                'deleteButton' => '.remove-item', // css class

                'model' => $modelsDetil[0],

                'formId' => 'dynamic-form',

                'formFields' => [

                    'kode_anak_asuh',

                    'kategori',

                    'keperluan',

                    'jumlah',

                    'ref_kwitansi',

                    'tgl_bayar',

                ],

            ]); 

            ?>


            <div class="container-items"><!-- widgetContainer -->

            <?php foreach ($modelsDetil as $i => $modelsDetil): ?>

                <div class="item panel panel-default"><!-- widgetBody -->

                    <div class="panel-heading">

                        <h3 class="panel-title pull-left">Detil Voucher</h3>

                        <div class="pull-right">

                            <button type="button" class="add-item btn btn-success btn-xs"><i class="glyphicon glyphicon-plus"></i></button>

                            <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>

                        </div>

                        <div class="clearfix"></div>

                    </div>

                    <div class="panel-body">

                        <?php

                            // necessary for update action.

                            if (! $modelsDetil->isNewRecord) {

                                echo Html::activeHiddenInput($modelsDetil, "[{$i}]id");

                            }

                        ?>

                                <?php $data = ArrayHelper::map(Coba::find()->all(), 'kode_anak_asuh', 'nama_calon');

                                ?>

                                <?php 

                                    echo $form->field($modelsDetil, "[{$i}]kode_anak_asuh")->dropDownList(

                                $data, 

                                ['prompt'=>'Pilih', 'style' => 'width:350px']); ?> 


                                <?= $form->field($modelsDetil, "[{$i}]kategori")->dropDownList(['Pendidikan' => 'Pendidikan', 'Kesehatan' => 'Kesahatan', 'Lain-lain' => 'Lain-lain'],

                                    ['prompt'=>'Pilih', 'style' => 'width:350px']) 

                                ?>


                                <?= $form->field($modelsDetil, "[{$i}]keperluan")->textInput(['maxlength' => true, 'style' => 'width:350px'])

                                ?>


                                <?= $form->field($modelsDetil, "[{$i}]jumlah")->textInput(['maxlength' => true, 'style' => 'width:350px'])

                                ?>


                                <?= $form->field($modelsDetil, "[{$i}]ref_kwitansi")->textInput(['maxlength' => true, 'style' => 'width:350px'])

                                ?>


                                <?= $form->field($modelsDetil, "[{$i}]tgl_bayar")->widget(\yii\jui\DatePicker::classname(), [

                                            'language' => 'id',

                                            'dateFormat' => "yyyy-MM-dd",

                                            'options' => [

                                                'class' => 'form-control picker',

                                                'style' => 'width:350px',

                                            ], 

                                            'clientOptions' => [

                                                'changeMonth' => true,

                                                'yearRange' => '1996:2099',

                                                'changeYear' => true,

                                            ],

                                    ]) 

                                ?>


                        </div><!-- .row -->

                    </div>

                </div>

            <?php endforeach; ?>

            </div>

            <?php DynamicFormWidget::end(); ?>


    </div>

    </div>


    </table>


    </div>


    </div> <!-- /panel -->


    </div> <!-- /container -->


    <div class="form-group">

    <div class="col-sm-offset-4">

        <?= Html::submitButton('Simpan', ['class' => 'btn btn-primary']) ?>

        <?php

        echo "&nbsp";

        echo "&nbsp";

        echo Html::a('Keluar', ['/'], ['class' => 'btn btn-success']);

        ?> 

        </div>

    </div>  


    <?php ActiveForm::end(); ?>


</div>


<?php

$this->registerJs(' 

$(function () {

    $(".dynamicform_wrapper").on("afterInsert", function(e, item) {

         $( ".picker" ).each(function() {

            $( this ).datepicker({

            dateFormat : "yy-mm-dd",

            language : "id",

            changeMonth: true,

            changeYear: true

          });

        });

    });

});

');

?>

in views/voucher/cetak.php (pdf)


<?php


use yii\helpers\Html;

use yii\bootstrap\ActiveForm;

use yii\widgets\Pjax;

use app\models\Voucher;

use app\models\DetilVoucher;

use app\models\Pengurus;

use app\models\User;

use app\models\Calon;


/* @var $this yii\web\View */

/* @var $model app\models\Donatur */

?>

<?php $form = ActiveForm::begin(['layout' => 'horizontal', 'enableAjaxValidation' => false,

    'id' => 'cetak',

    ]); ?>

<html>

<body style="color:#000066;">

<div id="apDiv1">No : <?php echo $model->no_voucher; ?> <br> 

Tanggal : <?php echo $this->params['tgl_voucher']; ?></div>

<div id="apDiv2">

  <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></p>


</div>

<br>


<p style="text-align: center;"><B>VOUCHER PENGELUARAN KAS<BR>(SPMU)</B></p>


<br>

Dibayar Kepada:

    <?php $model = \Yii::$app->user->identity->nama_pengurus ?>

    <?php echo $nama = $model; ?>

<br>

<table border="1" width="100%" style="color=#000066" class="a">

    <thead>

<tr>

    <th>No</th>

<th  style='text-align: center; width: 25%;'>Nama Anak</th>

<th  style='text-align: center; width: 25%;'>Kategori</th>

<th  style='text-align: center; width: 25%;'>Keperluan </th>

<!--th>Ref. Kwitansi</th>

<th>Tgl Bayar</th-->

<th>Jumlah</th>

</tr>

</thead>

<tbody>

<?php

    // Get nama donatur

    $POST_VARIABLE = Yii::$app->request->post('Calon');

    $nama = $POST_VARIABLE['nama_calon']; 

    // Get alamat donatur

    $request2 = $POST_VARIABLE['alamat']; 

?>

<?php

if( ! empty($modelsDetil)){

    $no=0;

    foreach($modelsDetil as $data){ $no++;

        echo "<tr>";

        echo "<td>$no</td>";

        echo "<td  style='text-align: center; width: 25%;'>".$data = $nama."</td>";

        echo "<td  style='text-align: center; width: 25%;'>".$data = $nama."</td>";

        echo "<td  style='text-align: center; width: 25%;'>".$data = $nama."</td>";

        //echo "<td>". str_replace(",",".",number_format($data->jumlah)) ."</td>";  

        echo "</tr>";

    }

    echo "<tr>";

    echo "<td colspan='3'></td>";

    echo "<td><b>Total </b></td>";

    //echo "<td>". str_replace(",",".",number_format($totali[0]->jumlah)) ."</td>";

    echo "</tr>";


}

?>

</tbody>

</table>

<br>

<table style="height: 67px;" width="100%" border=0 >

<tbody>

<tr>

<td width="40%"> 

<p style="text-align: center;"><br></p>

<br>

<br>

<br>

<br>

<p style="text-align: center;"><span style="text-decoration: underline;"><b>Hj.Elly Bachtar</b></span><br>Ketua</p>

</td>

<td width="40%">

<p style="text-align: center;">Diketahui Oleh: </p>

<br>

<br>

<br>

<br>

<p style="text-align: center;"><span style="text-decoration: underline;"><b>Hj.Sadariah Sudarsono</b></span><br>Bendahara</p>

</td>

<td  style="text-align: right; width: 50%;">

<p>Yang Menerima</p>

<br>

<br>

<br>

<br>

<p><span style="text-decoration: underline;"><br><strong>

    <?php $model = \Yii::$app->user->identity->nama_pengurus ?>

    <?php echo $nama = $model; ?></strong></span></p>

</td>

</tr>

</tbody>

</table>

</body>

</html>

<?php ActiveForm::end();?>

7226

voucher pdf.JPG