Yii 2.0: yii2-mpdf

A great PDF component for Yii2 based on the mPDF library.
35 followers

The yii2-mpdf extension is a Yii2 wrapper component for the mPDF library with enhancements. The mPDF library offers ability to generate PDF files from UTF-8 encoded HTML. This library is based on FPDF and HTML2FPDF, with a number of enhancements. The key features in the library are to be able to generate PDF files 'on-the-fly' from HTML content, handling different languages. See the list of features and/or examples for the library. The yii2-mpdf extension offers an easy way to integrate and use the mPDF library within your Yii application with subtle enhancements. The key features offerred with this release are:

  • Setup pdf component globally in your yii application configuration.
  • Setup mPDF properties or call mPDF methods easily using simple array configuration.
  • Enhances extension to setup your own custom CSS file for rendering the formatted HTML content.
  • Extension has a built-in version of bootstrap.css (v3.2.0 modified for mPDF) to be applied by default. This will allow you to generate PDF content from bootstrap markup HTML easily.
  • Offers easy way to prepend inline CSS in addition to your own CSS file.
  • Offers easy to use object oriented methods to render complex PDF.
  • Easy use of the extension like any Yii widget by using the render method with minimal configuration.
  • The extension uses the latest development version (v6.0beta) of the mPDF library. It uses the composer repository kartik-v/mpdf on packagist as a source for this latest version. mPDF 6.0 can utilise OpenType layout tables to display complex scripts. This release (v6.0) contains fonts (open source) to cover almost every imaginable script / language. Includes support for Arabic or Indic scripts (as well as Khmer, Lao, Myanmar etc.). It also is expected to improve the display of Thai, Vietnamese and Hebrew. It also includes additional fonts for Chinese, Japanese, and Korean.
  • Inbuilt integration with yii2-grid extension that allows you to export grid as PDF and even generate advanced PDF reports.

Latest Release

The latest version of the module is v1.0.0 released on 03-Nov-2014. Refer the CHANGE LOG for details.

Demo

You can read the detailed documentation and usage of the extension.

Installation

The preferred way to install this extension is through composer.

Either run:

$ php composer.phar require kartik-v/yii2-mpdf"*"

or add:

"kartik-v/yii2-mpdf": "*"

to the require section of your composer.json file.

Usage

Widget Like Usage

The component can be used straightforward in a manner similar to any widget to render your HTML content as PDF. For example, you can call the component simply like below in your controller action:

use kartik\mpdf\Pdf;
 
public function actionReport() {
    // get your HTML raw content without any layouts or scripts
    $content = $this->renderPartial('_reportView');
 
    // 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' => 'Krajee Report Title'],
         // call mPDF methods on the fly
        'methods' => [ 
            'SetHeader'=>['Krajee Report Header'], 
            'SetFooter'=>['{PAGENO}'],
        ]
    ]);
 
    // return the pdf output as per the destination setting
    return $pdf->render(); 
}

Global Component

You can also setup the widget as a global component for use across your application with defaults preset. For example, setup the following in the components section of your Yii application configuration file:

use kartik\mpdf\Pdf;
// ...
'components' => [
    // setup Krajee Pdf component
    'pdf' => [
        'class' => Pdf::classname(),
        'format' => Pdf::FORMAT_A4,
        'orientation' => Pdf::ORIENT_PORTRAIT,
        'destination' => Pdf::DEST_BROWSER,
        // refer settings section for all configuration options
    ]
]

Once you have setup the component, you can refer it across your application easily:

$pdf = Yii::$app->pdf;
$pdf->content = $htmlContent;
return $pdf->render();

Report

License

yii2-mpdf is released under the BSD 3-Clause License. See the bundled LICENSE.md for details.

Resources

Total 3 comments

#19916 report it
nirmalroka at 2016/07/26 11:37pm
Hi any way to put different header for different pages

Hi I have requirement to have some pages may not have headers and some pages have different headers so I am looking a way to put different header or no header with you above configuration.

Is this possible or not let me know

Thank you waiting for your response

#19228 report it
Kartik V at 2015/04/24 03:24pm
MPDF is a third party library

I do not own the MPDF library. Yii2 MPDF is a component that uses the MPDF Library. You must post your query to the MPDF library author or visit its website for details.

#19222 report it
Scott_Huang at 2015/04/24 04:10am
This widget size is huge, any possible to shrink it?

as title.

You know, I have to customize your grid widget export session to ignore mpdf since I not install this widget due to size issue.

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet