anouarabdsslm / laravel-fpdf

Fpdf allows to generate PDF files
Apache License 2.0
44 stars 51 forks source link

How to retrieve all data in the database? #4

Closed simpleann closed 10 years ago

simpleann commented 10 years ago

Can I add also diagram on the the fpdf?

anouarabdsslm commented 10 years ago

you can do anything that fpdf library offer, have look here http://www.fpdf.org/?lang=en

simpleann commented 10 years ago

I am using this website as my reference I just want to ask how will I call the class into my route? Or any possible way? Or how will I convert this codes to laravel I don't have any idea:

<?php define('FPDF_FONTPATH','font/'); require('../diag.php');

$pdf = new PDF_Diag(); $pdf->Open(); $pdf->AddPage();

$db = mysql_connect("localhost", "root","") or die(mysql_error());

mysql_select_db("laravel-master",$db) or die(mysql_error());

$sql = mysql_query("SELECT * FROM graph") or die(mysql_error());

$data = array('Men' => 1510, 'Women' => 1610, 'Children' => 1400, 'Adult' => 300, 'sdasd' => 500);

$pdf->SetFont('Arial', '', 10); $valX = $pdf->GetX(); $valY = $pdf->GetY(); $pdf->Cell(30, 5, 'Number of:'); $pdf->Cell(15, 5, $data[$i], 0, 0, 'R'); $pdf->Ln();

//Pie chart $pdf->SetFont('Arial', 'BIU', 12); $pdf->Cell(0, 5, '1 - Pie chart', 0, 1); $pdf->Ln(8); $pdf->Cell(30, 5, 'Number of men:'); $pdf->Cell(15, 5, $data['Men'], 0, 0, 'R'); $pdf->Ln(); $pdf->Cell(30, 5, 'Number of women:'); $pdf->Cell(15, 5, $data['Women'], 0, 0, 'R'); $pdf->Ln(); $pdf->Cell(30, 5, 'Number of children:'); $pdf->Cell(15, 5, $data['Children'], 0, 0, 'R'); $pdf->Ln(); $pdf->Cell(30, 5, 'Number of adult:'); $pdf->Cell(15, 5, $data['Adult'], 0, 0, 'R'); $pdf->Ln(); $pdf->Cell(30, 5, 'Number of sdasd:'); $pdf->Cell(15, 5, $data['sdasd'], 0, 0, 'R'); $pdf->Ln(); $pdf->Ln(8);

$pdf->SetXY(90, $valY); $col1=array(100,100,255); $col2=array(255,100,100); $col3=array(255,255,100); $col4=array(242,219,232); $col5=array(99,33,68); $pdf->PieChart(100, 35, $data, '%l (%p)', array($col1,$col2,$col3,$col4,$col5)); $pdf->SetXY($valX, $valY + 40);

//Bar diagram $pdf->SetFont('Arial', 'BIU', 12); $pdf->Cell(0, 5, '2 - Bar diagram', 0, 1); $pdf->Ln(8); $valX = $pdf->GetX(); $valY = $pdf->GetY(); $pdf->BarDiagram(190, 70, $data, '%l : %v (%p)', array(255,175,100)); $pdf->SetXY($valX, $valY + 80);

$pdf->Output(); ?>

Thank you for the help.

anouarabdsslm commented 10 years ago

Hi dear, Sorry fro the delay , first to retrieve your data from the db use eloquent , and if the package is already installed on your laravel project , I have made some changes to make your code work :

Fpdf::AddPage();

$data = array('Men' => 1510, 'Women' => 1610, 'Children' => 1400, 'Adult' => 300, 'sdasd' => 500);

Fpdf::SetFont('Arial', '', 10);
$valX = Fpdf::GetX();
$valY = Fpdf::GetY();
Fpdf::Cell(30, 5, 'Number of:'); 
Fpdf::Cell(15, 5, $data[$i], 0, 0, 'R'); 
Fpdf::Ln();

//Pie chart
Fpdf::SetFont('Arial', 'BIU', 12);
Fpdf::Cell(0, 5, '1 - Pie chart', 0, 1);
Fpdf::Ln(8);
Fpdf::Cell(30, 5, 'Number of men:');
Fpdf::Cell(15, 5, $data['Men'], 0, 0, 'R'); Fpdf::Ln();
Fpdf::Cell(30, 5, 'Number of women:');
Fpdf::Cell(15, 5, $data['Women'], 0, 0, 'R');
Fpdf::Ln();
Fpdf::Cell(30, 5, 'Number of children:');
Fpdf::Cell(15, 5, $data['Children'], 0, 0, 'R');
Fpdf::Ln();
Fpdf::Cell(30, 5, 'Number of adult:');
Fpdf::Cell(15, 5, $data['Adult'], 0, 0, 'R');
Fpdf::Ln();
Fpdf::Cell(30, 5, 'Number of sdasd:');
Fpdf::Cell(15, 5, $data['sdasd'], 0, 0, 'R');
Fpdf::Ln();
Fpdf::Ln(8);

Fpdf::SetXY(90, $valY);
$col1=array(100,100,255);
$col2=array(255,100,100);
$col3=array(255,255,100);
$col4=array(242,219,232);
$col5=array(99,33,68);
Fpdf::PieChart(100, 35, $data, '%l (%p)', array($col1,$col2,$col3,$col4,$col5));
Fpdf::SetXY($valX, $valY + 40);

//Bar diagram
Fpdf::SetFont('Arial', 'BIU', 12);
Fpdf::Cell(0, 5, '2 - Bar diagram', 0, 1);
Fpdf::Ln(8);
$valX = Fpdf::GetX();
$valY = Fpdf::GetY();
Fpdf::BarDiagram(190, 70, $data, '%l : %v (%p)', array(255,175,100));
Fpdf::SetXY($valX, $valY + 80);

Fpdf::Output();
exit;
simpleann commented 10 years ago

I still get an error.

image

Thank you.

anouarabdsslm commented 10 years ago

well I can see that pdf don't have any method called PieChart , also its may not has BarDiagram, that why you get this error .