-1

I use this code to create a MS Word document; however, I want to make it in landscape. Does anybody know how to it? Thanks

$fp = fopen('test.doc', 'w+');

$str = "<html><body>Content</body></html>";

fwrite($fp, $str);
fclose($fp);
Khoa Nguyen
  • 1,319
  • 7
  • 21
  • Word really use HTML as internal format?! Oo I can't imagine, thats the right way to create a word document. – KingCrunch Jun 01 '11 at 09:15
  • 1
    @KingCrunch - a lot of web developers work on the assumption that every file format every used by computers is actually HTML (or occasionally CSV) and don't appreciate that a __real__ Word .doc document is a BIFF format binary file... unless it's to complain that Microsoft use undocumented formats (also a mistaken belief nowadays) – Mark Baker Jun 01 '11 at 09:18
  • 1
    I know we can create a Word with COM object or Open Office template, but I only have a Linux shared hosting and I cannot do anything with the server. Therefore, I choose this solution. If you have any solution for library to create a real word, I very appreciate that. – Khoa Nguyen Jun 02 '11 at 00:47

1 Answers1

0

As far as I know, your code should not work. MS Word produces binary files so you need to use COM object.

Here's an example:

<?php
$word = new COM("word.application") or die ("Could not initialise MS Word object.");
$word->Documents->Open(realpath("Sample.doc"));

// Extract content.
$content = (string) $word->ActiveDocument->Content;

echo $content;

$word->ActiveDocument->Close(false);

$word->Quit();
$word = null;
unset($word);

The example is taken from here: http://www.developertutorials.com/tutorials/php/extracting-text-from-word-documents-via-php-and-com-81/

dierre
  • 7,140
  • 12
  • 75
  • 120
  • My code does not produce a real word document, but it works. However, I cannot control it properties. That is my problem. I think your code need a Windows because it uses COM. Please correct me if I am wrong. – Khoa Nguyen Jun 02 '11 at 00:55
  • Yes, only on Windows apparently. http://www.php.net/manual/en/com.requirements.php – dierre Jun 02 '11 at 08:27