Hi guys,

Im quite new to php. Currently, im trying to convert my php to word and been googling for a long time about this. Anyone know how to convert php to word (free). For your information, im using Suse Linux as my server. Really need some suggestion for my problem :D

Thanks.

Dani AI

Generated

Quick point of confusion cleared: \PhpOffice\PhpWord is a PHP namespace and class, not a magic folder you must create. When installed via Composer the package lives under vendor/ and Composer generates vendor/autoload.php that maps the PhpOffice\PhpWord namespace to the library files. Manually creating a PHPOffice folder and copying files usually causes more problems than it solves.

Practical checklist (project root):

# install the library via Composer
composer require phpoffice/phpword

Make sure your bootstrap or entry script loads Composer's autoloader before you use the class:

<?php
require_once __DIR__ . '/vendor/autoload.php';

Blank pages normally mean a fatal error is being suppressed. Turn on error reporting temporarily and check the web/PHP error log:

ini_set('display_errors', 1);
error_reporting(E_ALL);

Also confirm Composer itself is usable (composer --version) and that Composer ran from the same PHP environment the web server uses.

If Composer fails when pulling Zend dependencies, the installer will print why (missing PHP CLI, missing extensions, or permission issues). On SUSE, use the system package manager to install the PHP CLI and any missing extensions, ensure file permissions allow the webserver to read vendor/, and run composer diagnose for more hints.

Notes tied to earlier posts: was right to make Composer globally available; that fixes many installer errors. ’s idea of moving files can work as a last resort, but relying on Composer + vendor/autoload.php is the clean, dependency-safe solution. Exact composer output or PHP error-log lines are what reveal the root cause.

Recommended Answers

All 11 Replies

Member Avatar for Member #120589

When you say convert php to word. What do you mean? Save output to a word document?

Maybe this

But it does require some libraries to be installed with php.

As diafol said - do you just want to convert PHP output to Word documents? That is easy (assuming you know php well enough). To convert abstract data to Word is an entire other kettle of fish! In any case, you need to show us what your data is and what problems your are having before we can contribute more to your solution.

diafol/rubberman, yup i want to convert php output to word documents. i already tried phpword from github but im stuck when installing this requirement :

Zend\Escaper component
Zend\Stdlib component
Zend\Validator component

the tutorial from https://framework.zend.com/downloads said its can be done by installing composer. i tried the command via composer but the error as the attachment i included. zend.jpg

Go to
/usr/local/bin
location and execute
mv composer.phar /usr/local/bin/composer
command.
Then you can use composer further. Double check what docs say.

Thanks for your response/information diafol and tpojka, i already followed those command and i think its work since no error occur when i run it :D ~~thank for those ~~
Next, im trying used the code from example and and its become blank. Then i realise there's a lil confuse about this line :

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

Here's the full code :

<?php
require_once 'bootstrap.php';

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

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

// Adding an empty Section to the document...
$section = $phpWord->addSection();
// Adding Text element to the Section having font styled by default...
$section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
        . 'The important thing is not to stop questioning." '
        . '(Albert Einstein)'
);

/*
 * 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(
    '"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(
    '"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('"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. */

My next question is, why the code have PhpOffice before the PhpWord's folder? Is there anything i should install or should i create a folder named PhpOffice. I just assume the path is the problem.

Member Avatar for Member #120589

Strange you don't get an error. Try the folder idea.

diafol, u mean the composer command or the example file code?

Member Avatar for Member #120589

I mean try placing the PHPWord stuff inside PHPOffice folder

Im try looking for this PHPOffice folder in my server but none of this folder found. :D
is there something wrong with my composer installation? Is this PHPOffice folder will exist if i successfully install the composer or should i create this PHPOffice folder? Im so confuse right now.

Member Avatar for Member #120589

Create the folder and move PHPWord into it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.