mumunotes

  • Home
  • Contact Us

Thursday, 5 November 2015

Export Data to PDF with MPDF Extension

 mumunotesss     10:12     Yii, Yii2     No comments   

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.
others code
"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
Then, please update your composer.json. A new vendor(mpdf) has instaled.
Create a new file as main1.php in views/layouts and add this code.
<?php
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() ?>
Create a new file as template.php in views/site and add this code.
<?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() {
$this->layout='main1';
$model = Mahasiswa::find()->All();
$mpdf=new mPDF();
$mpdf->WriteHTML($this->renderPartial('template',['model'=>$model]));
$mpdf->Output('MyPDF.pdf', 'D');
exit;
}
And the last, add url ('/site/exportpdf') to your menu.
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.


Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Newer Posts Older Posts Home

About Me

mumunotesss
View my complete profile

Popular Posts

  • Change password with validation and repeat password on Yii2
    Hello everybody. I could not post an article because I didn't have new knowledge to share. Ok, this day I will share about "...
  • Import Data from CSV File to Database Without Extension in Yii2
    Hello everybody, Assalamualaikum Wr. Wb. Nearly 1 month I don't publish article. Now, I publish an article about " Import Data fro...
  • Membuat login Yii2 dengan Database
    Selamat pagi guys. Sorry beberapa hari kamaren sibuk. Jadi belum sempet update blog lagi. Mumpung kali ini sempet, hari ini mau berbagi te...
  • Membuat fungsi CRUD dengan Gii Yii2
    Selamat pagi guys. Setelah kemaren kita membuat login dari database, hari ini kita akan membuat CRUD (create, read, update, delete) dengan ...

Blog Archive

  • ▼  2015 (16)
    • ▼  November (1)
      • Export Data to PDF with MPDF Extension
    • ►  September (1)
    • ►  August (1)
    • ►  June (8)
    • ►  May (5)

Translate

Copyright © mumunotes
Distributed By My Blogger Themes | Blogger Theme By NewBloggerThemes