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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2329897/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 02:19:02  来源:igfitidea点击:

How to display images in Word generated from HTML?

htmlms-wordbase64

提问by St. John Johnson

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).

我目前正在通过生成 HTML 并将标题信息更改为显示为 .doc 文件来创建 Word 文档。这是一个穷人的方法,但它工作得很好(直到现在)。

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.

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

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

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

回答by Pekka

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 questionand this MSDN discussion.

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

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.

  • 在文档所在位置创建一个文件夹,将其与文档并排存放,并相对引用图像(<img src='imageFolder/image1.jpg'>

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

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

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

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

回答by Jeff

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

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

回答by Cris Denver Labe

You can use this html-docx.js

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

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

你只需要打电话 htmlDocx.asBlob("YOUR HTML")

Link to sample html to doc generation with image.

链接到示例 html 到带有图像的文档生成。

回答by Ashish

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:

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

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

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

Also, I have done a lot of R & D into converting HTML to DOCX, HTML to DOC, HTML to PDF. Feel free to ask if you need any help.

此外,我在将 HTML 转换为 DOCX、将 HTML 转换为 DOC、将 HTML 转换为 PDF 方面做了大量的研发。如果您需要任何帮助,请随时询问。