Reputation: 783
I'm writing a PHP application for a client that needs a pre-existing HTML page I've already created to be "exported" as an Word file. Simply, this is how it's done:
if (isset($_GET["word"])) {
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=some_file.doc");
}
This, of course, will be called if a "word" flag is located in the page querystring, e.g.:
whateverpage.php?somequery=string&someother=test&word
Anyways, my question is, despite how complex this HTML page actually is, it actually transfers pretty well to a nicely formatted Word file just by changing the content-type. The only problem I'm having is that new line breaks (HTML <br> tags) aren't formatting properly. E.g.: In my html, if I have something that looks like
Aug 01
with a BR between the lines, it always ends up showing
Aug 01
in the generated Word file.
I've done some Googling and lots of tests with various other things but nothing seems to format properly with a simple new line.
Does anyone know how to properly format a new line character in a Word file that's being created from an HTML file?
Any help is greatly appreciated.
Edit:
I've tried wrapping the said line in a P tag, ala:
<p>Aug<br>01</p>
Without luck. I've also tried making a basic document and Word, saving it as an HTML file and looking at the generated (i.e sloppy) Word HTML source. There is some CSS in there that I thought might give me a clue, but I tried everything and nothing seemed to work properly. Word seems to add an 'MsoNormal' class to wrapped paragraphs, I tried adding this but it just removes any font formatting I had and doesn't help. Here is the CSS Word creates itself:
p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin-top:0cm; margin-right:0cm; margin-bottom:10.0pt; margin-left:0cm; line-height:115%; mso-pagination:widow-orphan; font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:Calibri; mso-fareast-theme-font:minor-latin; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-fareast-language:EN-US;}
Upvotes: 1
Views: 6448
Reputation: 31
I had this same problem, I was tagging my line breaks like so:
<br/>
When I changed it to just
<br>
Then my line breaks starting working.
Upvotes: 3
Reputation: 253506
How about, if you want to maintain a line-break, just echo "<p>Aug</p><p>01</p>";
it ain't pretty, but it should effect the line break you're looking for.
Upvotes: 0
Reputation: 1958
Your problem is probably due to the fact that when you switch the content type to a Word document, the browser doesn't render it as HTML. My guess is that you need to add a newline to the Word document if you want a line break. How to insert this line break? I'm not sure, but you could always try:
echo "Aug\r\n01";
Where \r\n are the newline characters.
Upvotes: 1