如何在Word中显示HTML生成的图像?

发布于 2024-08-23 01:12:20 字数 208 浏览 4 评论 0原文

我当前正在通过生成 HTML 并更改标题信息以显示为 .doc 文件来创建 Word 文档。这是一个穷人的方法,但效果很好(到目前为止)。

我只是被要求在文件中包含一张图像。我最好的想法是使用 Base64 嵌入图像。这在浏览器中工作得很好,但是 Word 只给了我一个带有 X 的框。

关于如何将图像嵌入到此文件中并使其显示在 Microsoft Word 中的建议?

I'm currently creating a Word document by generating HTML and changing the header information to display as a .doc file. It's a poor man's method, but it works just fine (until now).

I was just asked to include an image in the file. My best idea was to base64 embed the image. This works fine in a browser, however Word just gives me a box with an X in it.

Suggestions on how I can embed an image into this file and have it display in Microsoft Word?

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

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

发布评论

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

评论(5

这是一个困难的问题,Word 无法处理 HTML 中的 data: base64 编码图像,至少这是 这个问题此 MSDN 讨论

您有三个选择:

  • 在文档位置创建一个文件夹,将其与文档一起存储,并相对引用图像 ( )

  • 使用绝对 URL 或文件路径(更糟糕)

  • 查看新的 Word > 2003 基于 XML 的文件格式,绝对有可能.

我能想到的唯一其他选择实际上是创建一个本机Word 文件,例如使用OpenOffice。

That's a tough one, Word isn't able to handle data: base64 encoded images in HTML, at least that's the outcome in this question and this MSDN discussion.

You have three options:

  • Create a folder in the location of the document, store it alongside the document, and reference images relatively (<img src='imageFolder/image1.jpg'>)

  • Work with absolute URLs or file paths (even more sucky)

  • Look into the new Word > 2003 XML based file format(s), it is definitely possible there.

The only other option I can think of is actually creating a native Word file, e.g. using OpenOffice.

小镇女孩 2024-08-30 01:12:20

您可以使用这个 html-docx.js

您只需调用 htmlDocx .asBlob("YOUR HTML")

链接到示例 html使用图像生成文档。

You can use this html-docx.js

You just have to call htmlDocx.asBlob("YOUR HTML")

Link to sample html to doc generation with image.

太傻旳人生 2024-08-30 01:12:20

我只是通过将 DOCX 打印为 PDF,然后使用 Acrobat 另存为 HTML 来实现这一点。图像显示很小,但确实存在。

I just achieved this by printing the DOCX to PDF then using Acrobat to Save As to HTML. Images showed up small, but there.

沉溺在你眼里的海 2024-08-30 01:12:20
 MIME-Version: 1.0
Content-Type: multipart/related; boundary="----=_NextPart_"

------=_NextPart_
Content-Location: file:///C:/Doc2.htm
Content-Transfer-Encoding: quoted-printable
Content-type: text/html; charset="utf-8"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml">
<head>
<style>
</style>
</head>

<body>

  <v:shape style=3D'width:234.75pt;height:135.75pt;'>
     <v:imagedata src=3D"Doc2/i1.png" />
  </v:shape>

</body>

</html>


------=_NextPart_
Content-Location: file:///C:/Doc2/i1.png
Content-Transfer-Encoding: base64
Content-Type: image/png

iVBORw0KGgoAAAANSUhEUgAAATkAAAC1CAYAAAAk5IjmAAAAAXNSR0IArs4c6QAAAARnQU1BAACx
..
AAAAAElFTkSuQmCC

------=_NextPart_--
 MIME-Version: 1.0
Content-Type: multipart/related; boundary="----=_NextPart_"

------=_NextPart_
Content-Location: file:///C:/Doc2.htm
Content-Transfer-Encoding: quoted-printable
Content-type: text/html; charset="utf-8"

<html xmlns:v=3D"urn:schemas-microsoft-com:vml">
<head>
<style>
</style>
</head>

<body>

  <v:shape style=3D'width:234.75pt;height:135.75pt;'>
     <v:imagedata src=3D"Doc2/i1.png" />
  </v:shape>

</body>

</html>


------=_NextPart_
Content-Location: file:///C:/Doc2/i1.png
Content-Transfer-Encoding: base64
Content-Type: image/png

iVBORw0KGgoAAAANSUhEUgAAATkAAAC1CAYAAAAk5IjmAAAAAXNSR0IArs4c6QAAAARnQU1BAACx
..
AAAAAElFTkSuQmCC

------=_NextPart_--
清秋悲枫 2024-08-30 01:12:20

我有类似的问题。我通过将 Base64 图像解码到磁盘并创建图像链接、在应用程序中部署 servlet 来侦听磁盘上图像指向的链接来解决这个问题。
这是我实现的解决方案:

如何将 HTML 转换为格式良好的 DOCX,且样式属性完好无损

I had the similar problem. I solved it by decoding the Base64 images to disk and creating a link to the images, deploying a servlet in my application to listen to the link pointed by images on disk.
Here is the solution I implemented:

How to convert HTML to well formed DOCX with styling attributes intact

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