Error when you import images in html HtmlFormatProvider

17 posts, 1 answers
  1. Андрей
    Андрей avatar
    14 posts
    Member since:
    Dec 2014

    Posted 31 May 2015 Link to this post

    Hi,

    I do HtmlFormatProvider.Import (html), in html have Images, there is an exception (ParamName: data)

    (Telerik ASP.NET AJAX 2015.1.104.45)

    Why?

  2. Answer
    Petya
    Admin
    Petya avatar
    989 posts

    Posted 01 Jun 2015 Link to this post

    Hi Андрей,

    For the time being, importing HTML that contains an image from an external source (i.e. on the web or a file) would not work. The library only supports images encoded as raw data. For all other cases you can use the import settings in order to capture the LoadFromUri event and load the image, for example like this:
    HtmlFormatProvider provider = new HtmlFormatProvider();
    provider.ImportSettings.LoadFromUri += (s, e) =>
    {
        string extension = Path.GetExtension(e.Uri);
        Uri uri = new Uri(e.Uri, UriKind.RelativeOrAbsolute);
        System.Net.WebClient oWebClient = new System.Net.WebClient();
        
        using (MemoryStream stream = new MemoryStream())
        {
            oWebClient.OpenRead(uri).CopyTo(stream);
            e.SetData(stream.ToArray());
        }
    };
    this.document = provider.Import(str);

    I hope this helps.

    Regards,
    Petya
    Telerik
  3. Андрей
    Андрей avatar
    14 posts
    Member since:
    Dec 2014

    Posted 01 Jun 2015 in reply to Petya Link to this post

    What is..."document " and "str"?

    this.document = provider.Import(str);

  4. Андрей
    Андрей avatar
    14 posts
    Member since:
    Dec 2014

    Posted 01 Jun 2015 in reply to Андрей Link to this post

    Now there are no errors, but then I export html and Images do not appear in the file docx (MS Word).

           

            DocxFormatProvider provider = new DocxFormatProvider();
            HtmlFormatProvider formatProvider = new HtmlFormatProvider();
            formatProvider.ImportSettings.LoadFromUri += (s, e) =>
            {
                string extension = Path.GetExtension(e.Uri);
                if (this.IsSupportedImageFormat(extension))
     
                {
                    Uri uri = new Uri(e.Uri, UriKind.RelativeOrAbsolute);
                    System.Net.WebClient oWebClient = new System.Net.WebClient();
     
                    using (MemoryStream stream = new MemoryStream())
                    {
                        var openRead = oWebClient.OpenRead(uri);
                        if (openRead != null)
                        {
                            openRead.CopyTo(stream);
                            e.SetData(stream.ToArray());
                        }
                    }
                     
                }
            };
    byte[] byteArray;
    RadFlowDocument document = formatProvider.Import(html);
    byteArray = provider.Export(document);

     // Response.BinaryWrite(byteArray);

  5. Petya
    Admin
    Petya avatar
    989 posts

    Posted 02 Jun 2015 Link to this post

    Hi Андрей,

    Could you confirm the images are properly downloaded? You can try saving them to files (i.e. the stream variable from your snippet) to see if everything is working correctly up until this point in the logic.

    In case the images are downloaded but are lacking in the exported DOCX, please send over a sample HTML document that we can test this with. I could not replicate an issue with the logic, so I'm not sure what might be causing such result on your end.

    Regards,
    Petya
    Telerik
  6. Андрей
    Андрей avatar
    14 posts
    Member since:
    Dec 2014

    Posted 02 Jun 2015 in reply to Petya Link to this post

    I am working in asp.net
    Image comes right, I checked.
    Without images docx document configured correctly.
    Here is the code (function ExportToWordClick makes exports to MS Word):

        private bool IsSupportedImageFormat(string extension)
        {
            if (extension != null)
            {
                extension = extension.ToLower();
            }
            return extension == ".jpg" ||
                extension == ".jpeg" ||
                extension == ".png" ||
                extension == ".bmp" ||
                extension == ".tif" ||
                extension == ".tiff" ||
                extension == ".ico" ||
                extension == ".gif" ||
                extension == ".wdp" ||
                extension == ".hdp";
        }
     
     
    protected void ExportToWordClick(object sender, EventArgs ea)
        {
            byte[] byteArray;
    string html = "<div id='p2'><span id=test1><img alt='' src='http://pravopmr.ru/Content/Images/News/attention.png' /></span>Test</div>";
      
            DocxFormatProvider provider = new DocxFormatProvider();
            HtmlFormatProvider formatProvider = new HtmlFormatProvider();
      
            formatProvider.ImportSettings.LoadFromUri += (s, e) =>
            {
                string extension = Path.GetExtension(e.Uri);
                if (this.IsSupportedImageFormat(extension))
      
                {
                    Uri uri = new Uri(e.Uri, UriKind.RelativeOrAbsolute);
                    System.Net.WebClient oWebClient = new System.Net.WebClient();
      
                    using (MemoryStream stream = new MemoryStream())
                    {
                        var openRead = oWebClient.OpenRead(uri);
                        if (openRead != null)
                        {
                            openRead.CopyTo(stream);
                            e.SetData(stream.ToArray());
                        }
                    }
                }
            };
      
            try
            {
                RadFlowDocument document = formatProvider.Import(html);
                RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
                byteArray = provider.Export(document);
            }
            catch (Exception)
            {
                const string errorMsg = "Произошла ошибка экспорта текста документа. Просьба обратиться к разработчикам";
                RadFlowDocument document = formatProvider.Import(errorMsg);
      
                byteArray = provider.Export(document);
      
            }
      
            Response.Clear();
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            Response.AddHeader("Content-Disposition",
                               String.Format("attachment;filename={0}","test.docx"));
            Response.BinaryWrite(byteArray);
            Response.Flush();
            Response.Close();
            Response.End();
    }

  7. Petya
    Admin
    Petya avatar
    989 posts

    Posted 05 Jun 2015 Link to this post

    Hello Андрей,

    It looks like the image is actually in the exported DOCX, but it is not visible due to its size. This is related to the fact that it doesn't have width and height specified in the source HTML and when you import the document, the library internally sets (1,1) dimensions to such images.

    With the current version of the library the only workaround I can suggest is to specify the width and height properties of the image in the HTML:
    string html = "<div id='p2'><span id=test1><img alt='' src='http://pravopmr.ru/Content/Images/News/attention.png' height=\"42\" width=\"42\"/></span>Test</div>";

    We are currently working on a way for the size of images to be obtained from its source, so the suggested workaround will no longer be required. You can track this item in the public portal for the availability of the functionality, hopefully, it should be ready in time for the upcoming Q2 2015 release.

    I hope this is useful.

    Regards,
    Petya
    Telerik
  8. Андрей
    Андрей avatar
    14 posts
    Member since:
    Dec 2014

    Posted 06 Jun 2015 in reply to Petya Link to this post

    Thank you very much.
  9. Manjrekar
    Manjrekar avatar
    3 posts
    Member since:
    Aug 2018

    Posted 30 Aug 2018 Link to this post

    Hi Team,

    Step 1. 

    We are trying to use the  $("#grid").kendoGrid({)); with some hard coded data on a html page.

    Step 2 

    HtmlFormatProvider formatProvider = new HtmlFormatProvider();
                formatProvider.ImportSettings.LoadFromUri += (s, e) =>
                {
                    string extension = Path.GetExtension(e.Uri);
                    Uri uri = new Uri(e.Uri, UriKind.RelativeOrAbsolute);
                    System.Net.WebClient oWebClient = new System.Net.WebClient();

                    using (MemoryStream stream = new MemoryStream())
                    {
                        var openRead = oWebClient.OpenRead(uri);
                        if (openRead != null)
                        {
                            openRead.CopyTo(stream);
                            e.SetData(stream.ToArray());
                        }
                    }
                };
                byte[] byteArray;
                RadFlowDocument document = formatProvider.Import(str);
                byteArray = pdfFormatProvider.Export(document);
               
                System.IO.File.WriteAllBytes(@"C:\temp\PDF\test.pdf", byteArray);

    LoadFromUri  is not downloading the grid data from html page from kendo grid.

    is this a limitation for HtmlFormatProvider,PdfFormatProvider ?

     

    Request you to reply to this quesiton.

     

  10. Tanya
    Admin
    Tanya avatar
    856 posts

    Posted 04 Sep 2018 Link to this post

    Hello Manjrekar,

    Could you provide more information about the setup you have on your end? To give a particular suggestion, we would need some additional details about the case. For example, are you loading a static HTML in the format provider? How do you pass the HTML? What is the exact behavior you are observing - is the Import() method called?

    Regards,
    Tanya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  11. Manjrekar
    Manjrekar avatar
    3 posts
    Member since:
    Aug 2018

    Posted 05 Sep 2018 in reply to Tanya Link to this post

    Hi Tanya,

    Scenario below

     

    1. I have a Abc.form,which has a kendo grid, which is loading from an Api.

    2. I have an Api,which would be calling the Abc.Form, which should in turn return html (http request).

    3. The dependencies for this form will be loaded from Import Method of HtmlFormatProvider,PdfFormatProvider .

    4. The grid data of kendo grid ,i wanted to save as a pdf.

     

    Let me know if you need more information.

     

  12. Manjrekar
    Manjrekar avatar
    3 posts
    Member since:
    Aug 2018

    Posted 07 Sep 2018 in reply to Manjrekar Link to this post

    Hi Tanya,

    Any updates on the above issue ?

     

    Request you to reply asap.

    Regards,

    Manish.

  13. Tanya
    Admin
    Tanya avatar
    856 posts

    Posted 10 Sep 2018 Link to this post

    Hello Manish,

    Would it be possible to share the HTML that is received so I can test the import functionality? I am still not sure what might be causing you issues and would like to test the exact content.

    Regards,
    Tanya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  14. Виктор
    Виктор avatar
    4 posts
    Member since:
    Aug 2018

    Posted 13 Sep 2018 Link to this post

    How instals?
  15. Tanya
    Admin
    Tanya avatar
    856 posts

    Posted 18 Sep 2018 Link to this post

    Hello Виктор,

    Do you need to install the Telerik Document Processing libraries? If so, they are distributed with several of our UI suites. You can find a list of the packages that include Telerik Document Processing in the documentation of the libraries at https://docs.telerik.com/devtools/document-processing. Each section in the Installing on Your Computer topic will redirect you to the installation information for the particular suite.

    Hope this information is helpful.

    Regards,
    Tanya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
  16. JeffSM
    JeffSM avatar
    42 posts
    Member since:
    May 2014

    Posted 08 May in reply to Petya Link to this post

    Dear,

    how do I ignore: formatProvider.ImportSettings.LoadFromUri

    because I have this problems for years and I need solve it soon as possible.

    best,

    Jeff

     

    #jeffersonMotta #telerikBugsJeff

  17. Tanya
    Admin
    Tanya avatar
    856 posts

    Posted 13 May Link to this post

    Hello Jeff,

    Can you please elaborate more on what are the problems you need to solve? WordsProcessing currently provides a built in mechanism for images whose data is referred using a URI source. You can find more information on the matter in the Settings help topic for HtmlFormatProvider.

    Regards,
    Tanya
    Progress Telerik
    Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Back to Top