+1-888-365-2779
Try Now
More in this section

Forums / Bugs & Issues / Export HTML Content to PDF using RadDocument : How to render the images?

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

2 posts, 1 answered
  1. Dion
    Dion avatar
    6 posts
    Registered:
    29 Mar 2014
    03 Oct 2014
    Link to this post

    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


    Answered
  2. Dion
    Dion avatar
    6 posts
    Registered:
    29 Mar 2014
    07 Oct 2014
    Link to this post
    Thread closed. Absolute URL worked fine though.
2 posts, 1 answered