How to export mysql data to pdf in codeigniter?

Member

by rollin , in category: PHP CMS , 9 months ago

How to export mysql data to pdf in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by tressie.damore , 9 months ago

@rollin 

To export MySQL data to PDF in CodeIgniter, you can follow these steps:

  1. First, you will need to create a controller in CodeIgniter that will fetch the data from the database and generate a PDF file.
  2. You can use the TCPDF library in CodeIgniter to generate PDF files. You can download the TCPDF library and add it to your CodeIgniter project.
  3. In your controller, fetch the data from the MySQL database using the Model and pass it to the TCPDF library to generate a PDF file.


Here is an example of how you can generate a PDF file in CodeIgniter:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public function exportToPdf(){
    // Load the TCPDF library
    $this->load->library('pdf');

    // Fetch data from MySQL database
    $data['results'] = $this->your_model_name->get_data();

    // Generate PDF file
    $this->pdf->setPaper('A4', 'landscape');
    $this->pdf->AddPage();

    foreach ($data['results'] as $result) {
        $this->pdf->Cell(0, 10, $result['column_name'], 0, 1);
    }

    $this->pdf->Output('filename.pdf', 'D');
}


  1. In this code, replace 'your_model_name' with the name of the model that fetches data from the MySQL database, and 'column_name' with the name of the column you want to display in the PDF file.
  2. You can call the exportToPdf() function in your view or controller to generate the PDF file.


Note: Make sure to configure the TCPDF library correctly in CodeIgniter and set the correct path to the library in your configuration file.