How to generate pdf in Codeigniter?

Member

by deron , in category: PHP Frameworks , 9 months ago

How to generate pdf in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by ryleigh , 3 months ago

@deron 

To generate a PDF in Codeigniter, you can use a library such as DOMPDF or TCPDF.


Here is an example of how to generate a PDF using DOMPDF in Codeigniter:

  1. Download and install DOMPDF. You can download DOMPDF from https://github.com/dompdf/dompdf and install it by copying the DOMPDF folder to your application's third_party directory.
  2. Load the DOMPDF library in your controller. Add the following line to your controller to load the DOMPDF library:
1
$this->load->library('dompdf_gen');


  1. Generate the PDF. To generate the PDF, you can use the following code in your controller:
1
2
3
4
5
6
$data['your_data'] = 'your data';
$html = $this->load->view('your_view', $data, true);

$this->dompdf->load_html($html);
$this->dompdf->render();
$this->dompdf->stream("your_pdf_filename.pdf");


In this example, "your_view" is the view file that contains the HTML code for the PDF, and "your_data" is the data that you want to pass to the view. The "render()" function generates the PDF, and the "stream()" function outputs the PDF to the browser.


You can also specify additional options for the PDF, such as the paper size and orientation, by using the "set_paper()" and "set_orientation()" functions.

1
2
$this->dompdf->set_paper('A4', 'portrait');
$this->dompdf->set_orientation('landscape');


I hope this helps! Let me know if you have any questions.