0

What is the return value of $template_processor->saveAs()? , i tried to catch the return value to determine whether the process is successfully executed or not

public function create_spm($data_spm){
    $templateFile = FCPATH.'upload_file/template_spm/template_spm.docx';
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $template_file = $templateFile;
    $template_processor = new \PhpOffice\PhpWord\TemplateProcessor($template_file);
    $template_processor->setValues($data_spm);
    $output_file = FCPATH.'upload_file/spm/spm.docx';
    $hasil=$template_processor->saveAs($output_file);
    var_dump($hasil);
    exit();
}

It's always return NULL

I've tried to catch the return value of $template_processor->saveAs() but it's always returning NULL

1 Answers1

0

You get NULL because the function doesn't return anything based on the source code :

/**
 * Saves the result document to the user defined file.
 *
 * @since 0.8.0
 *
 * @param string $fileName
 */
public function saveAs($fileName): void
{
    $tempFileName = $this->save();

    if (file_exists($fileName)) {
        unlink($fileName);
    }

    /*
     * Note: we do not use `rename` function here, because it loses file ownership data on Windows platform.
     * As a result, user cannot open the file directly getting "Access denied" message.
     *
     * @see https://github.com/PHPOffice/PHPWord/issues/532
     */
    copy($tempFileName, $fileName);
    unlink($tempFileName);
}

For successfully execute checking wise, I do a checking whether the file exist after saveAS($output_file) using file_exists($output_file)

Hope this helps and Happy coding!

Zeikman
  • 669
  • 5
  • 10