Export HTML Content to PDF using RadDocument : How to render

Posted by Community Admin on 04-Aug-2018 04:08

Export HTML Content to PDF using RadDocument : How to render the images?

All Replies

Posted by Community Admin on 03-Oct-2014 00:00

Hi All,

Now i'm facing an issue to export HTML content to PDF format using RadDocument. Plain Text Worked fine but images not rendered. Is there any particular ways to achieve that?
FYI, I'm using source code from Steve.

http://www.sitefinitysteve.com/blog/code/2014/07/30/export-html-or-content-to-pdf-word-etc-with-sitefinity

Here is my codes :

protected void OnDownloadPDF_Click(object sender, EventArgs e)
    string output = "";
    byte[] buffer = new byte[0];
  
    string filename = "file"
    string html = "<div>Hello World</div>"; // worked Fine
         
    // IF USING
    string html = "<img src='http://somewebsite.com/image/path.png' />"; // image not rendered
 
  
    var thread = new Thread(() =>
    
        Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider provider = new Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider();
        HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
  
        //Settings
        HtmlImportSettings importSettings = new HtmlImportSettings();
        importSettings.LoadImageFromUrl += importSettings_LoadImageFromUrl;
        importSettings.UseHtmlHeadingStyles = true;
  
        htmlProvider.ImportSettings = importSettings;
  
        RadDocument document = htmlProvider.Import(html);
        document.LayoutMode = DocumentLayoutMode.Paged;
        document.EnsureDocumentMeasuredAndArranged();
  
        buffer = provider.Export(document);
    );
  
    thread.SetApartmentState(ApartmentState.STA);
    thread.Start();
    thread.Join();
  
    Response.Clear();
    MemoryStream ms = new MemoryStream(buffer);
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-disposition", "attachment;filename=0.pdf".Arrange(filename));
    Response.Buffer = true;
    ms.WriteTo(Response.OutputStream);
    Response.End();
  
  
public static void importSettings_LoadImageFromUrl(object sender, LoadImageEventArgs e)
    try
    
        WebClient webClient = new WebClient();
        var uri = new Uri(e.Url);
  
        var bytes = webClient.DownloadData(uri);
  
        var extension = webClient.ResponseHeaders["Content-Type"].ToString();
        extension = extension.Replace("image/","");
        if(String.IsNullOrEmpty(extension))
            extension = "png";
        
  
        Stream stream = new MemoryStream(bytes);
        e.Handled = true;
        e.ImageElement.Init(stream, extension.ToLower());
    catch(Exception ex)
         
    


Appreciate for your help!

 

Thanks and best regards,

Dion Alexander


Posted by Community Admin on 07-Oct-2014 00:00

Thread closed. Absolute URL worked fine though.

This thread is closed