Tutorial Menampilkan data dari database di pdf dengan phalcon php framework




Jika anda belum sama sekali bermain dengan phalcon , lebih baik anda mengikuti tutorial phalcon paling awal terlebih dahulu agar mudah dalam mengikuti tutorial ini. :D

Image result for pdf and database

Baiklah pada tutorial kali ini saya akan berbagi cara Menampilkan data dari database di pdf dengan phalcon php framework.

Setelah sebelumnya kita mempelejari mengenai tutorial cara membuat pdf di phalcon.

Lanjutanya adalah bagaimana memparsing data dari database dan ditampilkan di pdf. Tetapi sebaiknya nada download dulu file pada tutorial sebelumnya agar bisa mengikuti tutorial ini.

Oke sekarang kita buat database terlebih dahulu , asumsikan kita sudah punya database dengan nama sekolah.
Lalu kita buat tabel siswa , berikut query pembuatan tabel siswa :


CREATE TABLE `siswa` (
  `id` int(11) NOT NULL,
  `nis` varchar(20) DEFAULT NULL,
  `nama` varchar(100) DEFAULT NULL,
  `alamat` varchar(100) DEFAULT NULL,
  `kelas` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Setelah itu kita inputkan beberapa data , berikut query insert beberapa data siswa :
INSERT INTO `siswa` VALUES ('1', 'NIS001', 'Mahesa', 'Jl. raya cibereuem ', '10A');
INSERT INTO `siswa` VALUES ('2', 'NIS002', 'Kanny', 'Jl. Banda Aceh', '10A');
INSERT INTO `siswa` VALUES ('3', 'NIS003', 'Desta', 'Jl. Lurgung', '10B');
INSERT INTO `siswa` VALUES ('4', 'NIS004', 'Arif', 'Jl. Belitung', '10B');
INSERT INTO `siswa` VALUES ('5', 'NIS005', 'Bons', 'Jl. Bangka', '10C');

Oke setelah database siap selanjutnya kita setting config terlebih dahulu seperti berikut  config nya yang berada di folder app/config/config.php.

<?php
return new \Phalcon\Config(array(
    'database' => array(
        'adapter'     => 'Mysql',
        'host'        => 'localhost',
        'username'    => 'root',
        'password'    => '',
        'dbname'      => 'sekolah',
    ),
    'application' => array(
        'controllersDir' => __DIR__ . '/../../app/controllers/',
        'modelsDir'      => __DIR__ . '/../../app/models/',
        'viewsDir'       => __DIR__ . '/../../app/views/',
        'pluginsDir'     => __DIR__ . '/../../app/plugins/',
        'libraryDir'     => __DIR__ . '/../../app/library/',
        'cacheDir'       => __DIR__ . '/../../app/cache/',
        'baseUri'        => '/tutorial_phalcon_pdf/',

    )
));
Setelah itu kita buat model pada folder model dengan nama Siswa.php
sehingga isi dari Siswa.php adalah :

<?php
class Siswa extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $nis;

    /**
     *
     * @var string
     */
    public $nama;

    /**
     *
     * @var string
     */
    public $alamat;

    /**
     *
     * @var string
     */
    public $kelas;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'nis' => 'nis', 
            'nama' => 'nama', 
            'alamat' => 'alamat', 
            'kelas' => 'kelas'
        );
    }

}

Setelah itu pada folder controller rubah isi dari file IndexController.php dan tambahkan kode berikut pada funcion createpdfAction :

public function createpdfAction()
    {
		$getdata=Siswa::find();
		$this->view->datas=$getdata;

    }
Sehingga isi dari IndexController.php adalah sbb
IndexController.php :

<?php
class IndexController extends ControllerBase
{

    public function indexAction()
    {
		
    }
	public function createpdfAction()
    {
		$getdata=Siswa::find();
		$this->view->datas=$getdata;

    }
}


Setelah itu pada folder views/index kita rubah createpdf.phtml menjadi seperti berikut :
createpdf.phtml :
<?php
class PDF extends FPDF
{

//Page footer
function Footer()
{
    //Position at 1.5 cm from bottom
    $this->SetY(-1);
    //Arial italic 8
    $this->SetFont('helvetica','I',8);
    //Page number
    $this->Cell(0,0.1,'Page '.$this->PageNo().'/{nb}',0,0,'C');
}
}
//Instanciation of inherited class
$pdf=new PDF('P','cm','A4');
$pdf->SetMargins(0.5,1,0.5,1);
$pdf->AliasNbPages();
$pdf->AddPage();

$sekarang=date("Y-m-d H:i:s");
$pdf->SetFont('helvetica','B',10);
$pdf->SetTextColor(0, 94, 105);


	$pdf->Cell(19,0.5,"Laporan Siswa",0,0,'C');
	$pdf->Ln();
	
	$pdf->SetFont('helvetica','',8);								
	$pdf->SetTextColor(0, 0, 0);
	$pdf->SetFillColor(240, 243, 244);
	$pdf->SetDrawColor(203, 207, 210);
	
	
	$pdf->Cell(4.5 , 0.5, '', 0, 'L', 'C',0);	
	$pdf->Cell(0.7 , 0.5, 'No', 1, 'L', 'C',1);
	$pdf->Cell(1.5 , 0.5,'Nis' , 1, 'LR', 'C',1);
	$pdf->Cell(3 , 0.5,'Nama' , 1, 'L', 'C',1);
	$pdf->Cell(4 , 0.5,'Alamat' , 1, 'L', 'C',1);
	$pdf->Cell(1 , 0.5,'Kelas' , 1, 'L', 'C',1);
	
	$no=1;
	foreach($datas as $data) {
		$pdf->SetFont('helvetica','',7);								
		$pdf->SetFillColor(255, 255, 255);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->Ln();
		$pdf->Cell(4.5 , 0.5, '', 0, 'L', 'C',0);	
		$pdf->Cell(0.7 , 0.4, $no, 1, 'L', 'C',1);
		$pdf->Cell(1.5 , 0.4,$data->nis , 1, 'LR', 'C',1);
		$pdf->Cell(3 , 0.4,$data->nama , 1, 'L', 'L',1);
		$pdf->Cell(4 , 0.4,$data->alamat , 1, 'L', 'L',1);
		$pdf->Cell(1 , 0.4,$data->kelas, 1, 'L', 'L',1);
		
		
		
		$no++;
	} 
	
$pdf->Output("Laporan Siswa.pdf","I");
?> 


Oke semuanya sudah selesai selanjtnya langsung buka pada browser anda :
http://localhost/tutorial_phalcon_pdf/index/createpdf

Maka hasilnya adalah sebagai berikut :








Thanks , you are flying with phalcon !!!
Download source lengkap disini :
https://drive.google.com/file/d/0B-NWqiDMG2MQM21zX2t5OC1WeXc/view?usp=sharing

Posting Komentar

0 Komentar