2

I've already got the functionality to print pdf reports and bills in my application, but now I need to also be able to export in .doc format. I'm using TCPDF's writeHTML function to write and format the pdf files, and I was wondering if there's a way to do the same for .doc files.

Bogdan
  • 1,869
  • 6
  • 24
  • 53
  • PHPWord - http://phpword.codeplex.com/ - can create .docx files (the default format for MS Word 2007 and 2010) – Mark Baker Feb 17 '12 at 14:33
  • If you're planning on making invoices and bills in DOC format, I'll advice against it. DOC is easily changeable, someone will easily be able to fabricate a report of yours. – Madara's Ghost Feb 17 '12 at 14:34
  • Easiest way is to simply produce an RTF file and serve it up as a .doc. – Marc B Feb 17 '12 at 14:34
  • Well I won't question my manager's decision but we're only gonna use the .doc functions internally. I personally think bills and invoices in doc are a bad idea as well, but hey, it's the manager's job to manage and mine to make stuff happen when people click on things. – Bogdan Feb 17 '12 at 14:38

1 Answers1

2

This is pretty straight forward. A .doc file can just contain HTML. If you pass along the Word header, you can just start printing HTML. So, for example:

<?php
header("Content-Type: application/vnd.ms-word");
print "<table border=\"1\"><tr><td><b>field1</b></td><td><b>field2</b></td></tr>";
print "<tr><td>value1 </td><td bgcolor=\"#137799\">value2 in blue cell bakground</td></tr></table>";
?>

Source: here (verified it to be working).

Oldskool
  • 34,211
  • 7
  • 53
  • 66
  • Thanks, I was aware you could just print doc files in php but i wasn't sure you can just stick html in it. will try to see how it works – Bogdan Feb 17 '12 at 14:56
  • Not strictly true.... a "real" .doc file cannot contain html. However, MS word is pretty forgiving, detects that you're lying to it, and loads the html anyway. – Mark Baker Feb 17 '12 at 14:59
  • I have just one question to ask tho, when I enter the exact thing you posted it prompts me to save or open a php file, can I have it create a .doc file instead (the php file contains the code, if that helps) Nevermind, i found the answer in the forum thread you linked. many thanks. – Bogdan Feb 17 '12 at 15:00
  • @Bogdan using the `Content-Disposition` header should take care of that. – Oldskool Feb 17 '12 at 15:09
  • Thanks for the help. one more question, if I may: is there any way to set margins and things like that for the document? – Bogdan Feb 20 '12 at 07:33