How to use tcpdf in Codeigniter?

Member

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

How to use tcpdf in Codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by orpha , 2 years ago

@lottie 

To use TCPDF in Codeigniter, you can follow these steps:

  1. Download and extract the TCPDF library from the official website (https://www.tcpdf.org/).
  2. Place the extracted TCPDF folder inside the application/libraries folder of your Codeigniter project.
  3. In your controller, load the TCPDF library using the $this->load->library() method. For example:
1
$this->load->library('tcpdf');


  1. Create a new PDF document using the TCPDF() constructor. You can pass the page size, orientation and other parameters as arguments to the constructor. For example:
1
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8', false);


  1. Set the document properties using the SetTitle(), SetAuthor() and SetSubject() methods. For example:
1
2
3
$pdf->SetTitle('My PDF Document');
$pdf->SetAuthor('John Doe');
$pdf->SetSubject('PDF with TCPDF in Codeigniter');


  1. Add content to the PDF using the various methods provided by TCPDF, such as AddPage(), WriteHTML(), Cell(), Multicell(), etc.
  2. Output the PDF to the browser or save it to a file using the Output() method. For example:
1
$pdf->Output('my_pdf_document.pdf', 'I');


This will output the PDF to the browser. You can also use the F option to save the PDF to a file on the server.


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

by filiberto , a year ago

@lottie 

To use TCPDF library in CodeIgniter, follow the steps below:


Step 1: Download and Extract TCPDF library

  1. Go to the official TCPDF website: https://tcpdf.org/
  2. Download the latest stable version of TCPDF.
  3. Extract the downloaded file to the library folder inside the CodeIgniter application directory.


Step 2: Creating a TCPDF Wrapper Class

  1. Create a new file named TCPDF_wrapper.php in the application/libraries folder.
  2. Paste the following code in the TCPDF_wrapper.php file:
1


Step 3: Configuring the Autoloader

  1. Open the application/config/autoload.php file.
  2. Locate the $autoload['libraries'] array.
  3. Add 'TCPDF_wrapper' to the array.
1
$autoload['libraries'] = array('TCPDF_wrapper');


Step 4: Using TCPDF in a Controller

  1. Create a new controller or open an existing one.
  2. Load the TCPDF_wrapper library using the $this->load->library('TCPDF_wrapper') method.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
load->library('TCPDF_wrapper');
    }

    public function generate_pdf() {
        // Example usage of TCPDF_wrapper class
        $pdf = new TCPDF_wrapper();
        $pdf->SetTitle('Hello World');
        $pdf->AddPage();
        $pdf->SetFont('helvetica', '', 12);
        $pdf->Cell(0, 10, 'Hello World', 0, 1, 'C');
        $pdf->Output();
    }
}
?>


Step 5: Accessing the Generate PDF Method

  1. Configure a route to access the generate_pdf() method.
  2. Open the application/config/routes.php file.
  3. Add the following line to the file:
1
$route['generate-pdf'] = 'PdfController/generate_pdf';


Now you can access the generate_pdf() method by accessing the URL http://your_domain/generate-pdf, and it will generate a PDF with the text "Hello World". You can modify the content and styling of the PDF according to your requirements.