Last active
August 9, 2017 07:54
-
-
Save tunathoni/9070d44d63ae681713a2613173ee29aa to your computer and use it in GitHub Desktop.
fpdf example table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "connect.php"; | |
require 'dompdf/dompdf_config.inc.php'; | |
$dompdf = new DOMPDF(); | |
$sql = "SELECT posts.title, users.name, posts.description from posts, users | |
WHERE posts.id_user = users.id_user | |
order by posts.id_blog"; | |
$data_db = mysqli_query($conn, $sql); | |
$str_html = '<table border = "1">'; | |
$str_html .= '<tr> | |
<td>Judul</td> | |
<td>Penulis</td> | |
<td>Deskripsi</td> | |
</tr> | |
'; | |
while ($row = mysqli_fetch_array($data_db)) { | |
$str_html .= '<tr> | |
<td>'.$row['title'].'</td> | |
<td>'.$row['name'].'</td> | |
<td>'.$row['description'].'</td> | |
</tr> | |
'; | |
} | |
$str_html .= '</table>'; | |
$dompdf->load_html($str_html); | |
// (Optional) Setup the paper size and orientation | |
// Render the HTML as PDF | |
$dompdf->render(); | |
// Output the generated PDF to Browser | |
$dompdf->stream('testing.pdf'); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "connect.php"; | |
require('fpdf/fpdf.php'); | |
class PDF extends FPDF | |
{ | |
// Load data | |
function LoadData($file) | |
{ | |
// Read file lines | |
$lines = file($file); | |
$data = array(); | |
foreach($lines as $line) | |
$data[] = explode(';',trim($line)); | |
return $data; | |
} | |
// Simple table | |
function BasicTable($header, $data) | |
{ | |
// Header | |
foreach($header as $col) | |
$this->Cell(60,7,$col,1); | |
$this->Ln(); | |
// Data | |
foreach($data as $row) | |
{ | |
foreach($row as $col) | |
$this->Cell(60,6,$col,1); | |
$this->Ln(); | |
} | |
} | |
// Better table | |
function ImprovedTable($header, $data) | |
{ | |
// Column widths | |
$w = array(40, 35); | |
// Header | |
for($i=0;$i<count($header);$i++) | |
$this->Cell($w[$i],7,$header[$i],1,0,'C'); | |
$this->Ln(); | |
// Data | |
foreach($data as $row) | |
{ | |
$this->Cell($w[0],6,$row[0],'LR'); | |
$this->Cell($w[1],6,$row[1],'LR'); | |
// $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R'); | |
// $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R'); | |
$this->Ln(); | |
} | |
// Closing line | |
$this->Cell(array_sum($w),0,'','T'); | |
} | |
// Colored table | |
function FancyTable($header, $data) | |
{ | |
// Colors, line width and bold font | |
$this->SetFillColor(255,0,0); | |
$this->SetTextColor(255); | |
$this->SetDrawColor(128,0,0); | |
$this->SetLineWidth(.3); | |
$this->SetFont('','B'); | |
// Header | |
$w = array(40, 35); | |
for($i=0;$i<count($header);$i++) | |
$this->Cell($w[$i],7,$header[$i],1,0,'C',true); | |
$this->Ln(); | |
// Color and font restoration | |
$this->SetFillColor(224,235,255); | |
$this->SetTextColor(0); | |
$this->SetFont(''); | |
// Data | |
$fill = false; | |
foreach($data as $row) | |
{ | |
$this->Cell($w[0],6,$row[0],'LR',0,'L',$fill); | |
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill); | |
// $this->Cell($w[2],6,number_format($row[2]),'LR',0,'R',$fill); | |
// $this->Cell($w[3],6,number_format($row[3]),'LR',0,'R',$fill); | |
$this->Ln(); | |
$fill = !$fill; | |
} | |
// Closing line | |
$this->Cell(array_sum($w),0,'','T'); | |
} | |
} | |
$pdf = new PDF(); | |
// Column headings | |
// Data loading | |
// $data = $pdf->LoadData('countries.txt'); | |
$sql = "SELECT posts.title, users.name, posts.description from posts, users | |
WHERE posts.id_user = users.id_user | |
order by posts.id_blog"; | |
$data_db = mysqli_query($conn, $sql); | |
$data = []; | |
while ($row = mysqli_fetch_array($data_db)) { | |
$data[] = [$row['title'], $row['name'], $row['description']]; | |
} | |
$header = array('Title', 'Creator', 'Description'); | |
$pdf->SetFont('Arial','',14); | |
$pdf->AddPage(); // untuk halaman baru | |
$pdf->BasicTable($header,$data); | |
// $pdf->AddPage(); | |
// $pdf->ImprovedTable($header,$data); | |
// $pdf->AddPage(); | |
// $pdf->FancyTable($header,$data); | |
$pdf->Output(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment