Hello every body, Assalamualaikum Wr. Wb.
Nearly 1 month, i don't share anything in my blog. And this section, i will post about "Export Data to PDF with MPDF Extension". Ok, let's begin.
First, please open composer.json file and change with following this content.
Create a new file as main1.php in views/layouts and add this code.
Oke, next open your SiteController.php file and create a new action as Exportpdf like this code.
Well, now it's time to try it. Access site/exportpdf url on your application!
The result.
It's easy, isn't? Now you can implement this method to your application project. So good luck. Thank you. Wassalamualaikum Wr. Wb.
Nearly 1 month, i don't share anything in my blog. And this section, i will post about "Export Data to PDF with MPDF Extension". Ok, let's begin.
First, please open composer.json file and change with following this content.
others codeThen, please update your composer.json. A new vendor(mpdf) has instaled.
"require": {
"php": ">=5.4.0",
"yiisoft/yii2": ">=2.0.4",
"yiisoft/yii2-bootstrap": "*",
"yiisoft/yii2-swiftmailer": "*",
"himiklab/yii2-recaptcha-widget" : "*",
"abhi1693/yii2-sms": "2.0.0",
"robregonm/yii2-pdf": "*",
"mpdf/mpdf":"*",
},
others code
Create a new file as main1.php in views/layouts and add this code.
<?phpCreate a new file as template.php in views/site and add this code.
use yii\helpers\Html;
?>
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
<meta charset="<?= Yii::$app->charset ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<?= Html::csrfMetaTags() ?>
<title><?= Html::encode($this->title) ?></title>
<?php $this->head() ?>
</head>
<body>
<?php $this->beginBody() ?>
<div class="wrap">
<div class="container">
<?= $content ?>
</div>
</div>
<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>
<?php
use yii\helpers\Html;
use yii\grid\GridView;
?>
<style type="text/css">
table {
*border-collapse: collapse; /* IE7 and lower */
border-spacing: 0;
width: 100%;
}
.zebra td, .zebra th {
padding: 10px;
border-bottom: 1px solid #f2f2f2;
}
.zebra tbody tr:nth-child(even) {
background: #f5f5f5;
-webkit-box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
-moz-box-shadow:0 1px 0 rgba(255,255,255,.8) inset;
box-shadow: 0 1px 0 rgba(255,255,255,.8) inset;
}
.zebra th {
text-align: left;
text-shadow: 0 1px 0 rgba(255,255,255,.5);
border-bottom: 1px solid #ccc;
background-color: #eee;
background-image: -webkit-gradient(linear, left top, left bottom, from(#f5f5f5), to(#eee));
background-image: -webkit-linear-gradient(top, #f5f5f5, #eee);
background-image: -moz-linear-gradient(top, #f5f5f5, #eee);
background-image: -ms-linear-gradient(top, #f5f5f5, #eee);
background-image: -o-linear-gradient(top, #f5f5f5, #eee);
background-image: linear-gradient(top, #f5f5f5, #eee);
}
.zebra th:first-child {
-moz-border-radius: 6px 0 0 0;
-webkit-border-radius: 6px 0 0 0;
border-radius: 6px 0 0 0;
}
.zebra th:last-child {
-moz-border-radius: 0 6px 0 0;
-webkit-border-radius: 0 6px 0 0;
border-radius: 0 6px 0 0;
}
.zebra th:only-child{
-moz-border-radius: 6px 6px 0 0;
-webkit-border-radius: 6px 6px 0 0;
border-radius: 6px 6px 0 0;
}
.zebra tfoot td {
border-bottom: 0;
border-top: 1px solid #fff;
background-color: #f1f1f1;
}
.zebra tfoot td:first-child {
-moz-border-radius: 0 0 0 6px;
-webkit-border-radius: 0 0 0 6px;
border-radius: 0 0 0 6px;
}
.zebra tfoot td:last-child {
-moz-border-radius: 0 0 6px 0;
-webkit-border-radius: 0 0 6px 0;
border-radius: 0 0 6px 0;
}
.zebra tfoot td:only-child{
-moz-border-radius: 0 0 6px 6px;
-webkit-border-radius: 0 0 6px 6px
border-radius: 0 0 6px 6px
}
</style>
<table class="zebra" style="margin:auto; width:100%;">
<thead>
<tr>
<th>No</th>
<th>NIM</th>
<th>NAMA</th>
<th>JURUSAN</th>
<th>ANGKATAN</th>
<th>ALAMAT</th>
<th>FOTO</th>
</tr>
</thead>
<?php
$i=1;
foreach($model as $data){
echo '
<tr>
<td>'.$i.'</td>
<td>'.$data['nim'].'</td>
<td>'.$data['nama'].'</td>
<td>'.$data['jurusan'].'</td>
<td>'.$data['angkatan'].'</td>
<td>'.$data['alamat'].'</td>
<td><img src="'.Yii::$app->request->baseUrl.'/uploads/'.$data['foto'].'" width="100px"></td>
</tr>
';
$i++;
}
?>
</table>
Oke, next open your SiteController.php file and create a new action as Exportpdf like this code.
public function actionExportpdf() {And the last, add url ('/site/exportpdf') to your menu.
$this->layout='main1';
$model = Mahasiswa::find()->All();
$mpdf=new mPDF();
$mpdf->WriteHTML($this->renderPartial('template',['model'=>$model]));
$mpdf->Output('MyPDF.pdf', 'D');
exit;
}
Well, now it's time to try it. Access site/exportpdf url on your application!
The result.
It's easy, isn't? Now you can implement this method to your application project. So good luck. Thank you. Wassalamualaikum Wr. Wb.