onizet / html2openxml

Html2OpenXml is a small .Net library that convert simple or advanced HTML to plain OpenXml components. This program has started in 2009, initially to convert user's comments into templated Word.
MIT License
332 stars 114 forks source link

Images are not in the docx file #167

Closed Kezzya closed 1 month ago

Kezzya commented 1 month ago

Hello. When I put <img src="urlWEBSITE"...> my docx file become blank, but, but if I put with img src="myfolder/image.png" parsing without images at all, but with text. I use the latest version of this program. In the oldets as I see in documentation I could adjust image settings, but now it is impossible, isn't it?

ASP.NET MVC

public ActionResult GenerateWordDocument()
 {
     using (var memoryStream = new MemoryStream())
     {
         using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(memoryStream, DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
         {
             var headers = db.DocumentConstructorLeftDatas.OrderBy(i => i.Npp).ToList();
             MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
             mainPart.Document = new Document();
             Body body = new Body();
             var model = headers;  
             string htmlContent = RenderRazorViewToString(this.ControllerContext, "Edit", model);
             var converter = new HtmlConverter(mainPart);
             converter.ParseBody(htmlContent);

             mainPart.Document.Save(); 
         }
         return File(memoryStream.ToArray(), "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
     }
 }
 public string RenderRazorViewToString(ControllerContext controllerContext, string viewName, object model)
 {
     controllerContext.Controller.ViewData.Model = model;
     using (var sw = new StringWriter())
     {
         var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
         var viewContext = new ViewContext(controllerContext, viewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw);
         viewResult.View.Render(viewContext, sw);
         return sw.GetStringBuilder().ToString();
     }
 }
onizet commented 1 month ago

Hello, yes this is still possible. look at documentation here Image Processing. I've just added a small code sample for clarification

Kezzya commented 1 month ago

But what if I have images local in /Images/ folder, do I need to change some settings?

Kezzya commented 1 month ago

I just don't see them in my docx file, don't know why, I think I parse HTML right, because visually it shows me my tags, so I dont know what the problem

onizet commented 1 month ago

If your image are in your local folder, you still must use the BaseImageUrl:

string baseUri = Path.Combine(Environment.CurrentDirectory, "images");
var converter = new HtmlConverter(mainPart, new DefaultWebRequest { 
                BaseImageUrl = new Uri(baseUri) });

await converter.ParseBody("<img src='wikipédia.png'>");

You can also provide a ILogger to the WebRequest to troubleshoot, or copy-paste the code of the DefaultWebRequest, and provide your own version (that way, you will be able to debug step by step). What is the file format extension?

If that still doesn't work, please share your image with me.

Kezzya commented 1 month ago

What is the file format extension? application/vnd.openxmlformats-officedocument.wordprocessingml.document

Yes it is still doesn't work, hmm. Before your library I used openxml alt chunks and also had problems with images, they were displayed like "x"

btw I write my project not in core version, just asp.net mvc 4.7.2, could it be the problem?

onizet commented 1 month ago

No, I doubt this is the problem as I support .Net 4.6.2. I was asking the file format of your image, not your docx.

Kezzya commented 1 month ago

png

onizet commented 1 month ago

Ok can you pm me your image to [deleted]?

Kezzya commented 1 month ago

plot I can put it here

Kezzya commented 1 month ago

I put full path of my project in baseUri variable and now it works, thanks for help :)