在找不到codeigniter的PHPWord中使用PHPOffice / Common

时间:2017-08-30 13:45:00

标签: php codeigniter phpword phpoffice

他们更新了插件并根据这个替换了一些共享类:https://github.com/PHPOffice/PHPWord/releases 在升级phpword时,我遇到了这个错误。

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;
        }
}

我使用了作曲家,我想知道如何使用公共共享库:https://github.com/PHPOffice/Common

请帮忙。

1 个答案:

答案 0 :(得分:0)

你能用终端ssh你的服务器来使用composer并避免使用第三方目录吗?我只是尝试用composer安装PHPWord。成功。默认情况下,Common目录在PHPWord包中。但我有this issue。它可以解决this way或甚至更好this way。 我使用了PHPWord GitHub页面中的基本代码示例。

最好在修复该行之前将PHPWord分割到您自己的存储库中,然后调用存储库以避免在更新包的情况下将来中断。 我不知道他们为什么不解决这个问题。

关注these 4 steps,以便能够重现我自己对作曲家的使用。

然后,您的示例类应该类似于:

<?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();
    }
}

您应该在helloWorld.docx位置获取helloWorld.odthelloWorld.htmlFCPATH个文件。