How can PHP be used to extract data from a MySQL database and convert it into a PDF document?

To extract data from a MySQL database and convert it into a PDF document using PHP, you can use the TCPDF library. First, you need to establish a connection to the MySQL database and retrieve the data you want. Then, you can use TCPDF to create a PDF document and populate it with the retrieved data.

<?php
require('tcpdf/tcpdf.php');

// Connect to MySQL database
$mysqli = new mysqli("localhost", "username", "password", "database");

// Check connection
if($mysqli === false){
    die("ERROR: Could not connect. " . $mysqli->connect_error);
}

// Retrieve data from database
$result = $mysqli->query("SELECT * FROM table");

$pdf = new TCPDF();
$pdf->SetMargins(15, 15, 15);
$pdf->AddPage();

// Add data to PDF
while ($row = $result->fetch_assoc()) {
    $pdf->Write(0, $row['column1'] . ' - ' . $row['column2'] . "\n");
}

// Output PDF
$pdf->Output('data.pdf', 'D');

// Close connection
$mysqli->close();
?>