随机产生字符串,用phpword修改模板,$airlinePnr和$iePnr两处是空的,而其他的值显示是正常的,请问是什么原因?
代码如下:
<?php
function pnrGenerate() {
$characters = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 6; $i++) {
$index = rand(0, strlen($characters) - 1);
$randomString .= $characters[$index];
}
echo $randomString;
}
// Importing the required dependencies
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\TemplateProcessor;
// Set the path for the PDF renderer library
$rendererLibraryPath = realpath(__DIR__ . '/vendor/mpdf/mpdf');
// var_dump($rendererLibraryPath); //返回 bool(false),命令行 composer require mpdf/mpdf
\PhpOffice\PhpWord\Settings::setPdfRendererPath($rendererLibraryPath);
\PhpOffice\PhpWord\Settings::setPdfRendererName('MPDF');
$passportNo = ‘EK3478566’;
$name = 'XUE/XING';
$title = 'MR';
$airlinePnr = pnrGenerate();
$iePnr = pnrGenerate();
echo $airlinePnr. '</br>';
echo $iePnr. '</br>';
$etkt = mt_rand(1000000000, 9999999999);
echo $etkt. '</br>';
$date = date('dMY');
$date = str_replace('20', '', $date);
$date = strtoupper($date);
echo $date. '</br>';
// Load the template file in DOCX format
// $template = IOFactory::load('template1.docx');
$template = new TemplateProcessor('template1.docx');
// Replace the placehoders in the template file with the extracted information
$template->setValue('AIRLINEPNR', $airlinePnr);
$template->setValue('IEPNR', $iePnr);
$template->setValue('NAME', $name);
$template->setValue('TITLE', $title);
$template->setValue('ETKT', $etkt);
$template->setValue('PASSPORTNO', $passportNo);
$template->setValue('DATE', $date);
// Create a new PDF writer
// $xmlWriter = IOFactory::createWriter($template, 'PDF');
// Save the PDF file
// $xmlWriter->save('document.pdf');
// Save the filled template as a new file in DOCX format
$template->saveAs('output.docx');
?>
模板文件内容如下:
成品文件内容如下: