Jumat, 06 Juni 2014

Creating a PDF file using FPDF

In this post, I will show you how create pdf using FPDF plugin.

First, you must have a fpdf, in this case I have downloaded fpdf17.php and including it into my code. For sure, you can see this example to create your own PDF file.


<?php
require('fpdf17.php');

include "libConnect.php";

class PDF extends FPDF {

    //*START* FOR HTML EXTEND 
    var $B;
    var $I;
    var $U;
    var $HREF;

    function PDF($orientation = 'P', $unit = 'mm', $size = 'A4') {
    // Call parent constructor
    $this->FPDF($orientation, $unit, $size);
    // Initialization
    $this->B = 0;
    $this->I = 0;
    $this->U = 0;
    $this->HREF = '';
    }

    function WriteHTML($html) {
    // HTML parser
    $html = str_replace("\n", ' ', $html);
    $a = preg_split('/<(.*)>/U', $html, -1, PREG_SPLIT_DELIM_CAPTURE);
    foreach ($a as $i => $e) {
        if ($i % 2 == 0) {
        // Text
        if ($this->HREF)
            $this->PutLink($this->HREF, $e);
        else
            $this->Write(5, $e);
        }
        else {
        // Tag
        if ($e[0] == '/')
            $this->CloseTag(strtoupper(substr($e, 1)));
        else {
            // Extract attributes
            $a2 = explode(' ', $e);
            $tag = strtoupper(array_shift($a2));
            $attr = array();
            foreach ($a2 as $v) {
            if (preg_match('/([^=]*)=["\']?([^"\']*)/', $v, $a3))
                $attr[strtoupper($a3[1])] = $a3[2];
            }
            $this->OpenTag($tag, $attr);
        }
        }
    }
    }

    function OpenTag($tag, $attr) {
    // Opening tag
    if ($tag == 'B' || $tag == 'I' || $tag == 'U')
        $this->SetStyle($tag, true);
    if ($tag == 'A')
        $this->HREF = $attr['HREF'];
    if ($tag == 'BR')
        $this->Ln(5);
    }

    function CloseTag($tag) {
    // Closing tag
    if ($tag == 'B' || $tag == 'I' || $tag == 'U')
        $this->SetStyle($tag, false);
    if ($tag == 'A')
        $this->HREF = '';
    }

    function SetStyle($tag, $enable) {
    // Modify style and select corresponding font
    $this->$tag += ($enable ? 1 : -1);
    $style = '';
    foreach (array('B', 'I', 'U') as $s) {
        if ($this->$s > 0)
        $style .= $s;
    }
    $this->SetFont('', $style);
    }

    function PutLink($URL, $txt) {
    // Put a hyperlink
    $this->SetTextColor(0, 0, 255);
    $this->SetStyle('U', true);
    $this->Write(5, $txt, $URL);
    $this->SetStyle('U', false);
    $this->SetTextColor(0);
    }
    //*END* FOR HTML EXTEND 

}

$pdf = new PDF();

// First page
$pdf->AddPage();
$pdf->SetFont('Arial','',20);
$pdf->WriteHTML("<b>Detail Pelabuhan ".$data['nama_pelabuhan']."</b>");
$pdf->ln(21);
$pdf->SetFont('Arial','',11);
$pdf->Write(5,"Provinsi: ".$data['nm_prov']);
$pdf->ln(7);
$pdf->Write(5,"Kabupaten/Kota: ".$data['nm_kabkota']);
$pdf->ln(7);
$pdf->Write(5,"Koordinat Geografis: ".$data['latitude'].", ".$data['longitude']);
$pdf->ln(15);

$pdf->Image('http://sikhubla.net/images/pelabuhan_sample.png');

$pdf->ln(15);

//*START* INFORMATION CONTENT
    $pdf->SetFont('Arial','',16);
    $pdf->WriteHTML("<b>Informasi Umum</b>");
    $pdf->ln(10);
    $pdf->SetFont('Arial','',11);
    $pdf->SetFillColor(230,230,230);

    $ihigh = 7;

    // row 1
    $pdf->Cell(60,$ihigh,'Hierarki:',T,0,'L',true);
    $pdf->Cell(30,$ihigh,$data['hierarki'],T,1,'T',true);

    //row 2
    $pdf->Cell(60,$ihigh,'Penyelenggara:',T,0,'L');
    $pdf->Cell(30,$ihigh,$data['penyelenggara'],T,1,'L');

    //row 3
    $pdf->Cell(60,$ihigh,'Operator:',T,0,'L',true);
    $pdf->Cell(30,$ihigh,$data['operator'],T,1,'L',true);

    //row 4
    $pdf->Cell(30,$ihigh,'Kedalaman:',T,0,'L');
    $pdf->Cell(30,$ihigh,'Alur',T,0,'L');
    $pdf->Cell(30,$ihigh,$data['kedalaman_alur']." m",T,1,'R');

    //row 5
    $pdf->Cell(30,$ihigh,'',0,0,'L');
    $pdf->Cell(30,$ihigh,'Kolam',T,0,'L',true);
    $pdf->Cell(30,$ihigh,$data['kedalaman_kolam']." m",T,1,'R',true);

    //row 6
    $pdf->Cell(60,$ihigh,'Kapasitas',TB,0,'L');
    $pdf->Cell(30,$ihigh,$data['kap_tot']." ton",TB,1,'R');
//*END* INFORMATION CONTENT

$pdf->Output();

FPDF Website: http://www.fpdf.org/ And you can find another script here:

Tidak ada komentar:

Posting Komentar

Cara Mengetahui Besar Database PostgreSQL Tanpa Mendownloadnya

Berikut adalah langkah-langkah untuk mengetahui ukuran semua database di instance PostgreSQL yang berjalan di dalam kontainer Docker: 1. Men...