douyi5822 2016-07-15 19:51
浏览 182
已采纳

安装PHPword的问题

I've been trying to install PHPWord for the last 4 hours and have had no luck. I've tried all methods of installation including composer as well as downloading the folder itself. Whenever I run the php file, it always returns me the error "Fatal error: Class 'PhpWord' not found in /home/ubuntu/workspace/hello-world.php on line 9" Also, it seems that whenever I attached the phpoffice/phpword in the composer.json file, it kept on giving me an installation error saying no version specified. BTW, I am running these files on a cloud hosting website (C9.io).

I've attached my composer.json as well as the hello_world.php

Any help is much appreciated.

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. */
?> 

This is the composer.json require part

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

1条回答 默认 最新

  • doushi2845 2016-07-15 19:58
    关注

    It looks like it installed properly since you're getting past the autoloader statements.

    The issue is that PhpWord is not the class, it would be \PhpOffice\PhpWord\PhpWord since it's namespaced.

    So you can try:

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

    or add the use statement back towards the top of the script which is commented out:

    use PhpOffice\PhpWord\PhpWord;
    

    This will allow you to call the class as PhpWord instead of the fully namespaced version.

    EDIT:

    Try this:

    From the command line (you've probably already done this):

    composer require phpoffice/phpword
    

    Create file test.php with contents:

    <?php
    
    require_once 'vendor/autoload.php';
    
    $phpword = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpword->addSection();
    
    $section->addText("Hello World!");
    
    $phpword->save('./hello.docx', 'Word2007');
    

    That should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?