php - Issues Installing PHPword -
i've been trying install phpword last 4 hours , have had no luck. i've tried methods of installation including composer downloading folder itself. whenever run php file, returns me error "fatal error: class 'phpword' not found in /home/ubuntu/workspace/hello-world.php on line 9" also, seems whenever attached phpoffice/phpword in composer.json file, kept on giving me installation error saying no version specified. btw, running these files on cloud hosting website (c9.io).
i've attached composer.json hello_world.php
any 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 new document... $phpword = new phpword(); /* note: element append document must reside inside of section. */ note: it's possible customize font style of text element add in 3 ways: - inline; - using named font style (new font style object implicitly created); - using explicitly created font style object. */ // adding text element font customized inline... $section->addtext( htmlspecialchars( '"great achievement born of great sacrifice, ' . 'and never result of selfishness." ' . '(napoleon hill)' ), array('name' => 'tahoma', 'size' => 10) ); // adding text element 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 not in never falling, ' . 'but in rising again after fall." ' . '(vince lombardi)' ), $fontstylename ); // adding text element 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 can , you\'re halfway there." (theodor roosevelt)') ); $mytextelement->setfontstyle($fontstyle); // saving document ooxml file... $objwriter = \phpoffice\phpword\iofactory::createwriter($phpword, 'word2007'); $objwriter->save('helloworld.docx'); // saving document odf file... $objwriter = \phpoffice\phpword\iofactory::createwriter($phpword, 'odtext'); $objwriter->save('helloworld.odt'); // saving document html file... $objwriter = \phpoffice\phpword\iofactory::createwriter($phpword, 'html'); $objwriter->save('helloworld.html'); /* note: skip rtf, because it's not xml-based , requires different example. / / note: skip pdf, because "html-to-pdf" approach used create pdf documents. */ ?>
this composer.json require part
"require": { "php": ">=5.3.3", "ext-xml": "*", "phpoffice/phpword":"dev-master" },
it looks installed since you're getting past autoloader statements.
the issue phpword
not class, \phpoffice\phpword\phpword
since it's namespaced.
so can try:
$phpword = new \phpoffice\phpword\phpword();
or add use
statement towards top of script commented out:
use phpoffice\phpword\phpword;
this allow call class phpword
instead of namespaced version.
edit:
try this:
from command line (you've done this):
composer require phpoffice/phpword
create file test.php 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.
Comments
Post a Comment