【发布时间】:2016-07-15 19:51:22
【问题描述】:
过去 4 小时我一直在尝试安装 PHPWord,但没有成功。我已经尝试了所有安装方法,包括作曲家以及下载文件夹本身。每当我运行 php 文件时,它总是返回错误“致命错误:第 9 行的 /home/ubuntu/workspace/hello-world.php 中找不到类 'PhpWord'”此外,似乎每当我附加 phpoffice /phpword 在 composer.json 文件中,它一直给我一个安装错误,说没有指定版本。顺便说一句,我在云托管网站 (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"
},
【问题讨论】: