<?php
// 1. 從數(shù)據(jù)庫(kù)查詢(xún)訂單數(shù)據(jù)
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("連接數(shù)據(jù)庫(kù)失敗: " . $conn->connect_error);
}
$sql = "SELECT * FROM orders";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// 2. 構(gòu)建發(fā)貨單內(nèi)容
$orderNumber = $row['order_number'];
$customerName = $row['customer_name'];
$shippingAddress = $row['shipping_address'];
// 其他字段根據(jù)需要自行添加
$invoiceContent = "<h1>發(fā)貨單</h1>";
$invoiceContent .= "<p>訂單號(hào):" . $orderNumber . "</p >";
$invoiceContent .= "<p>收貨人姓名:" . $customerName . "</p >";
$invoiceContent .= "<p>收貨地址:" . $shippingAddress . "</p >";
// 其他字段也可以在此處添加到發(fā)貨單內(nèi)容中
// 3. 打印發(fā)貨單
$printerDevice = "TP874VHH172"; // 打印機(jī)設(shè)備名稱(chēng)
$printerHandle = printer_open($printerDevice);
if (!$printerHandle) {
echo "無(wú)法打開(kāi)打印機(jī),請(qǐng)檢查打印機(jī)驅(qū)動(dòng)程序是否正確安裝。";
exit;
}
$printerStatus = printer_status($printerHandle);
if ($printerStatus['status'] == PRINTER_STATUS_ERROR) {
echo "打印機(jī)錯(cuò)誤,請(qǐng)檢查打印機(jī)是否正常工作。";
exit;
}
// 設(shè)置打印機(jī)參數(shù),如紙張類(lèi)型、對(duì)齊方式等
printer_set_option($printerHandle, PRINTER_MODE, "RAW");
printer_set_option($printerHandle, PRINTER_TEXT_ALIGN, PRINTER_TA_LEFT);
// 開(kāi)始打印
printer_start_doc($printerHandle, "發(fā)貨單");
printer_start_page($printerHandle);
printer_set_option($printerHandle, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_CUSTOM);
printer_set_option($printerHandle, PRINTER_PAPER_WIDTH, 80); // 假設(shè)紙張寬度為80字符
printer_write($printerHandle, $invoiceContent);
printer_end_page($printerHandle);
printer_end_doc($printerHandle);
printer_close($printerHandle);
// 4. 將發(fā)貨單保存為文件并發(fā)送到指定位置
$fileLocation = "path/to/save/invoice_" . $orderNumber . ".pdf";
$pdf = new TCPDF(); // 假設(shè)您已經(jīng)安裝了TCPDF庫(kù)
$pdf->AddPage();
$pdf->writeHTML($invoiceContent);
$pdf->Output($fileLocation, 'F');
// 發(fā)送到指定位置(假設(shè)通過(guò)電子郵件發(fā)送)
$recipientEmail = "[email protected]";
$subject = "發(fā)貨單";
$message = "請(qǐng)查收附件中的發(fā)貨單。";
$attachment = $fileLocation;
$headers = "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";
$fileContent = file_get_contents($attachment);
$fileType = mime_content_type($attachment);
$fileName = basename($attachment);
$message .= "--boundary\r\n";
$message .= "Content-Type: " . $fileType . "; name=\"" . $fileName . "\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment; filename=\"" . $fileName . "\"\r\n\r\n";
$message .= chunk_split(base64_encode($fileContent)) . "\r\n";
$message .= "--boundary--";
mail($recipientEmail, $subject, $message, $headers);
}
} else {
echo "沒(méi)有找到訂單數(shù)據(jù)。";
}
$conn->close();
?>
怎么用我也不會(huì)。有會(huì)的來(lái)教一下。媽的二開(kāi)的跑路了,工程爛尾了