You are on page 1of 4

<?

php /* An Example PDF Report Using FPDF by Matt Doyle From "Create Nice-Looking PDFs with PHP and FPDF" http://www.elated.com/articles/create-nice-looking-pdfs-php-fpdf/ */ require_once( "fpdf/fpdf.php" ); // Begin configuration $textColour = array( 0, 0, 0 ); $headerColour = array( 100, 100, 100 ); $tableHeaderTopTextColour = array( 255, 255, 255 ); $tableHeaderTopFillColour = array( 125, 152, 179 ); $tableHeaderTopProductTextColour = array( 0, 0, 0 ); $tableHeaderTopProductFillColour = array( 143, 173, 204 ); $tableHeaderLeftTextColour = array( 99, 42, 57 ); $tableHeaderLeftFillColour = array( 184, 207, 229 ); $tableBorderColour = array( 50, 50, 50 ); $tableRowFillColour = array( 213, 170, 170 ); $reportName = "2009 Widget Sales Report"; $reportNameYPos = 160; $logoFile = "widget-company-logo.png"; $logoXPos = 50; $logoYPos = 108; $logoWidth = 110; $columnLabels = array( "Q1", "Q2", "Q3", "Q4" ); $rowLabels = array( "SupaWidget", "WonderWidget", "MegaWidget", "HyperWidget" ); $chartXPos = 20; $chartYPos = 250; $chartWidth = 160; $chartHeight = 80; $chartXLabel = "Product"; $chartYLabel = "2009 Sales"; $chartYStep = 20000; $chartColours = array( array( array( array( array( ); $data = array( array( array( array( array( ); 255, 100, 100, 255, 100, 255, 100, 255, 100 100 255 100 ), ), ), ),

9940, 10100, 9490, 11730 ), 19310, 21140, 20560, 22590 ), 25110, 26260, 25210, 28370 ), 27650, 24550, 30040, 31980 ),

// End configuration /** Create the title page **/

$pdf = new FPDF( 'P', 'mm', 'A4' ); $pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] ); $pdf->AddPage(); // Logo $pdf->Image( $logoFile, $logoXPos, $logoYPos, $logoWidth ); // Report Name $pdf->SetFont( 'Arial', 'B', 24 ); $pdf->Ln( $reportNameYPos ); $pdf->Cell( 0, 15, $reportName, 0, 0, 'C' ); /** Create the page header, main heading, and intro text **/ $pdf->AddPage(); $pdf->SetTextColor( $headerColour[0], $headerColour[1], $headerColour[2] ); $pdf->SetFont( 'Arial', '', 17 ); $pdf->Cell( 0, 15, $reportName, 0, 0, 'C' ); $pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] ); $pdf->SetFont( 'Arial', '', 20 ); $pdf->Write( 19, "2009 Was A Good Year" ); $pdf->Ln( 16 ); $pdf->SetFont( 'Arial', '', 12 ); $pdf->Write( 6, "Despite the economic downturn, WidgetCo had a strong year. Sale s of the HyperWidget in particular exceeded expectations. The fourth quarter was generally the best performing; this was most likely due to our increased ad spe nd in Q3." ); $pdf->Ln( 12 ); $pdf->Write( 6, "2010 is expected to see increased sales growth as we expand int o other countries." ); /** Create the table **/ $pdf->SetDrawColor( $tableBorderColour[0], $tableBorderColour[1], $tableBorderCo lour[2] ); $pdf->Ln( 15 ); // Create the table header row $pdf->SetFont( 'Arial', 'B', 15 ); // "PRODUCT" cell $pdf->SetTextColor( $tableHeaderTopProductTextColour[0], $tableHeaderTopProductT extColour[1], $tableHeaderTopProductTextColour[2] ); $pdf->SetFillColor( $tableHeaderTopProductFillColour[0], $tableHeaderTopProductF illColour[1], $tableHeaderTopProductFillColour[2] ); $pdf->Cell( 46, 12, " PRODUCT", 1, 0, 'L', true ); // Remaining header cells $pdf->SetTextColor( $tableHeaderTopTextColour[0], $tableHeaderTopTextColour[1], $tableHeaderTopTextColour[2] ); $pdf->SetFillColor( $tableHeaderTopFillColour[0], $tableHeaderTopFillColour[1], $tableHeaderTopFillColour[2] );

