PHPWord代码无法编辑模板并另存为新文件

时间:2018-07-17 16:15:01

标签: php phpword phpoffice

我正在处理一个页面,其中记录着公司员工的所有数据和文件,其功能的一部分是生成身份证,这是我在努力的部分。

我通过首先安装作曲家并使用它在项目根目录(PHP页面所在的位置)中安装PHPWord库来下载PHP Word,并安装了以下文件和文件夹:

-composer.lock -composer.json -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模板本身以测试该模板是否有效,但它没有要么工作

0 个答案:

没有答案