我正在制作一个记录公司员工所有数据和文件的页面,它的部分功能是生成身份证,这是我正在努力解决的部分。
我下载了 PHP Word,首先安装了 composer 并使用它在项目的根目录(php 页面所在的位置)安装 PHPWord 库,它安装了以下文件和文件夹:
-composer.lock -composer.json -vendor(这个是包含所有库的文件夹)
“vendor”子文件夹和文件:-autoload.php -composer(文件夹)-pclzip(文件夹)-phpoffice(文件夹)-zendframework(文件夹)
我认为这是安装它所要做的一切,所以我继续添加代码,问题是它不起作用......这是代码,我添加了注释以进一步解释:
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
require_once 'conector_superadmin.php';
$link = connect($conn);
//Retrieving the id number sent via url to get the employee number
$idSelectedEmployee = $_GET['SelectedEmployee'];
//Data taken from small form with extra information for the ID Card
$idType = $_POST['idType'];
$ValidFrom = $_POST['ValidFrom'];
$ExpiresOn = $_POST['ExpiresOn'];
//Retrieving employee's information from "employees" table in db
$sql = "SELECT * FROM employees WHERE idEmployee = '$idSelectedEmployee'";
$result = mysqli_query($link, $sql);
while($row = mysqli_fetch_array($result)){
$FullName = $row['FullName'];
$Position = $row['Position'];
$EmployeeNum = $row['EmployeeNum'];
$BloodType = $row['BloodType'];
$Alergies = $row['Alergies'];
$PhoneNum = $row['PhoneNum'];
}
//The company uses two kinds of id cards, so i use these conditionals to get the type from a “select” field, it does work, i tested it
if($idType == "Plastic"){
//This code inside however is what isn’t working at all, it won't do anything despite taking this code from their template
include_once 'Sample_Header.php';
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('IDCards/Templates/ID_TEST.docx');
//I am using in the docx file the “${Variable}” format as the template and other documentation explains
$templateProcessor->setValue('EmployeeName', $FullName);
$templateProcessor->setValue('PhoneNum', $PhoneNum);
$templateProcessor->setValue('BloodType', $BloodType);
$templateProcessor->setValue('Alergies', $Alergies);
$templateProcessor->setValue('EmployeeNum', $EmployeeNum);
$templateProcessor->setValue('Position', $Position);
$templateProcessor->setValue('ValidFrom', $ValidFrom);
$templateProcessor->setValue('ExpiresOn', $ExpiresOn);
$templateProcessor->saveAs('IDCards/PlasticID.docx');
}
if($idType == "Paper"){
//Still empty
}
//I created this table to insert the data taken from the “employees’ table, just for the purpose of testing if all the information was retrieved correctly, needless to say, it works fine
$CredencialQuery = "INSERT INTO `idCards` (`idID`, `FullName`, `PhoneNum`, `BloodType`, `Alergies`, `EmployeeNum`, `Position`, `ValidFrom`, `ExpiresOn`, `idType`) VALUES (NULL, '$FullName', '$PhoneNum', '$BloodType', '$Alergies', '$EmployeeNum', '$Position', '$ValidFrom', '$ExpiresOn', '$idType');";
mysqli_query($link, $CredencialQuery);
}
?>
所以是的,我意识到问题本身可能是 PHPWord 代码,要么是我的代码有问题,要么是库没有正确安装,我尝试运行 php 模板本身来测试该模板是否有效,但它也不起作用