如何将图像添加到 php 创建的 MS Word 文档中?

发布于 2024-08-19 11:58:38 字数 706 浏览 4 评论 0原文

这是我的代码,被大大截断了。

<?php
    session_start(); 
    header("Content-Type: application/msword");
    header('Content-Disposition: attachment; filename="Contrat d\'étude.doc"');
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>
        <p align="right">Version: A</p>
        <img src="img/logoPoly.png" alt="École Polytechnique de Montréal" />

        <h1>CONTRAT D'ÉTUDES HORS QUÉBEC</h1>
        ............

我尝试在这里使用常规 img 标签添加图像,但是一旦在用户的计算机上,该路径显然不起作用。那么我该如何解决这个问题呢?

顺便说一句,元标记是设置字符集的好方法吗?或者有更好的方法,也许使用 php 头函数?

感谢您的帮助!

Here is my code, greatly truncated.

<?php
    session_start(); 
    header("Content-Type: application/msword");
    header('Content-Disposition: attachment; filename="Contrat d\'étude.doc"');
?>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>

    <body>
        <p align="right">Version: A</p>
        <img src="img/logoPoly.png" alt="École Polytechnique de Montréal" />

        <h1>CONTRAT D'ÉTUDES HORS QUÉBEC</h1>
        ............

I try here adding the image using the regular img tag, but once on the user's computer, the path doesn't work, obviously.. So how can I go about this?

And btw, is the meta tag the good way of setting the charset? Or is there a better way, perhaps using the php header function?

Thanks for helping out!

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

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

发布评论

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

评论(2

一抹淡然 2024-08-26 11:58:38

您的代码中是否有某些内容已被截断,可将所有 HTML 转换为 MS Word 文件?

因为...呃...您知道使用 .doc 扩展名保存 HTML 文件实际上并不会使其成为 Word 文件,对吗?

如果您有 HTML,只需将其作为 HTML 提供,用户就可以使用浏览器自己的“用图像保存网页”功能来保存图像。或者,如果您愿意,您可以编写一个 MHTML 文件,包括其中的图像。

但 HTML-file-hiding-under-DOC-file-extension 并不是真正有效的格式。如果 Word 能够加载/导入该内容,那么您就不应该依赖它;要求它拖入图像就像要求棒上的月亮一样。

是的,当您提供希望人们保存的 HTML 文件时,meta 是设置字符集的唯一方法。 Content-Type 标头通常也是放置它的好地方,但是一旦文件保存在不支持保存类型元数据(即,目前所有流行的文件系统)。

顺便说一句,像 é 这样的非 ASCII 字符包含在 filename 标头参数中是完全不可靠的。根据浏览器(对于 IE,还有区域设置),它们被视为不同的编码,并且没有标准的方法来转义它们。将非 ASCII 字符放入下载文件的文件名中的唯一可靠方法是将它们保留为尾随 URL 部分,并编码为 UTF-8。例如。:

http://www.example.com/download.php/Contrat%20d%27%C3%A9tude.doc

Is there something in your code you have truncated which converts all that HTML to an MS Word file?

Because... er... you know saving an HTML file with a .doc extension doesn't actually make it a Word file, right?

If you've got HTML, just serve it as HTML, and the user can take care of saving images using the browser's own ‘Save web page with images’ feature. Or you could write an MHTML file if you like, including the images inside it.

But HTML-file-hiding-under-DOC-file-extension isn't really a valid format. It's something you shouldn't rely on if Word manages to load/import the thing at all; asking for it to drag in images as well is asking for the moon on a stick.

Yes, meta is the only way to set charset when you're supplying an HTML file that you expect people to save. The Content-Type header is normally a good place to put it as well, but that information will be lost as soon as the file is saved on a filesystem that doesn't support saving type metadata (which is, currently, all popular filesystems).

Incidentally, non-ASCII characters like é are totally unreliable to include in a filename header parameter. They get treated as different encodings depending on the browser (and for IE, also the locale) and there's no standard way of escaping them. The only reliable way to put non-ASCII characters in the filename of a downloaded file is to leave them as trailing URL parts, encoded as UTF-8. eg.:

http://www.example.com/download.php/Contrat%20d%27%C3%A9tude.doc
若水般的淡然安静女子 2024-08-26 11:58:38

在 MS WORD 上创建模板(添加一些图像),并将其另存为 doc 文件。在记事本++或smth代码编辑器上打开此文件,将所有代码复制到php文件中并添加您的php变量。
要添加图像,您需要将图像转换为base64格式。如果您注意到文字文件代码,添加的图像是某种基于字符的格式。

Create your template on MS WORD(add some images), and save it as doc file. Open this file on notepad++ or smth code editor, copy all code into php file and add your php variables.
For adding image, you need to convert an image to base64 format. If you notice word file code, added images are some character-based format.

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