Home | About | View All Posts

1 Mar 2022

How to create PDF file in Yii2 using mpdf

As we know mPDF is a poplular PHP library. We can generate PDF files from UTF-8 encoded HTML using this PHP library. It is mainly based on FPDF and HTML2FPDF.

We can stratify the following steps for the use of mPDF library in the Yii2 framework -
  1. Installing mPdf library
  2. Importing mPdf library into controller file
  3. Creating actions using object of mPDF class
  4. Creating view file for pdf content
  5. Generating and accessing pdf file by end user

  1. Installing mPdf library -
    1. Mpdf manual installation without composer -
      The mPdf version 7 does not advice for manual installation without composer so it is discouraged and not officially supported. The new mPdf uses many dependencies for libraries and classes.

      You need to install those dependencies also in the Yii2 framework. Please visit at "How to install mPdf manually in Yii2 framework" for manual installation and explanation for the steps.
    2. Mpdf auto installation using composer -
      Yii2 mPDF Tutorial : How to install mPDF library using composer? -
      We will use composer for installing the mPDF library in our Yii2 application. All dependencies (libraries and classes) will be automatically installed in Yii2 applicaiton folder by composer itself. Please write the following code for installing the mPDF in the command window in your project folder directory -
      composer require mpdf/mpdf "dev-master"
      //above command will install the mpdf via github (https://github.com/mpdf/mpdf)
      
      For example in my case(yii2cms is the yii project folder name) -
      C:\xampp\htdocs\yii2cms>composer require mpdf/mpdf "dev-master"
      
      or (if you donot want to update dependencies)
      composer require --ignore-platform-reqs mpdf/mpdf "dev-master"
      Then -
      composer update
      
  2. Importing mPdf library into controller file -
    Now, we will import or call the mpdf library into a controller by using the code "use Mpdf\Mpdf;". Here for demo purpose, we will use the SiteController controller. You can create or use own controller as per your requirement.

    Controller File Path - Yii2Project/frontent/controllers/SiteController.php
    namespace app\controllers;
    
    use Yii;
    use yii\filters\AccessControl;
    use yii\web\Controller;
    use yii\filters\VerbFilter;
    use app\models\LoginForm;
    use app\models\ContactForm;
    
    use Mpdf\Mpdf; //Importing the mpdf library installed by composer
    
    class SiteController extends Controller {
    
  3. Creating actions using object of mPDF class -
    Now, we will write the action code in the above controller class for generating the pdf file using the mpdf library. We need to create an object for mPDF. The object will be used for calling the mpdf method to show or download html content into a view file. Following are the code -
     
    	// action for pdf file showing in the browser
    	public function actionAboutPdf(){
            $mpdf = new mPDF();
            $mpdf->WriteHTML($this->renderPartial('aboutpdf')); // Assigning the view file having html content
            $mpdf->Output();
            exit;
        }
        
        // action for pdf file for download
        public function actionForceDownloadAboutPdf(){
            $mpdf = new mPDF();
            $mpdf->WriteHTML($this->renderPartial('aboutpdf')); // Assigning the view file having html content
            $mpdf->Output('AboutPdf.pdf', 'D'); // download pdf file forcely named as - "AboutPdf.pdf" 
            exit;
        }      
    
    You can check in detail for the function renderPartial() at "View rendering process in yii2 framework".

  4. Creating view file for pdf content -
    Now, we will create a view file in which we will use HTML content for generating the pdf file.
    View File Path - Yii2Project/frontend/views/site/aboutpdf.php

  5. Generating and accessing pdf file by end user(Yii2 pdf generator) -
    The pdf file can be shown publically to the end user, in two ways as explain below -
    1. Using direct link in the browser - Copy and paste below urls as per your choice to print or download.
      For show in browser as pdf format -
      http://infotokri.infinityfreeapp.com/yii2cms2022/frontend/web/index.php?r=site%2Faboutpdf
      For download the article as pdf format -
      http://infotokri.infinityfreeapp.com/yii2cms2022/frontend/web/index.php?r=site/force-download-about-pdf

    2. By using action(site/about-pdf, site/force-download-about-pdf) url in a link, for example using below Yii2 HTML helper anchor code or Yii2 URL helper code.
      //For button - showing in the browser
      <?= Html::a('Show in PDF format', ['site/about-pdf'], ['target'=>'_blank', 'class'=>'btn btn-info']) ?>
      or
      <a href="<?= Url::to(['site/about-pdf']); ?>" target="_blank" class="btn btn-info">Show in PDF format</a>
      
      //For force download button
      <?= Html::a('Download in PDF format', ['site/force-download-about-pdf'], ['target'=>'_blank', 'class'=>'btn btn-info']) ?>
      or
      <a href="<?= Url::to(['site/force-download-about-pdf']); ?>" target="_blank" class="btn btn-info">Download in PDF format</a>
      
      

      We have shown these two buttons in the demo as like screenshot below -



      You can check and view them at the link below.

    Demo - see it in live environment | Download Source
Tags :

0 comments:

Post a Comment