CodeIgniter 4: Cara Membuat Laporan PDF

CodeIgniter 4: Cara Membuat Laporan PDF

Sobatcoding.com - Membuat Laporan PDF dengan Codeigniter 4

 

Salah satu fitur yang ada di sebuah aplikasi terutama website adalah fitur cetak laporan. Laporan bisa dicetak dalam beberapa format antara lain format excel, csv, word ataupun juga format pdf.

Terdapat berbagai macam library yang disediakan untuk membuat sebuah data ke format pdf antara lain TCPDF, DOMPDF, FPDF, WKHTMLTOPDF dan masih banyak library yang lainnya.

Pada kesempatan kali ini admin akan berbagi cara membuat laporan pdf dengan codeigniter 4 menggunakan library TCPDF.

 

Perhatikan langkah-langkah pembuatan laporan PDF Codeigniter 4 berikut ini:

Download Library TCPDF

Pertama kita download dulu library TCPDF melalui composer dengan perintah seperti berikut:

composer require tecnickcom/tcpdf

Tunggu sampai proses download selesai.

 

Membuat Controller

Buatlah sebuah controller bernama PdfController.php dan masukkan kode seperti berikut:

<?php

namespace App\Controllers;

use TCPDF;

class PdfController extends BaseController
{

    public function cetak()
    {

        // create new PDF document
        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
        
        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Sobatcoding.com');
        $pdf->SetTitle('PDF Sobatcoding.com');
        $pdf->SetSubject('TCPDF Tutorial');
        $pdf->SetKeywords('TCPDF, PDF, example, sobatcoding.com');

        // set default header data
        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
        $pdf->setFooterData(array(0,64,0), array(0,64,128));

        // set header and footer fonts
        $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

        // set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

        // set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        // set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        // set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

        // set default font subsetting mode
        $pdf->setFontSubsetting(true);

        // Set font
        // dejavusans is a UTF-8 Unicode font, if you only need to
        // print standard ASCII chars, you can use core fonts like
        // helvetica or times to reduce file size.
        $pdf->SetFont('dejavusans', '', 14, '', true);

        // Add a page
        // This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

       //view mengarah ke invoice.php
        $html = view('invoice');

        // Print text using writeHTMLCell()
        $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);

        // ---------------------------------------------------------
        $this->response->setContentType('application/pdf');
        // Close and output PDF document
        // This method has several options, check the source code documentation for more information.
        $pdf->Output('invoice-pos-sobatcoding.pdf', 'I');

    }

}

 

Custom Header dan Footer

Untuk custom header footer kita bisa membuat sebuah library baru bernama MY_TCPDF.php dan simpan di folder Libaries. Masukkan kode seperti berikut :

<?php

namespace App\Libraries;

use TCPDF;

class MY_TCPDF extends TCPDF {

    //Page header
    public function Header() {
        // Logo
        $image_file = ROOTPATH.'public/logo-bg.png';
        /**
         * width : 50
         */
        $this->Image($image_file, '', '', 50);
        // Set font
        $this->SetFont('helvetica', 'B', 11);
        $this->SetX(70);
        $this->Cell(0, 2, 'sobatcoding.com', 0, 1, '', 0, '', 0);
        // Title
        $this->SetFont('helvetica', '', 9);
        $this->SetX(70);
        $this->Cell(0, 2, 'Gg. Permadi No 55 Polehan Malang', 0, 1, '', 0, '', 0);
        $this->SetX(70);
        $this->Cell(0, 2, 'Telp. 0813 3198 9882', 0, 1, '', 0, '', 0);
        $this->SetX(70);
        $this->Cell(0, 2, 'https://sobatcoding.com', 0, 1, '', 0, '', 0);
        
        // QRCODE,H : QR-CODE Best error correction
        $this->write2DBarcode('https://sobatcdoing.com', 'QRCODE,H', 0, 3, 20, 20, ['position' => 'R'], 'N');

        $style = array('width' => 0.25, 'cap' => 'butt', 'join' => 'miter', 'dash' => 0, 'color' => array(0, 0, 0));
        $this->Line(15, 25, 195, 25, $style);

    }

