使用 HTML 文件作为 Word 文件时换行格式?

发布于 2024-08-03 16:56:31 字数 1700 浏览 3 评论 0原文

我正在为客户编写一个 PHP 应用程序,该客户需要我已经创建的预先存在的 HTML 页面,以便将其“导出”为 Word 文件。简单地说,这就是它的完成方式:

if (isset($_GET["word"])) {
  header("Content-type: application/vnd.ms-word");
  header("Content-Disposition: attachment;Filename=some_file.doc");
}

当然,如果页面查询字符串中存在“word”标志,则会调用此方法,例如:

whateverpage.php?somequery=string&someother=test&word

无论如何,我的问题是,尽管这个 HTML 页面实际上有多复杂,但它实际上传输得相当好只需更改内容类型即可生成格式良好的 Word 文件。我遇到的唯一问题是新换行符(HTML
标签)的格式不正确。例如:在我的 html 中,如果我有一些看起来像

Aug
01

行间带有 BR 的内容,它最终总是会显示

Aug 01

在生成的 Word 文件中。

我已经做了一些谷歌搜索和许多其他各种测试,但似乎没有任何东西可以通过简单的新行正确格式化。

有谁知道如何在从 HTML 文件创建的 Word 文件中正确设置换行符的格式?

非常感谢任何帮助。

编辑:

我尝试将上述行包装在 P 标签中,唉:

<p>Aug<br>01</p>

没有运气。我还尝试过制作一个基本文档和 Word,将其另存为 HTML 文件并查看生成的(即草率的)Word HTML 源代码。我认为其中有一些 CSS 可能会给我一些线索,但我尝试了所有方法,但似乎都无法正常工作。 Word 似乎在包装段落中添加了一个“MsoNormal”类,我尝试添加它,但它只是删除了我拥有的任何字体格式,并且没有帮助。这是 CSS Word 自己创建的:

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;}

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;}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

短叹 2024-08-10 16:56:31

我遇到了同样的问题,我像这样标记我的换行符:

<br/>

当我将其更改为“

<br>

然后我的换行符开始工作”时。

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.

话少情深 2024-08-10 16:56:31

您的问题可能是由于当您将内容类型切换到 Word 文档时,浏览器不会将其呈现为 HTML。我的猜测是,如果您想要换行,则需要在 Word 文档中添加换行符。
如何插入这个换行符?我不确定,但你总是可以尝试:

echo "Aug\r\n01";

\r\n 是换行符。

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.

Bonjour°[大白 2024-08-10 16:56:31

怎么样,如果你想保持换行符,只需 echo "

Aug

01

"; 这并不漂亮,但是它应该会影响您正在寻找的换行符。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文