I
I
IwanIwanow2021-07-20 14:38:24
PHP
IwanIwanow, 2021-07-20 14:38:24

How to run PhpWord on hosting?

I need PhpWord to create a docx file based on a template.
I download PhpWord from github , press the green "Code" button and select Download ZIP. In the archive, I take the PhpWord folder from the src folder and upload it to the hosting. I upload the template.docx template file to the PhpWord folder, in the text of which, in the right place, the variable ${day} is added - instead of this variable, I am going to insert the date into the template. On the Internet, I found the following code, dated 2017:

require_once('vendor/autoload.php'); // ставится ТОЛЬКО через Composer!
$_doc = new \PhpOffice\PhpWord\TemplateProcessor('Template.docx');
$_doc->setValue('d_num', '10/29-77-Ю'); //номер договора
// вывод непосредственно в браузер
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="dogovor.docx"');
header('Cache-Control: max-age=0');
$_doc->saveAs('php://output');
die;

I have a regular shared hosting, Composer is not for my case, as far as I understand. The code above resulted in this:
<?php
$_doc = new \TemplateProcessor('template.docx');
$_doc->setValue('day', '20'); //день
// вывод непосредственно в браузер
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="template2.docx"');
header('Cache-Control: max-age=0');
$_doc->saveAs('php://output');
die;
?>

I run the file with this code and get the error:
Fatal error: Uncaught Error: Class 'TemplateProcessor' not found in ...

This page mentions the PHP class loader and the code I put in my file:
<?php

$GLOBALS['class_path'] = array(__DIR__ . '/lib', __DIR__);

// Set-up class_path superglobal variable using php include_path as basis
if (!array_key_exists('class_path', $GLOBALS)) {
    $GLOBALS['class_path'] = array();
    foreach (explode(PATH_SEPARATOR, get_include_path()) as $path) {
        // substitute __DIR__ path for '.' instead
        if ($path == '.') {
            array_push( $GLOBALS['class_path'], realpath(__DIR__) );
            continue;
        }
        array_push( $GLOBALS['class_path'], realpath($path) );
    }
}

if (!function_exists('import')):
function import($package = '') {
    if (empty($package)) {
        trigger_error("Package path must be specified.", E_USER_ERROR);
    }
    $package_bits = explode('\\', $package);
    $package_path = implode(DIRECTORY_SEPARATOR, $package_bits) . '.php';
    foreach ($GLOBALS['class_path'] as $path) {
        $file = $path . DIRECTORY_SEPARATOR . $package_path;
        if (file_exists($file)) {
            require_once($file);
            $entity_name = implode('\\', $package_bits);
            if (!(class_exists($entity_name, false) ||
                interface_exists($entity_name, false)
                || trait_exists($entity_name, false))) {
            $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 1)[0];
            trigger_error("Entity '" . $package . "' not found in file '" . $package_path . "' for import called in " .
                    $caller['file'] . " on line " . $caller['line'], E_USER_ERROR);
            }
            return;
        }
    }
}
endif;

spl_autoload_register('import');

$_doc = new \TemplateProcessor('template.docx');
$_doc->setValue('day', '20'); //день
// вывод непосредственно в браузер
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename="template2.docx"');
header('Cache-Control: max-age=0');
$_doc->saveAs('php://output');
die;
?>

The done did not help and the error did not go away. The same page says:
be sure to use "use" as needed before trying to instantiate PHPWord
- how to use it? The information I found dates back to 2014 or 2017 and did not help me, maybe I'm doing something wrong. Please tell me how to run the latest version of PhpWord on shared hosting.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question