Exporting PDF from WebPage Using RadDocument & PdfFormatProvider

3 posts, 1 answers
  1. Stacey
    Stacey avatar
    2 posts
    Member since:
    Oct 2012

    Posted 05 Aug 2014 Link to this post

    I am trying to generate a pdf from a webpage and am able to get the text content, but not images to export. I know there are some settings with HtmlFormatProvider for how to export images, but I have been unable to successfully do this.

    Here is what I have so far:

    protected void ConvertToPdfButton_Click(object sender, ImageClickEventArgs e)
        byte[] buffer = new byte[0];
        string urlToConvert = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path);
        string html = string.Empty;
        var thread = new Thread(() =>
            PdfFormatProvider provider = new Telerik.Windows.Documents.FormatProviders.Pdf.PdfFormatProvider();
            HtmlFormatProvider htmlProvider = new HtmlFormatProvider();
            HtmlImportSettings importSettings = new HtmlImportSettings();
            importSettings.UseHtmlHeadingStyles = true;
            htmlProvider.ImportSettings = importSettings;
            var webRequest = HttpWebRequest.Create(urlToConvert);
            RadDocument document;
            using (Stream stream = webRequest.GetResponse().GetResponseStream())
                document = htmlProvider.Import(stream);
            document.LayoutMode = DocumentLayoutMode.Paged;
            buffer = provider.Export(document);
        MemoryStream ms = new MemoryStream(buffer);
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=DesignandBuildwithMetal PDF.pdf");
        Response.Buffer = true;
  2. Answer
    Petya avatar
    990 posts

    Posted 07 Aug 2014 Link to this post

    Hi Stacey,

    Creating images in the context of RadDocument creates a BitmapImage image source for each of the images. However, as you are working on a separate thread you need to freeze those sources to ensure they will be visible in the exported document.

    Additionally, if there are images loaded from url in the HTML, you need to download them. This can be done in the LoadImageFromUrl event handler of the provider's import settings.

    You can find an updated version of this example in the original blog post: Using an external library for the export to PDF functionality in Telerik’s ASP.NET Editor.

    I hope this is useful.

    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
  3. Stacey
    Stacey avatar
    2 posts
    Member since:
    Oct 2012

    Posted 07 Aug 2014 in reply to Petya Link to this post

    Hello Petya,

    I was able to get the images to output correctly to the pdf thanks to your information.

    One thing I would point out to anyone following this is that there is one section in that blog post that needs updated.

    You will get a null exception if you try using:
    provider.ImportSettings.LoadImageFromUrl += ImportSettings_LoadImageFromUrl
    // Instead use this
    HtmlImportSettings importSettings = new HtmlImportSettings();
    importSettings.LoadImageFromUrl += ImportSettings_LoadImageFromUrl;
    provider.ImportSettings = importSettings;
Back to Top