PHP Script for Dynamic PDF Generation of Order Details
Database Connection and Query
This script generates a PDF report of orders by connecting to a MySQL database and fetching data. It uses the FPDF library for PDF creation.
First, it establishes a connection to the MySQL database:
require('AyudaPDF/fpdf/fpdf.php');
$dbc = mysqli_connect('localhost', 'root', 'root', 'birt') or die('Could not connect to the database: ' . mysqli_connect_error());
Then, it defines an SQL query to retrieve order details:
$sentencia = "SELECT orderNumber, orderDate, requiredDate, status, customerName FROM orders, customers WHERE orders.customerNumber = customers.customerNumber";
$resultado = mysqli_query($dbc, $sentencia);
PDF Class Definition
A custom PDF class extends the base FPDF class:
class PDF extends FPDF {
Footer Function
function Footer() {
$this->SetFont('Arial', '', 10);
$this->SetY(-15);
$this->Cell(0, 10, utf8_decode('Page ') . $this->PageNo(), 0, 0, 'R');
$this->SetY(-15);
$this->Cell(0, 10, utf8_decode('This is an IT example'), 0, 0, 'C');
}
Table Function
function tabla($datos) {
$this->SetFont('Arial', '', 20);
$this->SetFillColor(19, 184, 234);
$this->SetX(55);
$this->Cell(100, 15, utf8_decode("Order NÂș " . $datos[0]), 0, 1, "C", true);
$this->Ln();
$this->SetFont('Courier', 'B', 12);
$this->Cell(50, 10, utf8_decode("Order Date: "), 0, 0, "L");
$this->SetFont('Courier', '', 12);
$this->Cell(80, 10, utf8_decode($datos[1]), 0, 1, "R");
$this->SetFont('Courier', 'B', 12);
$this->Cell(50, 10, utf8_decode("Required Date: "), 0, 0, "L");
$this->SetFont('Courier', '', 12);
$this->Cell(80, 10, utf8_decode($datos[2]), 0, 1, "R");
$this->SetFont('Courier', 'B', 12);
$this->Cell(50, 10, utf8_decode("Status: "), 0, 0, "L");
$this->SetFont('Courier', '', 12);
$this->Cell(80, 10, utf8_decode($datos[3]), 0, 1, "R");
$this->SetFont('Courier', 'B', 12);
$this->Cell(50, 10, utf8_decode("Customer Name: "), 0, 0, "L");
$this->SetFont('Courier', '', 12);
$this->Cell(80, 10, utf8_decode($datos[4]), 0, 1, "R");
$this->Ln();
}
Table Header Function
function tabla_encabezado() {
$this->SetFont('Arial', 'B', 12);
$this->SetFillColor(19, 184, 234);
$this->Cell(25, 10, utf8_decode("Code"), 1, 0, "C", true);
$this->Cell(110, 10, utf8_decode("Name"), 1, 0, "C", true);
$this->Cell(20, 10, utf8_decode("Price"), 1, 0, "C", true);
$this->Cell(20, 10, utf8_decode("Quantity"), 1, 0, "C", true);
$this->Cell(15, 10, utf8_decode("Line"), 1, 0, "C", true);
$this->Ln();
}
Table Data Function
function tabla2($datos1) {
$this->SetFont('Courier', '', 12);
$this->Cell(25, 10, $datos1[0], 1, 0, "C");
$this->Cell(110, 10, $datos1[1], 1, 0, "C");
$this->Cell(20, 10, $datos1[2], 1, 0, "C");
$this->Cell(20, 10, $datos1[3], 1, 0, "C");
$this->Cell(15, 10, $datos1[4], 1, 0, "C");
$this->Ln();
}
}
PDF Generation
Finally, the script creates a PDF object, adds a page, and iterates through the query results to populate the PDF:
$pdf = new PDF();
$pdf->AddPage();
while ($row = mysqli_fetch_array($resultado)) {
$pdf->Ln();
$pdf->tabla($row);
$pdf->tabla_encabezado();
$sentencia2 = "SELECT products.productCode, productName, priceEach, quantityOrdered, orderLineNumber FROM orderdetails, products WHERE orderdetails.productCode = products.productCode AND orderNumber='$row[0]'";
$resultado2 = mysqli_query($dbc, $sentencia2);
while ($row2 = mysqli_fetch_array($resultado2)) {
$pdf->tabla2($row2);
}
}
$pdf->Output();
This script effectively demonstrates how to use PHP and the FPDF library to generate dynamic PDF reports from a MySQL database.