linux - [Solved-5 Solutions] How to Create Word Document using PHP in Linux - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to Create Word Document using PHP in Linux ?

Linux - Solution 1:

If you need to produce "real" Word documents you need a Windows-based web server and COM automation.

fake HTTP headers for tricking Word into opening raw HTML

A rather common alternative is:

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>Fake word document</b>";
echo "</body>";
echo "</html>"
click below button to copy the code. By - Linux tutorial - team

Make sure you don't use external stylesheets. Everything should be in the same file.

Note that this does not send an actual Word document. It merely tricks browsers into offering it as download and defaulting to a .doc file extension. Older versions of Word may often open this without any warning/security message, and just import the raw HTML into Word. PHP sending sending that misleading Content-Type header along does not constitute a real file format conversion.

Linux - Solution 2:

PHPWord can generate Word documents in docx format. It can also use an existing .docx file as a template - template variables can be added to the document in the format ${varname}

Linux - Solution 3:

OpenOffice templates + OOo command line interface.

  • Create manually an ODT template with placeholders, like [%value-to-replace%]
  • When instantiating the template with real data in PHP, unzip the template ODT (it's a zipped XML), and run against the XML the textual replace of the placeholders with the actual values.
  • Zip the ODT back
  • Run the conversion ODT -> DOC via OpenOffice command line interface.

Linux - Solution 4:

The easiest way to create DOC files on Linux, using PHP is with the Zend Framework component phpLiveDocx.

"phpLiveDocx allows developers to generate documents by combining structured data from PHP with a template, created in a word processor. The resulting document can be saved as a PDF, DOCX, DOC or RTF file. The concept is the same as with mail-merge."

Linux - Solution 5:

OpenTBS can create DOCX dynamic documents in PHP using the technique of templates.

No temporary files needed, no command lines, all in PHP.

It can add or delete pictures. The created document can be produced as a HTML download, a file saved on the server, or as binary contents in PHP.

It can also merge OpenDocument files (ODT, ODS, ODF, ...)


Related Searches to - linux - linux tutorial - How to Create Word Document using PHP in Linux