<?php
// 1. 从数据库查询订单数据
$servername = "localhost";
$username = "your_username";
$password = "your_password";
$dbname = "your_database";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接数据库失败: " . $conn->connect_error);
}
$sql = "SELECT * FROM orders";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
// 2. 构建发货单内容
$orderNumber = $row['order_number'];
$customerName = $row['customer_name'];
$shippingAddress = $row['shipping_address'];
// 其他字段根据需要自行添加
$invoiceContent = "<h1>发货单</h1>";
$invoiceContent .= "<p>订单号:" . $orderNumber . "</p >";
$invoiceContent .= "<p>收货人姓名:" . $customerName . "</p >";
$invoiceContent .= "<p>收货地址:" . $shippingAddress . "</p >";
// 其他字段也可以在此处添加到发货单内容中
// 3. 打印发货单
$printerDevice = "TP874VHH172"; // 打印机设备名称
$printerHandle = printer_open($printerDevice);
if (!$printerHandle) {
echo "无法打开打印机,请检查打印机驱动程序是否正确安装。";
exit;
}
$printerStatus = printer_status($printerHandle);
if ($printerStatus['status'] == PRINTER_STATUS_ERROR) {
echo "打印机错误,请检查打印机是否正常工作。";
exit;
}
// 设置打印机参数,如纸张类型、对齐方式等
printer_set_option($printerHandle, PRINTER_MODE, "RAW");
printer_set_option($printerHandle, PRINTER_TEXT_ALIGN, PRINTER_TA_LEFT);
// 开始打印
printer_start_doc($printerHandle, "发货单");
printer_start_page($printerHandle);
printer_set_option($printerHandle, PRINTER_PAPER_FORMAT, PRINTER_FORMAT_CUSTOM);
printer_set_option($printerHandle, PRINTER_PAPER_WIDTH, 80); // 假设纸张宽度为80字符
printer_write($printerHandle, $invoiceContent);
printer_end_page($printerHandle);
printer_end_doc($printerHandle);
printer_close($printerHandle);
// 4. 将发货单保存为文件并发送到指定位置
$fileLocation = "path/to/save/invoice_" . $orderNumber . ".pdf";
$pdf = new TCPDF(); // 假设您已经安装了TCPDF库
$pdf->AddPage();
$pdf->writeHTML($invoiceContent);
$pdf->Output($fileLocation, 'F');
// 发送到指定位置(假设通过电子邮件发送)
$recipientEmail = "recipient@example.com";
$subject = "发货单";
$message = "请查收附件中的发货单。";
$attachment = $fileLocation;
$headers = "From: your_email@example.com\r\n";
$headers .= "Reply-To: your_email@example.com\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 "没有找到订单数据。";
}
$conn->close();
?>
怎么用我也不会。有会的来教一下。妈的二开的跑路了,工程烂尾了
{{item.user_info.nickname ? item.user_info.nickname : item.user_name}}
作者 管理员 企业
{{itemf.name}}
{{itemc.user_info.nickname}}
{{itemc.user_name}}
回复 {{itemc.comment_user_info.nickname}}
{{itemf.name}}