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.

Recommended Answers

All 11 Replies

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 diafol

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 diafol

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 diafol

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.