    // Page footer
    public function Footer() {
        // Position at 15 mm from bottom
        $this->SetY(-15);
        // Set font
        $this->SetFont('helvetica', 'I', 8);
        // Page number
        $this->Cell(0, 10, 'Page '.$this->getAliasNumPage().'/'.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
    }
}

 

Kita ubah library yang kita gunakan dengan library MY_TCPDF.php yang telah dibuat. Caranya sebagai berikut :

#use TCPDF;
#Ubah source library ke path library MY_TCPDF

use App\Libraries\MY_TCPDF AS TCPDF;

 

Ubah source path library ke path library MY_TCPDF, sehingga di PdfController.php terlihat seperti berikut :

<?php

namespace App\Controllers;

use App\Libraries\MY_TCPDF AS TCPDF;


class PdfController extends BaseController
{

 

Membuat View

Kita buat sebuah view bernama invoice.php dan masukkan kode seperti berikut :

<p style="font-size:18pt;text-align:right">INVOICE</p>
<span>Kepada Yth.</span><br/>
<table cellpadding="0" >
    <tr>
        <th width="10%">Nama</th>
        <th width="40%">: <strong>M. Basri</strong></th>
        <th width="10%">Sales</th>
        <th width="40%">: <strong>Administrator</strong></th>
    </tr>
    <tr>
        <th width="10%">Alamat</th>
        <th width="40%">: <strong>Jl. Muharto Gang 5 Malang</strong></th>
        <th width="10%">No.Invoice</th>
        <th width="40%">: <strong>202109280001</strong></th>
    </tr>
    <tr>
        <th width="10%">Telp</th>
        <th width="40%">: <strong>081244785625</strong></th>
        <th width="10%">Tanggal</th>
        <th width="40%">: <strong>28 Sept 2021</strong></th>
    </tr>
</table>
<p></p>
<table id="tb-item" cellpadding="4" >
    <tr style="background-color:#a9a9a9">
        <th width="35%" style="height: 20px"><strong>Nama Barang</strong></th>
        <th width="8%" style="height: 20px;text-align:center"><strong>Qty</strong></th>
        <th width="12%" style="height: 20px"><strong>Satuan</strong></th>
        <th width="15%" style="height: 20px"><strong>Harga</strong></th>
        <th width="15%" style="height: 20px"><strong>Diskon</strong></th>
        <th width="15%" style="height: 20px"><strong>Total</strong></th>
    </tr>
    <tr>
        <td style="height: 20px">Pembuatan website E-commerce</td>
        <td style="height: 20px;text-align:center">1</td>
        <td style="height: 20px;">PCS</td>
        <td style="height: 20px;text-align:right">1.500.000</td>
        <td style="height: 20px;text-align:right">0</td>
        <td style="height: 20px;text-align:right">1.500.000</td>
    </tr>
    <tr style="border:1px solid #000">
        <td colspan="5" style="height: 20px"><strong>Grand Total</strong></td>
        <td style="height: 20px;text-align:right"><strong>1.500.000</strong></td>
    </tr>
</table>
<p>Terbilang: Satu Juta Lima Ratus Ribu Rupiah</p>
<p><u>TRANSFER VIA</u></p>
<p>BCA: IDR<br/>A/C : 164-800-3325<br/>A/N : SOBATCODING.COM</p>
<p>&nbsp;</p>
<table cellpadding="4" >
    <tr>
        <td width="50%" style="height: 20px;text-align:center">
            <p>&nbsp;</p>
        </td>
        <td width="50%" style="height: 20px;text-align:center">
            <p>Malang, 28 Sept 2021</p>
            <p>Hormat kami,</p>
            <p></p>
            <p></p>
            <p></p>
            <p>sobatcoding.com</p>
        </td>
    </tr>
</table>

 

Tambahkan style CSS seperti berikut:

<style>
    p, span, table { font-size: 12px}
    table { width: 100%; border: 1px solid #dee2e6; }
    table#tb-item tr th, table#tb-item tr td {
        border:1px solid #000
    }
</style>

 

Membuat Route

Buka file Routes.php dan buatlah sebuah route untuk akses ke controller diatas :

$routes->get('/pdf/cetak', 'PdfController::cetak');

Jalankan perintah php spark serve dan akses url http://localhost:8080/pdf/cetak 

Hasil dari script di atas bila berhasil adalah seperti berikut :

PDF Sobatcoding.com

 

Contoh diatas adalah contoh pembuatan laporan invoice format PDF dengan tampilan static. Kalian bisa membuat laporan PDF bersifat dinamis menggunakan database MYSQL yang telah kalian persiapkan.

 

Sekian tutorial kali ini dan semoga bermanfaat.

Jika teman-teman memiliki pertanyaan atau saran mengenai artikel ini, jangan ragu untuk meninggalkan komentar pada form di bawah ini.