Do you know any way to generate doc and docx files with PHP and without COM component? I've tried PHPWord, which creates docx files, but these cannot be opened in OpenOffice because they cause it to crash. I've also tried PHPDocx, but it didn't generate any files at all.
-
*(related)* [Create Word Document Using PHP in Linux](http://stackoverflow.com/questions/124959/create-word-document-using-php-in-linux) – Gordon Nov 11 '10 at 10:51
-
2Also don't give up on PHPDocx. It looks like a decent package – Pekka Nov 11 '10 at 11:18
-
1Don't give up on PHPWord either... there's a lot of work going into it. Not all versions of OO can read docx files either, are you sure that yours does? – Mark Baker Nov 11 '10 at 11:20
-
PHPDocx is paid-for though. No chance! – Matt Fletcher Jun 23 '14 at 14:05
4 Answers
Generating word documents with JS:
I have created a simple open-source library that will replace tags by values.
For example Hi {name} with data={name:"John"} will be replaced by Hi John.
Here it is : https://github.com/edi9999/docxtemplater
- 19,701
- 13
- 88
- 127
See here:
http://www.webcheatsheet.com/php/create_word_excel_csv_files_with_php.php
To quote from the article the most common method:
Using HTTP Headers
In this method you need to format the HTML/PHP page using Word-friendly CSS and add header information to your PHP script. Make sure you don't use external style sheets since everything should be in the same file.
As a result user will be prompted to download a file. This file will not be 100% "original" Word document, but it certainly will open in MS Word application. You can use this method both for Unix and Windows environments.
<?php
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>My first document</b>";
echo "</body>";
echo "</html>";
?>
- 69,876
- 20
- 132
- 137
-
25This is garbage. Don't copy and paste that around further. Sending fake HTTP headers along with some HTML output does **not** make it a Word document. It might have tricked browsers into passing it to Word, which implicitly converts it. Newer versions will at least raise a warning. And any business class firewall is likely to block such crude workarounds. – mario Jun 07 '15 at 05:52
-
@mario - note this is some four years old, and also part of the answer on the duplicate link you have marked. I agree with some of your synopsis, however note that `garbage` is likely hyperbole. – SW4 Jun 08 '15 at 06:54
-
Sorry for the strong wording, btw. But this very code sample was feeding into misinformed newbie questions. Just closed the topic, and clarified it in the primary duplicate instead. – mario Jun 08 '15 at 07:01
-
As I mentioned here PHP Convert Word file to HTML without losing styling and images,
The best solution I've found so far is http://www.phplivedocx.org/ . You use it with the Zend framework. Very easy to set up and you get minimal deviation from the actual word format. It generates and converts word docs from/to html (among other formats) very well.