Cara Membuat Laporan PDF menggunakan TCPDF di CodeIgniter


tcpdf


TCPDF Library

  1. Download plugin di sini
  2. Extract dan paste di /web/application/libraries/tcpdf/
  3. Edit file pdf.php yang berada di /web/application/libraries/

    <?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    require_once dirname(__FILE__) . '/tcpdf/tcpdf.php';
    class Pdf extends TCPDF {
        function __construct() {
            parent::__construct();
        }
          protected $processId = 0;
          protected $header = '';
          protected $footer = '';
          static $errorMsg = '';
          public $page_counter = 1;
          public $isLastPage = false;
          /**
           * This method is used to override the parent class method.
           * */
          public function Header() {
              // Position at 15 mm from bottom
              $this->SetY(-500);
              // Set font
              $this->SetFont('helvetica', 'B', 10);
              // Page number
              $halaman = $this->PageNo();
              if (isset($_POST["hal"]) && !empty($_POST["hal"]) && $this->isLastPage) {
                  $this->Cell(0, 12, ($halaman - 1) + $_POST["hal"], 0, 0, 'R', 0, '', 0, false, 'T', 'M');
              } else if (isset($_POST["hal"]) && !empty($_POST["hal"])) {
                  $this->Cell(0, 12, ($halaman - 1) + $_POST["hal"], 0, 0, 'R', 0, '', 0, false, 'T', 'M');
              } else if (!isset($_POST["hal"]) && empty($_POST["hal"]) && $this->isLastPage) {
                  $this->Cell(0, 12, ($halaman - 1) + 1, 0, 0, 'R', 0, '', 0, false, 'T', 'M');
              } else {
                  $this->Cell(0, 12, ($halaman - 1) + 1, 0, 0, 'R', 0, '', 0, false, 'T', 'M');
              }
          }

          public function lastPage($resetmargins = false) {
              $this->setPage($this->getNumPages(), $resetmargins);
              $this->isLastPage = true;
          }

          public function Footer() {
              // Position at 15 mm from bottom
              //$this->SetY(-15);
              // Set font
              $this->SetFont('helvetica', 'B', 8);
              // Page number
              $halaman = $this->PageNo();
              if (isset($_POST["hal"]) && !empty($_POST["hal"]) && $this->isLastPage) {
                  $this->Cell(0, 12, false, 0, 0, 'C', 0, '', 0, false, 'T', 'M');
              } else if (isset($_POST["hal"]) && !empty($_POST["hal"])) {
                  $this->Cell(0, 12, false, 'T', 0, 'C', 0, '', 0, false, 'T', 'M');
              } else if (!isset($_POST["hal"]) && empty($_POST["hal"]) && $this->isLastPage) {
                  $this->Cell(0, 12, false, 0, 0, 'C', 0, '', 0, false, 'T', 'M');
              } else {
                  $this->Cell(0, 12, false, 'T', 0, 'C', 0, '', 0, false, 'T', 'M');
              }
          }
      }

      /* End of file Pdf.php */
      /* Location: ./application/libraries/Pdf.php */
    • Controllers

      Buka file controller yang dipakai dan gunakan fungsi pemanggilan view saja.

      function pdf() {
      ini_set('memory_limit', '-1');
      $data['cetak'] = $this->rko_model->cetak_4($org_key, $tahap, $tahun);
      $this->load->view('pdf_view', $data);
      }
    • Sesuaikan Model
    • Custom View

      <?php
      $this->load->library('Pdf');$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);$width = $pdf->pixelsToUnits(812.598425197); //width F4$height = $pdf->pixelsToUnits(1227.244094488); //heightF4$resolution = array($width, $height);
      $pdf->SetDisplayMode('fullpage', 'SinglePage', 'UseNone');
      $pdf->SetFont('times', '', 10); //font-family, 'color', 'size'$pdf->setFooterMargin(20); //sesuaikan SetAutoPageBreak$pdf->SetMargins(40, 30, 13, 20, true); //left, top, right, bottom$pdf->setPrintHeader(false);$pdf->setPrintFooter(true);$pdf->SetAutoPageBreak(true, 20); //sesuaikan setFooterMargin$pdf->SetAuthor('Titile');$pdf->AddPage('L', $resolution); //L, Pob_start();
      $html = '<table>
      <thead><tr><td></td></tr></thead><tbody>';
      //foreach(){}

      $html.='</tbody>
      </table>';
      // echo $html;
      ob_end_clean();$pdf->writeHTML($html, true, false, true, false, '');$pdf->Output('Bagian_5_RKO.pdf', 'D');
      ?>


      Jika ada error silakan komen atau hubungi saya.


    Sekian, terimakasih atas kunjungannya.
    Salam,
    PHP Aku : Membangun web terorganisasi dan mudah dimengerti, contoh tutorial dengan menggunakan HTML, CSS, JavaScript, SQL, PHP, dan XML.


    EmoticonEmoticon