dqkyz02602 2017-08-30 13:44
浏览 193

PHPOffice / Common使用PHPWord找不到codeigniter

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条回答 默认 最新

  • dptn69182 2017-08-30 17:47
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器