1. 云栖社区>
  2. PHP教程>
  3. 正文

How to use mPDF In Yiiframework 2

作者:用户 来源:互联网 时间:2017-12-01 10:04:59

How to use mPDF In Yiiframework 2 - 摘要: 本文讲的是How to use mPDF In Yiiframework 2, Hello there, Today I would like to show you how to integrate mPDF library with Yii framework 2.First let me give you so

Hello there, Today I would like to show you how to integrate mPDF library with Yii framework 2.First let me give you some basic information about mPDF. mPDF is a PHP Class by using this we can easily generate PDF files from HTML templates so you can style your HTML template as you wish. Here is the simple steps to use mPDF with YII2 framework.

You can install in tow way – using composer or you can install manually.

Install mPDF Using Composer

Require this package in your composer.json by adding "mpdf/mpdf":"*" or you can directly run php composer.phar require mpdf/mpdf "dev-master" or composer require mpdf/mpdf "dev-master" it will update your composer.json and downloads mPDF library.

"require":{"php":">=5.4.0","yiisoft/yii2":"*","yiisoft/yii2-bootstrap":"*","yiisoft/yii2-swiftmailer":"*","mpdf/mpdf":"*"},

Next, update Composer from the Terminal

$composerupdate Install mPDF Manually

Download mPDF from : http://www.mpdf1.com/mpdf/index.php and Extract it to your vendor/ folder of your yii2 application. Now rename and change the file path as mpdf/mpdf/ and See the below image for file structure.

How to use mPDF In Yiiframework 2

Now go to vendor/composer/autoload_namespaces.php file and add the below line to array

'mPDF'=>array($vendorDir.'/mpdf/mpdf'), How to use – Create PDF Using mPDF

Below is simple example – you just need to import mPDF by using use mPDF; and the crate object for mPDF. using generated object you call all of mPDF methods.

<?phpnamespaceapp/controllers;usemPDF;classMpdfControllerextends/yii/web/Controller{ // show in browser publicfunctionactionIndex(){$mpdf=newmPDF();$mpdf->WriteHTML($this->renderPartial('mpdf_view'));$mpdf->Output();exit;}// download publicfunctionactionForceDownloadPdf(){$mpdf=newmPDF();$mpdf->WriteHTML($this->renderPartial('mpdf_view'));$mpdf->Output('MyPDF.pdf','D');exit;}}

Download

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索,以便于您获取更多的相关知识。