This is a migrated thread and some comments may be shown as answers.

Error when you import images in html HtmlFormatProvider

16 Answers 749 Views
WordsProcessing
This is a migrated thread and some comments may be shown as answers.
Андрей
Top achievements
Rank 1
Андрей asked on 31 May 2015, 12:31 PM

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?

16 Answers, 1 is accepted

Sort by
0
Accepted
Petya
Telerik team
answered on 01 Jun 2015, 05:06 PM
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
0
Андрей
Top achievements
Rank 1
answered on 01 Jun 2015, 07:44 PM

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

this.document = provider.Import(str);

0
Андрей
Top achievements
Rank 1
answered on 01 Jun 2015, 08:30 PM

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

0
Petya
Telerik team
answered on 02 Jun 2015, 10:49 AM
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
0
Андрей
Top achievements
Rank 1
answered on 02 Jun 2015, 11:28 AM

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();
}

0
Petya
Telerik team
answered on 05 Jun 2015, 10:26 AM
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 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
0
Андрей
Top achievements
Rank 1
answered on 06 Jun 2015, 06:56 AM
Thank you very much.
0
Manjrekar
Top achievements
Rank 1
answered on 30 Aug 2018, 04:05 PM

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.

 

0
Tanya
Telerik team
answered on 04 Sep 2018, 01:38 PM
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.
0
Manjrekar
Top achievements
Rank 1
answered on 05 Sep 2018, 10:35 AM

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.

 

0
Manjrekar
Top achievements
Rank 1
answered on 07 Sep 2018, 05:13 AM

Hi Tanya,

Any updates on the above issue ?

 

Request you to reply asap.

Regards,

Manish.

0
Tanya
Telerik team
answered on 10 Sep 2018, 09:11 AM
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 and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Виктор
Top achievements
Rank 1
answered on 13 Sep 2018, 05:15 PM
How instals?
0
Tanya
Telerik team
answered on 18 Sep 2018, 03:07 PM
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 and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
answered on 09 May 2019, 04:05 AM

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

0
Tanya
Telerik team
answered on 13 May 2019, 09:46 AM
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.
Tags
WordsProcessing
Asked by
Андрей
Top achievements
Rank 1
Answers by
Petya
Telerik team
Андрей
Top achievements
Rank 1
Manjrekar
Top achievements
Rank 1
Tanya
Telerik team
Виктор
Top achievements
Rank 1
JeffSM
Top achievements
Rank 2
Iron
Veteran
Iron
Share this question
or