How to generate pdf in Codeigniter?

Member

by deron , in category: PHP Frameworks , 2 years ago

How to generate pdf in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by ryleigh , a year 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.

by giovanny.lueilwitz , 10 months ago

@deron 

To generate a PDF in CodeIgniter, you can use the TCPDF library. Follow the steps below:

  1. Download the TCPDF library from the official website (https://tcpdf.org/) and extract it.
  2. Create a new folder named "libraries" inside your application folder.
  3. Copy the entire TCPDF library folder into the newly created "libraries" folder.
  4. Create a new controller in your CodeIgniter application, for example "PdfController.php", and load the TCPDF library.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Your Name');
        $pdf->SetTitle('Your Title');
        $pdf->SetSubject('Your Subject');
        $pdf->SetKeywords('Keywords');

        // Add a page
        $pdf->AddPage();

        // Set some content in the PDF
        $pdf->SetFont('helvetica', '', 12);
        $pdf->Cell(0, 10, 'Hello World!', 0, 1, 'C');

        // Output the PDF as a file
        $pdf->Output('example.pdf', 'I'); // 'I' to open in browser, 'D' to download

        // You can also save the PDF to a file by using 'F' as the second parameter of the Output() method
        // $pdf->Output('example.pdf', 'F');
    }
}


Note: Make sure to provide the correct path to the TCPDF library in the require_once statement.

  1. To generate the PDF, access the generatePDF() method through a URL in your browser. For example: http://localhost/your-app/index.php/PdfController/generatePDF.


The example above demonstrates how to generate a simple PDF with the text "Hello World!" on one page. You can further customize and populate the PDF according to your needs using the TCPDF methods and functions.