PHPOffice/Common using PHPWord in codeigniter not found

3.8k views Asked by At

They have updated the plugin and replace some shared class according to this: https://github.com/PHPOffice/PHPWord/releases and upon upgrading the phpword, I encountered this error.

A PHP Error was encountered

Severity: Error

Message: Class 'PhpOffice\Common\Text' not found

Filename: Element/Text.php

Line Number: 139





<?php
include_once(APPPATH."third_party/PHPWord/Autoloader.php");
if (!defined('BASEPATH'))
    exit('No direct script access allowed');


use PhpOffice\PhpWord\Autoloader;
use PhpOffice\PhpWord\Settings;
Autoloader::register();
Settings::loadConfig();


class Test extends CI_Controller {

    public function index() {
                $phpWord = new \PhpOffice\PhpWord\PhpWord();
                $phpWord->getCompatibility()->setOoxmlVersion(14);
                $phpWord->getCompatibility()->setOoxmlVersion(15);

                $section = $phpWord->addSection();
                $html = '<p><strong style="font-weight: 600;"This is a heading</strong></p>

                $filename = 'test.doc';

                \PhpOffice\PhpWord\Shared\Html::addHtml($section, $html);
                $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
                $objWriter->save($filename);
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.$filename);
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('Content-Length: ' . filesize($filename));
                flush();
                readfile($filename);
                unlink($filename); // deletes the temporary file
                exit;
        }
}

I have use composer and I'm wondering how if to use the common shared library: https://github.com/PHPOffice/Common

Please help.

1

There are 1 answers

0
Tpojka On

Can you ssh your server with terminal to use composer and avoid third party directory instead? I just tried to install PHPWord with composer. Successfully. Common directory is in PHPWord package by default. But I had this issue. It can be solved this way or maybe even better this way. I have used basic code example from PHPWord GitHub page.

It would be best to fork PHPWord into your own repository before fixing that line and than call your repository to avoid future breaks in case of update package. I am not sure why they didn't fix that though.

Follow these 4 steps to be able to reproduce my own use of composer.

Then, your sample class should look something like:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

use PhpOffice\PhpWord\PhpWord;

class Welcome extends CI_Controller
{

    public function index()
    {
        try {
            // code example from GitHub https://github.com/PHPOffice/PHPWord#getting-started
            // without require 'bootstrap.php' line, following 4 steps you already auto-loaded classes
    } catch (\Exception $e) {
          echo $e->getMessage();
    }
}

and you should get helloWorld.docx, helloWorld.odt and helloWorld.html files in FCPATH location.