PDF generate in Codeigniter using DOMPDF

Mahabubur Rahman
44



Dompdf is an HTML to PDF converter written in PHP. DOMPDF is a CSS 2.1 compliant HTML layout and rendering engine. It is a style-driven renderer: it will download and read the style features of external stylesheets, inline style tags, and individual HTML elements.




In this topic, we will discuss how to generate PDF using DOMPDF with Codeigniter Framework.



Step 1: Download Pdf.php library from https://github.com/chrisnharvey/CodeIgniter-PDF-Generator-Library/tree/master/libraries and copy it to codeigniter library directory.

Step 2: Download DOMPDF from https://github.com/dompdf/dompdf/tree/v0.6.2 or https://code.google.com/p/dompdf/downloads/list. Extract it and copy the dompdf folder to codeigniter library directory.

Step 3: Open Welocme Controller or any other. Create a method on this controller as bellow-

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function mypdf(){
$this->load->library('pdf');
$this->pdf->load_view('common/template');
$this->pdf->render();
$this->pdf->stream("welcome.pdf");
}

}
Here mypdf function (you can give any name of this function) of Welcome controller will generate pdf file of common/template view file.

In the mypdf  function we first load pdf library and then load view file then render it and stream.

Ok it is done. you can try and enjoy it.

To learn about mPDF please go the following link

PDF Generate in Codeigniter using mPDF

Post a Comment

