打印快遞單有個特點:
被打印紙的背景是固定的,

你只能 在合適的位置輸入快遞單的內容,操作步驟如下:
1、制作 word 模板
參考文章 “圖解如何用打印機套打快遞單”
2、在 模板 中放置“占位符”
打開上面定制好的模板,在 文本輸入框 中輸入 占位符 文本,如:
用戶名:${UserName}
身份證:${IDNo}
效果圖如下:【注意:打印的時候,需要把背景圖刪除!】

這些占位符定義規則,是根據 PHPWord 庫定義的,官方教程:
http://phpword.readthedocs.io/en/latest/templates-processing.html?highlight=replace
利用 PHPWord 庫,可用動態地 修改替換占位符的內容,參考代碼如下:
use common\library\PhpWord\Settings;
use common\library\PhpWord\TemplateProcessor;
public function test() {
// 模板文件
$tplFile = DATA_PATH . '/contract/template_1.docx';
// 輸出 word 文件名
$fileName = 'phpgo的購卡合同.docx';
// 實例化 模板器
Settings::setOutputEscapingEnabled(true);
$templateProcessor = new TemplateProcessor($tplFile);
// 替換 關鍵字
$templateProcessor->setValue('UserName', '劉德花22');
$templateProcessor->setValue('IDNo', '362422199607020812');
$templateProcessor->setValue('Sex', '女');
// 自動輸出下載 word 文件
$tempFileName = $templateProcessor->save();
$docxData = file_read($tempFileName);
unlink($tempFileName);
ob_start();
header("Cache-Control: public");
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
if (strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE')) {
header('Content-Disposition: attachment; filename=' . $fileName);
} else if (strpos($_SERVER["HTTP_USER_AGENT"], 'Firefox')) {
header('Content-Disposition: attachment; filename=' . $fileName);
} else {
header('Content-Disposition: attachment; filename=' . $fileName);
}
header("Pragma:no-cache");
header("Expires:0");
echo $docxData;
ob_end_flush();
}