for ( $i=0; $i<count($columnLabels); $i++ ) { $pdf->Cell( 36, 12, $columnLabels[$i], 1, 0, 'C', true ); } $pdf->Ln( 12 ); // Create the table data rows $fill = false; $row = 0; foreach ( $data as $dataRow ) { // Create the left header cell $pdf->SetFont( 'Arial', 'B', 15 ); $pdf->SetTextColor( $tableHeaderLeftTextColour[0], $tableHeaderLeftTextColour[ 1], $tableHeaderLeftTextColour[2] ); $pdf->SetFillColor( $tableHeaderLeftFillColour[0], $tableHeaderLeftFillColour[ 1], $tableHeaderLeftFillColour[2] ); $pdf->Cell( 46, 12, " " . $rowLabels[$row], 1, 0, 'L', $fill ); // Create the data cells $pdf->SetTextColor( $textColour[0], $textColour[1], $textColour[2] ); $pdf->SetFillColor( $tableRowFillColour[0], $tableRowFillColour[1], $tableRowF illColour[2] ); $pdf->SetFont( 'Arial', '', 15 ); for ( $i=0; $i<count($columnLabels); $i++ ) { $pdf->Cell( 36, 12, ( '$' . number_format( $dataRow[$i] ) ), 1, 0, 'C', $fil l ); } $row++; $fill = !$fill; $pdf->Ln( 12 ); } /*** Create the chart ***/ // Compute the X scale $xScale = count($rowLabels) / ( $chartWidth - 40 ); // Compute the Y scale $maxTotal = 0; foreach ( $data as $dataRow ) { $totalSales = 0; foreach ( $dataRow as $dataCell ) $totalSales += $dataCell; $maxTotal = ( $totalSales > $maxTotal ) ? $totalSales : $maxTotal; } $yScale = $maxTotal / $chartHeight; // Compute the bar width $barWidth = ( 1 / $xScale ) / 1.5;

// Add the axes: $pdf->SetFont( 'Arial', '', 10 ); // X axis $pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + $chartWidth, $chartYPos ); for ( $i=0; $i < count( $rowLabels ); $i++ ) { $pdf->SetXY( $chartXPos + 40 + $i / $xScale, $chartYPos ); $pdf->Cell( $barWidth, 10, $rowLabels[$i], 0, 0, 'C' ); } // Y axis $pdf->Line( $chartXPos + 30, $chartYPos, $chartXPos + 30, $chartYPos - $chartHei ght - 8 ); for ( $i=0; $i <= $maxTotal; $i += $chartYStep ) { $pdf->SetXY( $chartXPos + 7, $chartYPos - 5 - $i / $yScale ); $pdf->Cell( 20, 10, '$' . number_format( $i ), 0, 0, 'R' ); $pdf->Line( $chartXPos + 28, $chartYPos - $i / $yScale, $chartXPos + 30, $char tYPos - $i / $yScale ); } // Add the axis labels $pdf->SetFont( 'Arial', 'B', 12 ); $pdf->SetXY( $chartWidth / 2 + 20, $chartYPos + 8 ); $pdf->Cell( 30, 10, $chartXLabel, 0, 0, 'C' ); $pdf->SetXY( $chartXPos + 7, $chartYPos - $chartHeight - 12 ); $pdf->Cell( 20, 10, $chartYLabel, 0, 0, 'R' ); // Create the bars $xPos = $chartXPos + 40; $bar = 0; foreach ( $data as $dataRow ) { // Total up the sales figures for this product $totalSales = 0; foreach ( $dataRow as $dataCell ) $totalSales += $dataCell; // Create the bar $colourIndex = $bar % count( $chartColours ); $pdf->SetFillColor( $chartColours[$colourIndex][0], $chartColours[$colourIndex ][1], $chartColours[$colourIndex][2] ); $pdf->Rect( $xPos, $chartYPos - ( $totalSales / $yScale ), $barWidth, $totalSa les / $yScale, 'DF' ); $xPos += ( 1 / $xScale ); $bar++; } /*** Serve the PDF ***/ $pdf->Output( "report.pdf", "I" ); ?>

You might also like