安装PHPword的问题

时间:2016-07-15 19:51:22

标签: php json

我在过去4个小时内一直试图安装PHPWord并且没有运气。我已尝试过所有安装方法,包括编辑器以及下载文件夹本身。每当我运行php文件时,它总会返回错误"致命错误:Class' PhpWord'在第9行和第34行的/home/ubuntu/workspace/hello-world.php中找不到;此外,似乎每当我在composer.json文件中附加phpoffice / phpword时,它都会一直给我一个安装错误,说明没有指定版本。顺便说一句,我在云托管网站(C9.io)上运行这些文件。

我已经附上了我的composer.json以及hello_world.php

非常感谢任何帮助。

hello_world.php

<?php
require_once('PHPWord-master/Autoloader.php');
\PhpOffice\PhpWord\Autoloader::register();

//use PhpOffice\PhpWord\PhpWord;
//use PhpOffice\PhpWord\Style\Font;
// Creating the new document...
$phpWord = new PhpWord();

/* Note: any element you append to a document must reside inside of a Section. */


Note: it's possible to customize font style of the Text element you add in     three ways:
- inline;
- using named font style (new font style object will be implicitly created);
- using explicitly created font style object. */
// Adding Text element with font customized inline...
$section->addText(
htmlspecialchars(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)'
),
array('name' => 'Tahoma', 'size' => 10)
);

// Adding Text element with font customized using named font style...
$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
$fontStyleName,
array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
);
$section->addText(
htmlspecialchars(
'"The greatest accomplishment is not in never falling, '
. 'but in rising again after you fall." '
. '(Vince Lombardi)'
),
$fontStyleName
);

// Adding Text element with font customized using explicitly created font      style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText(
htmlspecialchars('"Believe you can and you\'re halfway there." (Theodor    Roosevelt)')
);
$myTextElement->setFontStyle($fontStyle);

// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');

// Saving the document as ODF file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'ODText');
 $objWriter->save('helloWorld.odt');

// Saving the document as HTML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
$objWriter->save('helloWorld.html');

/* Note: we skip RTF, because it's not XML-based and requires a different     example. /
/ Note: we skip PDF, because "HTML-to-PDF" approach is used to create PDF documents. */
?> 

这是composer.json需要部分

"require": 
{
"php": ">=5.3.3",
"ext-xml": "*",
"phpoffice/phpword":"dev-master"
},

1 个答案:

答案 0 :(得分:7)

看起来它已正确安装,因为您已经越过了自动加载器语句。

问题是PhpWord不是类,它是\PhpOffice\PhpWord\PhpWord,因为它是命名空间。

所以你可以尝试:

$phpWord = new \PhpOffice\PhpWord\PhpWord();

或将use语句添加回已注释掉的脚本顶部:

use PhpOffice\PhpWord\PhpWord;

这将允许您将该类称为PhpWord,而不是完全命名空间版本。

编辑:

试试这个:

从命令行(你可能已经这样做了):

composer require phpoffice/phpword

使用内容创建文件test.php:

<?php

require_once 'vendor/autoload.php';

$phpword = new \PhpOffice\PhpWord\PhpWord();
$section = $phpword->addSection();

$section->addText("Hello World!");

$phpword->save('./hello.docx', 'Word2007');

这应该有效。