How can PHP be used to automate the process of generating printable vouchers from MySQL data for a bond printer?

To automate the process of generating printable vouchers from MySQL data for a bond printer using PHP, we can query the database for the voucher information, format it into a printable document, and send it to the printer using a library like TCPDF.

<?php
// Connect to MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);

// Query database for voucher data
$sql = "SELECT * FROM vouchers";
$result = $conn->query($sql);

// Generate printable document using TCPDF
require('tcpdf/tcpdf.php');
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', '', 12);

// Loop through voucher data and add to PDF
while($row = $result->fetch_assoc()) {
    $pdf->Cell(0, 10, $row['voucher_code'], 0, 1);
}

// Output PDF to printer
$printer_ip = "192.168.1.100";
$printer_port = 9100;
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $printer_ip, $printer_port);
socket_write($socket, $pdf->Output('voucher.pdf', 'S'), strlen($pdf->Output('voucher.pdf', 'S')));
socket_close($socket);

// Close database connection
$conn->close();
?>