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.

Here is the updated post on CodeIgniter 3 – Using mPDF with CodeIgniter 3

Following are the simple steps to use mPDF with CodeIgniter.

Download and extract mPDF

Download mPDF from : http://www.mpdf1.com/mpdf/index.php 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.

3.8 4 votes
Article Rating
Subscribe
Notify of
guest
131 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jaimin Gohel
Jaimin Gohel
6 years ago

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

ต้อม
ต้อม
6 years ago
Reply to  Jaimin Gohel

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

$pdfFilePath = “output_pdf_name.pdf”;

//load mPDF library
$this->load->library(‘m_pdf’);
$obj=$this->m_pdf->load();

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

Gustavo Vedana Erckmann
Gustavo Vedana Erckmann
6 years ago

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

$html=$this->load->view(‘pdf_output’,$data,true);

$pdfFilePath = “certificado.pdf”;
$this->load->library(‘m_pdf’);
$this->pdf = $this->m_pdf->load(‘c’,’A4-L’);
$this->pdf->WriteHTML($html);
$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
freddy
6 years ago

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

freddy
freddy
6 years ago

Hello arjun how to change paper size there ?

already search here https://davidsimpson.me/2013/05/19/using-mpdf-with-codeigniter/ but there is no clear explaination ?

arjun
6 years ago
Reply to  freddy

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
$this->load->library(‘m_pdf’,”‘en-GB'”);

// 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
freddy
6 years ago
Reply to  arjun

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

arjun
6 years ago
Reply to  freddy

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
PHP Dev
6 years ago

Working Code

Gilson Tomy
Gilson Tomy
6 years ago

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

arjun
6 years ago
Reply to  Gilson Tomy

Hey try this :

$this->m_pdf->WriteHTML($html);

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

vikiesakki
vikiesakki
6 years ago
Reply to  arjun

Call to undefined method m_pdf::WriteHTML();

I got this error

arjun
5 years ago
Reply to  vikiesakki

Post updated..

RV
RV
5 years ago
Reply to  arjun

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

arjun
5 years ago
Reply to  RV

what is CI version and OS

arjun
5 years ago
Reply to  arjun

Post updated.. please download demo using download

arjun
5 years ago
Reply to  RV

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

arjun
5 years ago
Reply to  arjun

Post updated.. issue resolved

minminoo
minminoo
6 years ago

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

arjun
5 years ago
Reply to  minminoo

post updated …

vikiesakki
vikiesakki
6 years ago

Call to undefined method m_pdf::WriteHTML();

arjun
5 years ago
Reply to  vikiesakki

I have updated my post ….

monojit halder
monojit halder
6 years ago

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
Nuno Reis
5 years ago

Hello

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

Best regards

Nuno Reis

Alexander Habath
Alexander Habath
5 years ago
Reply to  Nuno Reis

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:

$this->load->library(‘m_pdf’);

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

$pdfer = new mPDF($param);

$pdfer->WriteHTML($html);

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

Nuno Reis
Nuno Reis
5 years ago

Thx for the update info!

Best regards

Nuno Reis

arjun
5 years ago

post has been updated

arjun
5 years ago
Reply to  Nuno Reis

I have updated post ….

Nuno Reis
Nuno Reis
5 years ago
Reply to  arjun

Thx for the update

Best regards

arjun
5 years ago
Reply to  Nuno Reis

it’s my pleasure

Zbyněk Kočí
Zbyněk Kočí
5 years ago

Idiotic post… it cannot work like this

arjun
5 years ago
Reply to  Zbyněk Kočí

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

P Srikanth
P Srikanth
5 years ago

How to apply page layout ->single in this library

arjun
5 years ago
Reply to  P Srikanth

Could you provide more information?

Rinoj Mathew
Rinoj Mathew
5 years ago

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

arjun
5 years ago
Reply to  Rinoj Mathew

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

Vineeth V
Vineeth V
5 years ago

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.

arjun
5 years ago
Reply to  Vineeth V

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
P Srikanth
5 years ago

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

arjun
5 years ago
Reply to  P Srikanth

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

P Srikanth
P Srikanth
5 years ago
Reply to  arjun

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.

arjun
5 years ago
Reply to  P Srikanth

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

// For example – page_viewphp is your view,
$this->load->view(‘header_view’);
// you actual content
$this->load->view(‘footer_view’);

arjun
5 years ago
Reply to  P Srikanth

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

Fauzi Ahmad
Fauzi Ahmad
5 years ago

Unable to load the requested class: m_pdf

arjun
5 years ago
Reply to  Fauzi Ahmad

Download demo and check once

P Srikanth
P Srikanth
5 years ago

How to set by default pdf to landscape

gaurav
gaurav
5 years ago

Can i redirect after creating pdf

P Srikanth
P Srikanth
5 years ago

Setting to set pdf as landscape..?

Saranya Ganesh
Saranya Ganesh
5 years ago

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
magilan
5 years ago

How could i send mine pdf to a mail adress?

Pradeep Kushwaha
Pradeep Kushwaha
5 years ago

Hii
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
Pradeep Kushwaha
5 years ago

Hii
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
Rushabh Patel
5 years ago

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

vinay thool
vinay thool
4 years ago
Reply to  Rushabh Patel

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
Mariano
5 years ago

( ! ) 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
Pastoolio
5 years ago

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.

Bhawani
Bhawani
4 years ago

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

waris shaikh
waris shaikh
4 years ago

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
Rizal Ady Pratama
4 years ago

How to configure paper orientation Landscape or portrait ?

Pradeep Kushwaha
Pradeep Kushwaha
4 years ago

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
Sandy
4 years ago

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
Reyaan Ahamd
4 years ago

Good One..

Amrish Bagthaliya
Amrish Bagthaliya
4 years ago

Hello,

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

plz help me.

thanks

arjun
4 years ago

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
bharathi jyothi chandrika
4 years ago
Reply to  arjun

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
Warayoot Julsawate
4 years ago

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

Backtrace:
……………………………………………..

what happen?

thx every body.

HM Masud Alom Rubel
HM Masud Alom Rubel
4 years ago

hi arjun ,

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

Komal Garg
Komal Garg
4 years ago

Hi,
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??
Thanks..
Komal

Komal Garg
Komal Garg
4 years ago

Hi,
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??
Regards

arjun
4 years ago
Reply to  Komal Garg

Are you getting any error?

Komal Garg
Komal Garg
4 years ago
Reply to  arjun

No.. only blank page

Ratnesh Kumar Dheeraj
Ratnesh Kumar Dheeraj
4 years ago
Reply to  Komal Garg

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

arjun
4 years ago
Reply to  Komal Garg

hey download demo files and try

Komal Garg
Komal Garg
4 years ago
Reply to  arjun

I tried, but its not running on localhost.

garrisonian
4 years ago
Reply to  Komal Garg

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.

Thanks

Bhingaradiya Nilesh R
Bhingaradiya Nilesh R
4 years ago

can it Load Tag

bharathi jyothi chandrika
bharathi jyothi chandrika
4 years ago

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

zyrine mae importa
zyrine mae importa
4 years ago

I got this error:

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

Filename: classes/cssmgr.php

Line Number: 219

Backtrace:

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
Harshad Dusane
4 years ago

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

Rohan Patil
Rohan Patil
4 years ago

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

Rohan Patil
Rohan Patil
4 years ago
Reply to  Rohan Patil

I got the solution.

now its working fine.
thanks.

arjun
4 years ago
Reply to  Rohan Patil

What was the issue?

Rohan Patil
Rohan Patil
4 years ago
Reply to  arjun

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

Tahmid
Tahmid
2 years ago
Reply to  Rohan Patil

can you please give me folder path, i am getting same error

arjun
2 years ago
Reply to  Tahmid

Adjust this path according to your directory location:
include_once APPPATH.’/third_party/mpdf/mpdf.php’;

arjun
2 years ago
Reply to  Tahmid

You can try this post, if your project support composer:
https://arjunphp.com/create-pdf-using-mpdf-codeigniter-3/

naga raju
naga raju
4 years ago

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

Backtrace:

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

arjun
4 years ago
Reply to  naga raju

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

Vinu Chacko
Vinu Chacko
3 years ago
Reply to  arjun

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

arjun
3 years ago
Reply to  Vinu Chacko

Let me check with 7.1 version

Vinu Chacko
Vinu Chacko
3 years ago
Reply to  arjun

Ok. Thank you

Smrutikant Nayak
Smrutikant Nayak
3 years ago
Reply to  Vinu Chacko

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
Vinu Chacko
3 years ago

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

chintan
chintan
3 years ago
Reply to  Vinu Chacko

please send me the solution for solve this issue

arjun
3 years ago
Reply to  chintan

Here is the updated post – tested with PHP7.1 and CI 3 – https://arjunphp.com/create-pdf-using-mpdf-codeigniter-3/

chintan
chintan
3 years ago
Reply to  arjun

Thanks for your help.
but, i am not using composer in my project
is it possible to set it manually in this PHP7.1 and CI 3?
i am using hmvc modular.

arjun
3 years ago
Reply to  chintan

yes, it is possible. Let me update my post

chintan
chintan
3 years ago
Reply to  arjun

ok thanks,
when you update let me know please.

arjun
3 years ago
Reply to  chintan

Here is the updated code – NOTE – made changes in Mpdf library since it is not supporting PHP7 version
https://arjunphp.com/assets/uploads/2018/02/generating-a-pdf-in-codeigniter-using-mpdf-CodeIgniter-3.1.7.zip

chintan
chintan
3 years ago
Reply to  arjun

thanks @arjunphp:disqus.
i will check and let you know if any problem.

chintan
chintan
3 years ago
Reply to  arjun

it is working fine with simple file.
but when i try with my data and file it return with this error
Cannot modify header information – headers already sent by (output started at /opt/lampp/htdocs/rb_glass/system/core/Exceptions.php:271)

but if i print file data without mpdf it display file properly
when i use mpdf at that time the error occure.

this is not actually core file error there are some changes needed in my file
i have try to fine out but i can’t can you help me to find out please…
here you can see my file for 24 hour https://codeshare.io/amJoMk

chintan
chintan
3 years ago
Reply to  arjun

hello @arjunphp:disqus please se my previous comment

chintan
chintan
3 years ago
Reply to  arjun

it is working fine with simple file.
but when i try with my data and file it return with this error
Cannot modify header information – headers already sent by (output started at /opt/lampp/htdocs/rb_glass/system/core/Exceptions.php:271)

but if i print file data without mpdf it display file properly
when i use mpdf at that time the error occure.

this is not actually core file error there are some changes needed in my file
i have try to fine out but i can’t can you help me to find out please…
here you can see my file for 24 hour https://codeshare.io/GqQxPA

please help me to solve this issue.

Allisson Jorge
Allisson Jorge
3 years ago

Thank You!

Anoop Rajendran
Anoop Rajendran
3 years ago

Unable to load the requested class: m_pdf

Syed Osama Baqi
Syed Osama Baqi
3 years ago

I have show this error
Failed to load PDF document.

arjun
3 years ago

Download and try with demo files

Syed Osama Baqi
Syed Osama Baqi
3 years ago
Reply to  arjun

Fixed now. Thank you

Syed Osama Baqi
Syed Osama Baqi
3 years ago

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

Sangamesh Panchal
Sangamesh Panchal
3 years ago

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

Ayyappa Amara
3 years ago

Working fine, thank you. saved lot of time.

arjun
3 years ago
Reply to  Ayyappa Amara

Thank you.

Radha
Radha
3 years ago

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
Leorah Sumarong
3 years ago
arjun
3 years ago

M_pdf library is not loading, did you place M_pdf.php in the library folder?

Download the demo files and verify with it

DMCA.com Protection Status
131
0
Would love your thoughts, please comment.x
()
x