1

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;}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
theotherlight
  • 783
  • 1
  • 8
  • 12

3 Answers3

2

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.

1

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.

Eddie
  • 1,968
  • 17
  • 19
  • Okay, I just tried this but it remains the same output in the Word file. I'll edit my original question to indicate what I've previously attempted. Thank you for your assistance though. – theotherlight Sep 04 '09 at 13:53
  • Sorry I couldn't help. Maybe you should try researching how a Word file works and how _your_ browser renders Word documents. – Eddie Sep 04 '09 at 13:55
  • For what it's worth, the Word file isn't being rendered in the browser. Switching the content-type forces the HTML file to be rendered in Microsoft Word itself (Open/Save As dialogs pops up), which leads me to believe it shouldn't have anything to do with the browser rendering? – theotherlight Sep 04 '09 at 14:01
  • Ah, I thought it was being loaded in the browser. You're right, it has nothing to do with how the browser handles it. I still recommend you read up on newlines in Word documents. – Eddie Sep 04 '09 at 14:07
0

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.

David Thomas
  • 249,100
  • 51
  • 377
  • 410