如何在Word中显示HTML生成的图像?
我当前正在通过生成 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.
您可以使用这个 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.
我只是通过将 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.
我有类似的问题。我通过将 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