Ghufron Asadly. Diberdayakan oleh Blogger.

membuat pdf pada Cakephp dengan FPDF

Kamis, 05 November 2015

cara membuat pdf di cakephp dowload dulu fpdfnya disini
langkahnya :
1.taruh library fpdf di app/vendor
2.buat layout baru yaitu pdf.ctp dan isikan kode berikut
<?php
    header('Content-Disposition: attachment; filename="downloaded.pdf"');
    echo $content_for_layout;

 ?>
3.buat function di controller pdf() seperti dibawah ini
public function pdf(){
            App::import('Vendor', 'Fpdf', array('file' => 'fpdf/fpdf.php'));
            $this->layout = 'pdf'; //this will use the pdf.ctp layout
            $this->set('fpdf', new FPDF('P', 'mm', 'A4'));
            $data=$this->Post->find('first', array('conditions' => array('Post.id' => '14')));
            $this->set(compact('data'));
            $this->render('pdf');
    }
4.buat view sesuai controllernya pdf.ctp
<?php
$judul = "Halo apa kabar";
        $header = array(
            array("label" => "NIM", "length" => 30, "align" => "L"),
array("label" => "NAMA", "length" => 30, "align" => "L"),
            array("label" => "ALAMAT", "length" => 30, "align" => "L"),
            array("label" => "ALAMAT", "length" => 30, "align" => "L"),
            array("label" => "ALAMAT", "length" => 30, "align" => "L"),
            array("label" => "TGL LAHIR", "length" => 30, "align" => "L")
        );
$pdf = new FPDF();
        $fpdf->AddPage();
        #tampilkan judul
        $fpdf->SetFont('Arial', 'B', 16);
        $fpdf->Cell(10, 10, $judul);
        #buat header tabel
        $fpdf->SetFont('Arial', '', '10');
        $fpdf->SetFillColor(255, 0, 0);
        $fpdf->SetTextColor(255);
        $fpdf->SetDrawColor(128, 0, 0);
        foreach ($header as $kolom) {
//            debug($kolom);exit;
            $fpdf->Cell($kolom['length'], 5, $kolom['label'], 1, '0', $kolom['align'], true);
        }
        $fpdf->Ln();
        #tampilkan data tabelnya
        $fpdf->SetFillColor(224, 235, 255);
        $fpdf->SetTextColor(0);
        $fpdf->SetFont('');
        $fill = false;
        foreach ($data as $baris) {
            $i = 0;
            foreach ($baris as $cell) {
//                debug($header);exit;
//                $fpdf->Cell($header[$i]['length'], 5, $cell, 1, '0', $kolom['align'], $fill);
                $fpdf->Cell(30, 5, $cell, 1, '0', $kolom['align'], $fill);
                $i++;
            }
            $fill = !$fill;
            $fpdf->Ln();
        }
        
        
        $fpdf->Output();
        ?>

Tidak ada komentar:

Posting Komentar

 

Most Reading