I was looking to use PHP to create a Microsoft Word document. After looking online I found that most of the offered solutions were to just create an .doc that had not formatting done whatsoever. I was wondering what would be the best way to create a Word document that I could format in PHP i.e. change font, color, size, etc. for my company. I am guessing that some sort of library would be needed for this. Any responses would be greatly appreciated.
Asked
Active
Viewed 3.0k times
6
-
There's plenty of resources for this. E.g. [Reading/Writing a MS Word file in PHP](http://stackoverflow.com/q/188452) – Pekka Jul 08 '13 at 16:47
-
Your best bet will probably be creating a HTML file, and converting that into a docx file afterwards. See e.g. [Convert html to word /excel / powerPoint with PHP](http://stackoverflow.com/q/3590646) – Pekka Jul 08 '13 at 16:48
-
Are these classes free to use for commercial companies? – Tyler Hilbert Jul 08 '13 at 17:06
-
Depends on which one you mean – Pekka Jul 08 '13 at 17:09
-
is PHPWord able to be used free for commercial companies? – Tyler Hilbert Jul 08 '13 at 21:51
4 Answers
3
Build the content for the dynamic Word document in HTML, along with some Office specific style properties.
Public Sub Page_Load(sender as Object, e as EventArgs)
Dim strBody As New System.Text.StringBuilder("")
strBody.Append("<html " &
"xmlns:o='urn:schemas-microsoft-com:office:office' " &
"xmlns:w='urn:schemas-microsoft-com:office:word'" &
"xmlns='http://www.w3.org/TR/REC-html40'>" &
"<head><title>Time</title>")
//The setting specifies document's view after it is downloaded as Print instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" &
"<xml>" &
"<w:WordDocument>" &
"<w:View>Print</w:View>" &
"<w:Zoom>90</w:Zoom>" &
"<w:DoNotOptimizeForBrowser/>" &
"</w:WordDocument>" &
"</xml>" &
"<![endif]-->")
strBody.Append("<style>" &
"<!-- /* Style Definitions */" &
"@page Section1" &
" {size:8.5in 11.0in; " &
" margin:1.0in 1.25in 1.0in 1.25in ; " &
" mso-header-margin:.5in; " &
" mso-footer-margin:.5in; mso-paper-source:0;}" & _
" div.Section1" &
" {page:Section1;}" &
"-->" &
"</style></head>")
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" &
"<div class=Section1>" & _
"<h1>Time and tide wait for none</h1>" &
"<p style='color:red'><I>" &
DateTime.Now & "</I></p>" &
"</div></body></html>")
// Force this content to be downloaded as a Word document with the name of your choice
Response.AppendHeader("Content-Type", "application/msword")
Response.AppendHeader ("Content-disposition", "attachment;filename=myword.doc")
Response.Write(strBody)
End Sub
-
You code works for me. this is how I used in my PHP code. BTW, what is Section1? Is it a class name? – stackflow Apr 14 '16 at 05:01
0
dave's code works for me. I have used it like this in my PHP code. This opens in print layout.
<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'>
<head>
<title>Time</title>
<!--[if gte mso 9]>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:Zoom>100</w:Zoom>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->
<style>
@page Section1 {
size: 8.5in 11.0in;
margin: 1.0in 1.25in 1.0in 1.25in ;
mso-header-margin: .5in;
mso-footer-margin: .5in;
mso-paper-source: 0;
}
div.Section1 {
page: Section1;
}
</style>

stackflow
- 2,112
- 6
- 32
- 45
-1
You can do it using plain text and variables whilst not having it open in HTML - see here http://www.jakebown.com/?p=77

Jake Bown
- 411
- 4
- 11
-
3Note that [link-only answers are discouraged](http://meta.stackoverflow.com/tags/link-only-answers/info), SO answers should be the end-point of a search for a solution (vs. yet another stopover of references, which tend to get stale over time). Please consider adding a stand-alone synopsis here, keeping the link as a reference. – kleopatra Aug 31 '13 at 12:51
-