Generating a PDF in Codeigniter using mPDF

Hey, today I will show you integrating mPDF into CodeIgniter Application. mPDF is a PHP Class, by using this we can easily generate PDF files from HTML Templates.So styling PDF files as easy styling your HTML template as you wish.

Following are the simple steps to use mPDF with CodeIgniter.

Download and extract mPDF

Download mPDF from : and Extract it to your application/third_party/ folder of your CodeIgniter.

Create a new file in your application/libraries/ name it M_pdf.php and copy past the blow PHP Script.

That is it. You had successfully integrated mPDF in CodeIgniter. Now let’s use it.

How to Use

Load the mPDF library just like your load other CI’s other libraries and then call pdf property. See the below example code for better understanding.

That’s it.

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.


I am Arjun from Hyderabad (India). I have been working as a software engineer from last 7+ years, and its my passion to learn new things and implement them as a practice. Aside from work, I likes gardening and spending time with pets.

  • Jaimin Gohel

    did exactly as you said its giving me error like this..

    • ต้อม

      $html=$this->load->view(‘test’,”, true);

      $pdfFilePath = “output_pdf_name.pdf”;

      //load mPDF library

      //download it.
      $obj->Output($pdfFilePath, “D”);

  • Gustavo Vedana Erckmann

    In my CI (2.3)I have to change something to work


    $pdfFilePath = “certificado.pdf”;
    $this->pdf = $this->m_pdf->load(‘c’,’A4-L’);
    $this->pdf->Output($pdfFilePath, “D”);

    In m_pdf class I needed to change the load function because parameters are not working.

    — FUNCTION LOAD CHANGED ————————————–

    function load($mode = ”, $format = ‘A4’, $default_font_size = 0, $default_font = ”, $mgl = 15, $mgr = 15, $mgt = 16, $mgb = 16, $mgh = 9, $mgf = 9, $orientation = ‘l’) {

    include_once APPPATH. ‘/third_party/mpdf60/mpdf.php’;

    return new mPDF($mode, $format, $default_font_size, $default_font, $mgl, $mgr, $mgt, $mgb, $mgh, $mgf, $orientation);


    • freddy

      @gustavovedanaerckmann:disqus it’s not working for me to change size i did your advice above paper doesn’t change >.<

  • freddy

    Hello arjun how to change paper size there ?

    already search here but there is no clear explaination ?

    • while loading library pass page size and other setting as a second parameter, like below shown

      // Define a new mPDF document using win-1252 fonts based on a language/country code

      // Define a Landscape page size/format by name
      $this->load->library(‘m_pdf’,”‘utf-8’, ‘A4-L'”);

      // Define a page size/format by array – page will be 190mm wide x 236mm height
      $this->load->library(‘m_pdf’,”‘utf-8’, array(190,236)”);

      // Define a page using all default values except “L” for Landscape orientation
      $this->load->library(‘m_pdf’,””,”, 0, ”, 15, 15, 16, 16, 9, 9, ‘L'”);

      • freddy

        these code setting for controller or in mpdf.php ? please more example arjun

        • If you want to set setting globally change it in your library file

          if ($params == NULL)
          $param = ‘”en-GB-x”,”A4″,””,””,10,10,10,10,6,3’; // change this line

          if you need different page sizes only for particular action or controller load something like above shown : $this->load->library(‘m_pdf’,””,”, 0, ”, 15, 15, 16, 16, 9, 9, ‘L'”);

  • PHP Dev

    Working Code

  • Gilson Tomy

    it shows an error as Call to a member function WriteHTML()

    • Hey try this :


      $this->m_pdf->Output($pdfFilePath, “D”);

      • vikiesakki

        Call to undefined method m_pdf::WriteHTML();

        I got this error

      • RV

        I still get the same error after trying that. please help

        • what is CI version and OS

          • Post updated.. please download demo using download

        • some times it may be … case sensitive issue …..

      • Post updated.. issue resolved

  • minminoo

    Showing error Fatal error: Call to a member function WriteHTML() on a non-object in

  • vikiesakki

    Call to undefined method m_pdf::WriteHTML();

    • I have updated my post ….

  • monojit halder

    My view is picture 1 bellow after download pdf its look like picture 2. Please help to create exact pdf like my view.. Please Help me..

  • Nuno Reis


    For CI 3,x does not seem to work.
    Is there any code changes for CI 3.x?

    Best regards

    Nuno Reis

    • Alexander Habath

      I changed the library to:

      class M_pdf
      public function __construct()
      require_once APPPATH.’/third_party/mpdf/mpdf.php’;

      and then load it like this in any controller:


      $param = ‘”en-GB-x”,”A4″,””,””,10,10,10,10,6,3’;

      $pdfer = new mPDF($param);


      $pdfer->output($pdfFilePath, “D”);

      • Nuno Reis

        Thx for the update info!

        Best regards

        Nuno Reis

      • post has been updated

    • I have updated post ….

      • Nuno Reis

        Thx for the update

        Best regards

        • it’s my pleasure

  • Zbyněk Kočí

    Idiotic post… it cannot work like this

    • what is your problem, do you know CI? Download link also included in the post

  • P Srikanth

    How to apply page layout ->single in this library

    • Could you provide more information?

  • Rinoj Mathew

    I have face the errors “Some data has already been output to browser, can’t send PDF file” and Message: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead , can you please help me.
    Thanks in advance

    • It means that your echoing something before sending PDF file to the browser or probably you should have enabled output compression

  • Vineeth V

    I want to create a Malayalam pdf, so I have created a view with some Malayalam text. But it is coming as rectangles in pdf as shown in the below image. Please help.

    • which version of Mpdf are you using? For version 6.0 and above you can use following config settings

      $mpdf->autoScriptToLang = true;
      $mpdf->autoLangToFont = true;

  • P Srikanth

    How to set fixed header and footer for every page by using this library

    • Are you converting HTML pages to PDF? If yes, make your HTML pages dynamic which means common header and footer.

      • P Srikanth

        In PDF 5 pages are there,how to set header and footer to every page.Like heading at starting and phone no,address at ending.

        • you can load partial view inside the view file In CI.

          // For example – page_viewphp is your view,
          // you actual content

        • If you want to show form in PDF, then design it and convert it to pdf

  • Fauzi Ahmad

    Unable to load the requested class: m_pdf

    • Download demo and check once

  • P Srikanth

    How to set by default pdf to landscape

  • gaurav

    Can i redirect after creating pdf

  • P Srikanth

    Setting to set pdf as landscape..?

  • Saranya Ganesh

    Hi, I got error ‘Call to a member function WriteHTML() on a non-object’ and i don’t know why it is happening .. So can u guide me? as well as in view i need pdf format…

  • magilan

    How could i send mine pdf to a mail adress?

  • Pradeep Kushwaha

    i try to convert html page to pdf it work fine. But font size of text on pdf remain constant it does not changed according to font size given in m_pdf library ,Event not according to font size of html page.
    i am using the css for the table content
    my library code is–

    param =$param;
    $this->pdf = new mPDF($this->param);

  • Pradeep Kushwaha

    i try to convert html to m pdf it work fine. But font family and bold tag not apply in pdf
    my config_font.php code is…
    $this->fontdata = array(
    “HelveticaNeue” => array(
    ‘R’ => “HelveticaNeue.ttf”,
    ‘B’ => “HelveticaNeueBold.ttf”,
    ‘I’ => “HelveticaNeueItalic.ttf”,
    ‘BI’ => “HelveticaNeueBoldItalic.ttf”,

    and html code

    Device Name :

    Here bold and font family in pdf
    plz help me

  • Rushabh Patel

    Hello Arjun
    I use the same code for pdf but it display error…Plz help

    • vinay thool

      You Should Create a folder on application/libraries and put it all the Mpdf files and folder..
      and create class file like this..

      param ='”en-GB-x”,”A4″,””,””,10,10,10,10,10,10′;
      $this->pdf = new mPDF($this->param);


      then you can load your library..

  • Mariano

    ( ! ) Fatal error: Class ‘mPDF’ not found in C:wampwwwci_3applicationlibrariesM_pdf.php on line 13
    Call Stack
    # Time Memory Function Location
    1 0.0010 152632 {main}( ) ..index.php:0
    2 0.0030 197688 require_once( ‘C:wampwwwci_3systemcoreCodeIgniter.php’ ) ..index.php:315
    3 0.0680 2163648 call_user_func_array:{C:wampwwwci_3systemcoreCodeIgniter.php:514} ( ) ..CodeIgniter.php:514
    4 0.0680 2164048 DataAdminFunctioncont->pdf_format( ) ..CodeIgniter.php:514
    5 0.1150 2317736 CI_Loader->library( ) ..DataAdminFunctioncont.php:136
    6 0.1150 2317800 CI_Loader->_ci_load_library( ) ..Loader.php:218
    7 0.1150 2320568 CI_Loader->_ci_init_library( ) ..Loader.php:1087
    8 0.1160 2321024 M_pdf->__construct( ) ..Loader.php:1292

    facing this error, please help.

  • Pastoolio

    For me the file doesn’t get created, I use this code:

    $pdfFilePath = FCPATH . “tmp/output_pdf_name.pdf”;

    And yes, I do have the tmp folder in my top folder with permissions set to 777.
    I can see the content of the pdf in my browser debugger.

    • Arjun

      are you getting any error?

  • Bhawani

    Thank you dear…i have done with your nice tutorial…generate nice pdf 🙂

  • waris shaikh

    Gr8 bro its working like a charm i have searched alot like fpdf, pdfdom but not get the actual thing as mpdf perfectly did 🙂
    May AL-Mighty Bless you and your family 🙂 AAMEEN

  • Rizal Ady Pratama

    How to configure paper orientation Landscape or portrait ?

  • Pradeep Kushwaha

    Hi Arjun,
    I am using your library code to convert HTML to pdf is work fine for size upto 30 page.
    But When i am coverting larrge data beyod 30 page its give fetal Error As follow

    “Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 23 bytes) in /opt/lamp/apache2/htdocs/application/third_party/mpdf/classes/cssmgr.php on line 213”
    I also set the memory limit 512M from default size 128M. in /opt/lamp/php/etc/php.ini file.
    Still i am getting the same error.

  • Sandy

    Hi Arjun,

    I’m trying to generate pdf files in a loop, and each loop has a unique html data.
    when the loop is running… html data is being appended to instance for each loop.

    Please provide the solution how to avoid this problem it is type of urgent issue for me.

    Note: I have implemented the structure as you have specified. thank in advance..

  • Reyaan Ahamd

    Good One..

  • Amrish Bagthaliya


    i want to print Chinese words in pdf how can i do this, its coming in block block.

    plz help me.


    • You need to enable the support of PDF Asian font like this:

      $this->m_pdf->pdf->autoLangToFont = true;
      $this->m_pdf->pdf->autoScriptToLang = true;

      • bharathi jyothi chandrika

        Hi arjun,

        I’m using
        $this->m_pdf->pdf->autoLangToFont = true;
        $this->m_pdf->pdf->autoScriptToLang = true;
        but in PDF for telugu language it showing boxes. Please help me.

  • Warayoot Julsawate

    hell me plz.
    my web error
    A PHP Error was encountered

    Severity: Compile Error

    Message: Cannot redeclare class mPDF

    Filename: mpdf/mpdf.php

    Line Number: 90


    what happen?

    thx every body.

  • HM Masud Alom Rubel

    hi arjun ,

    this code very helpfull but i can’t apply style ..plz help

  • Komal Garg

    Thanks for this code.
    It is working perfectly fine on localhost.. But on live it is not generating Pdf.
    Can you please help me out??

  • Komal Garg

    I am using mpdf to generate pdf..
    On localhost, its working fine.
    But On server it shows blank page.
    Can you please help me out??

    • Are you getting any error?

      • Komal Garg

        No.. only blank page

        • Ratnesh Kumar Dheeraj

          can u share your code pls?? [email protected]

        • hey download demo files and try

          • Komal Garg

            I tried, but its not running on localhost.

    • Is your problem resolved? I am facing same issue. On one of my servers PDF is being generated successully while on other empty pdf file is created.

      If your problem has been resolved, please guide me as well.


  • Bhingaradiya Nilesh R

    can it Load Tag

  • bharathi jyothi chandrika

    can i translate English words into telugu using mfpdf like Lekhini. ?

  • zyrine mae importa

    I got this error:

    Message: preg_replace(): Compilation failed: regular expression is too large at offset 53965

    Filename: classes/cssmgr.php

    Line Number: 219


    File: C:wampwwwIntegrated_prevapplicationthird_partympdfclassescssmgr.php
    Line: 219
    Function: preg_replace

    File: C:wampwwwIntegrated_prevapplicationthird_partympdfmpdf.php
    Line: 12844
    Function: ReadCSS

    File: C:wampwwwIntegrated_prevapplicationcontrollershome.php
    Line: 1117
    Function: WriteHTML

    File: C:wampwwwIntegrated_previndex.php
    Line: 295
    Function: require_once

    what happen?

  • Harshad Dusane

    can we change design while generating pdf using mpdf????

  • Rohan Patil

    I am getting Error as,
    Fatal error: Class ‘mPDF’ not found

    • Rohan Patil

      I got the solution.

      now its working fine.

      • What was the issue?

        • Rohan Patil

          issue was regarding to folder path.
          thanks for your support.

  • naga raju

    hi arjun i getting this error,

    Severity: 8192

    Message: Methods with the same name as their class will not be constructors in a future version of PHP; mPDF has a deprecated constructor

    Filename: mpdf/mpdf.php

    Line Number: 89


    File: E:xammphtdocscrmapplicationlibrariesM_pdf.php
    Line: 3
    Function: _error_handler

    File: E:xammphtdocscrmapplicationlibrariesM_pdf.php
    Line: 3
    Function: include_once

    File: E:xammphtdocscrmapplicationcontrollerswelcome.php
    Line: 30
    Function: library

    File: E:xammphtdocscrmindex.php
    Line: 315
    Function: require_once

    • Download sample files and test once, what is your PHP version?

      • Vinu Chacko

        Same Error . I am using php 7.1.1.
        Is the bug fixed ?

        • Let me check with 7.1 version

          • Vinu Chacko

            Ok. Thank you

          • Smrutikant Nayak

            Vinu, if you are still searching this then, replace line 96 and 97 in mpdf/includes/functions.php with following

            $str = preg_replace_callback(‘/&#([0-9]+);/m’, function($m){ return code2utf($m[1],$lo); }, $str);
            $str = preg_replace_callback(‘/&#([0-9]+);/m’, function($m){ return codeHex2utf($m[1],$lo);}, $str);

          • Vinu Chacko

            Thank you. I’am already solved.. Thank you for your advice.

  • Allisson Jorge

    Thank You!

  • Anoop Rajendran

    Unable to load the requested class: m_pdf

  • Syed Osama Baqi

    I have show this error
    Failed to load PDF document.

    • Download and try with demo files

      • Syed Osama Baqi

        Fixed now. Thank you

  • Syed Osama Baqi

    Please guide, If I use this for html to pdf then how to this pdf are in email attachment.
    Thank in advance

  • Sangamesh Panchal

    Hi I want fonts in kannada,tamil,telugu,hindi
    so what is the setting to do in mdf6.0 library

  • Working fine, thank you. saved lot of time.

  • Radha

    I am using codeigniter. and i want convert html data to pdf. This html data contains google maps and jquery plugin. i am able to create with html data but google maps and jquery plugin is not inserting into pdf. Please help me.

  • Leorah Sumarong
  • Atul Baldaniya

    Hi Arjun,

    I am trying to send pdf file like this but not working.

    can you help me for this.

    $html=$this->load->view(‘formpdf’, $data, true);
    $pdfFilePath = “myfile.pdf”;
    $this->m_pdf->pdf->Output($pdfFilePath, “D”);


  • Worldemart Development

    Thanks dear… You saved my lots of time…. Protection Status