44Comments
  1. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    Regards,
    SAS Training in Chennai| SAS Course in Chennai

    ReplyDelete
  2. defined('BASEPATH') OR exit('No direct script access allowed');
    here, BASEPATH = base_url ???

    ReplyDelete
  3. HI, it's working good, but I want to generate pdf for 3 php files at a time, please help me.

    ReplyDelete
  4. I placed your code in my project but when i run it, it display blank page. I think it has problem to find pdf.php file from library directory.

    ReplyDelete
    Replies
    1. The article for DOM version 0.6.* https://github.com/dompdf/dompdf/tree/v0.6.2


      So try it with the specific version.

      Delete
  5. The best article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
    the best infomtica training institute

    ReplyDelete
  6. You Code show me error like this

    Severity: Warning

    Message: require_once(C:/xampp/htdocs/ctscms/application/libraries/dompdf/include/functions.inc.php): failed to open stream: No such file or directory

    Filename: dompdf/dompdf_config.inc.php

    Line Number: 52

    Backtrace:

    File: C:\xampp\htdocs\ctscms\application\libraries\dompdf\dompdf_config.inc.php
    Line: 52
    Function: _error_handler

    File: C:\xampp\htdocs\ctscms\application\libraries\dompdf\dompdf_config.inc.php
    Line: 52
    Function: require_once

    File: C:\xampp\htdocs\ctscms\application\libraries\Pdf.php
    Line: 14
    Function: require_once

    File: C:\xampp\htdocs\ctscms\application\third_party\MX\Loader.php
    Line: 173
    Function: _ci_load_library

    File: C:\xampp\htdocs\ctscms\application\modules\requisition\controllers\Welcome.php
    Line: 5
    Function: library

    File: C:\xampp\htdocs\ctscms\index.php
    Line: 315
    Function: require_once


    Fatal error: require_once(): Failed opening required 'C:/xampp/htdocs/ctscms/application/libraries/dompdf/include/functions.inc.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\ctscms\application\libraries\dompdf\dompdf_config.inc.php on line 52
    A PHP Error was encountered

    Severity: Compile Error

    Message: require_once(): Failed opening required 'C:/xampp/htdocs/ctscms/application/libraries/dompdf/include/functions.inc.php' (include_path='.;C:\xampp\php\PEAR')

    Filename: dompdf/dompdf_config.inc.php

    Line Number: 52

    Backtrace:

    ReplyDelete
    Replies
    1. The article for DOM version 0.6.* https://github.com/dompdf/dompdf/tree/v0.6.2

      Delete
  7. • Nice Blog It's such a useful information to all.
    tib co training in chennai

    ReplyDelete
  8. The best article! I found some useful information in your blog,
    sas training in chennai

    ReplyDelete
  9. This comment has been removed by the author.

    ReplyDelete
  10. how to set pdf file using database.. procedure

    ReplyDelete
    Replies
    1. You have to know how to pass data to view. So that you can do it properly.

      Delete
  11. Nice Article. Thanks for sharing usefull Information and I have seen more info on Top Hotel Management College with Affiliated to Osmania University

    ReplyDelete
  12. This information really worth saying, i think you are master of the content and thank you so much sharing that valuable information and get new skills after refer that post.
    Java Training in Chennai

    ReplyDelete
  13. Hi,
    I have this mistake:

    A PHP Error was encountered
    Severity: Warning

    Message: Cannot modify header information - headers already sent by (output started at /home/espacial/public_html/reports/application/controllers/Welcome.php:2)

    Filename: core/Common.php

    Line Number: 564

    Backtrace:

    File: /home/espacial/public_html/reports/index.php
    Line: 315
    Function: require_once

    ReplyDelete
  14. A PHP Error was encountered
    Severity: Warning

    Message: A non-numeric value encountered

    Filename: include/page_frame_decorator.cls.php

    Line Number: 439

    Backtrace:

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\page_frame_decorator.cls.php
    Line: 439
    Function: _error_handler

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 753
    Function: check_page_break

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\inline_frame_reflower.cls.php
    Line: 63
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\block_frame_reflower.cls.php
    Line: 750
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\page_frame_reflower.cls.php
    Line: 123
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\frame_decorator.cls.php
    Line: 711
    Function: reflow

    File: E:\xampp\htdocs\account\application\libraries\dompdf\include\dompdf.cls.php
    Line: 973
    Function: reflow

    File: E:\xampp\htdocs\account\application\controllers\Admin.php
    Line: 1341
    Function: render

    File: E:\xampp\htdocs\account\index.php
    Line: 315
    Function: require_once

    ReplyDelete
  15. Hi to everybody, here everyone is sharing such knowledge, so it’s fastidious to see this site, and I used to visit this blog daily.

    Data Science Training

    ReplyDelete
  16. Very interesting and it caught my attention. Bookmarking your article which will probably be my guide. Thank you very much.
    by Cognex AWS Training and certification in chennai,

    ReplyDelete
  17. Welcome! Exceptionally supportive counsel inside this article! It is the little changes that produce the biggest changes. Much obliged for sharing!
    evrmag

    ReplyDelete
  18. Thanks a lot very much for the high quality and results-oriented help. I won’t think twice to endorse your blog post to anybody who wants and needs support in this area. same as your blog i found another one Mobile Marketing .Actually I was looking for the same information on internet for Mobile Marketing and came across your blog. I am impressed by the information that you have on this blog. Thanks once more for all the details.

    ReplyDelete
  19. Thanks for Sharing This Article.It is very so much valuable content. . AWS course in Chennai

    ReplyDelete
  20. Awesome blog thank you for sharing.

    Make your career development the best by learning software courses in the best software training institute in Chennai.
    power bi certification training
    msbi training in chennai
    Docker Training in Chennai
    android training in chennai
    ios training in chennai
    Xamarin Training in Chennai

    ReplyDelete


  21. Aimore Tech is the Best Software training institute in chennai with 6+ years of experience. We are offering online and classroom training.


    hadoop training in chennai
    software testing training in chennai
    ccna training in chennai

    ReplyDelete

  22. Nice blog thank you .For your Sharing It's a pleasure to read your post.It's full of information I'm looking for and I'd like to express that "The content of your post is awesome"

    Aimore Tech is the Best Software training institute in chennai with 6+ years of experience. We are offering online and classroom Trainings.
    Dotnet Training in Chennai
    Core java Training in Chennai
    Web design Training in Chennai

    ReplyDelete
Post a Comment