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

Exporting PDF from WebPage Using RadDocument & PdfFormatProvider

2 Answers 280 Views
Miscellaneous
This is a migrated thread and some comments may be shown as answers.
Stacey
Top achievements
Rank 1
Stacey asked on 05 Aug 2014, 08:14 PM
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();
 
        //Settings
        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;
        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=DesignandBuildwithMetal PDF.pdf");
 
    Response.Buffer = true;
    ms.WriteTo(Response.OutputStream);
    Response.End();
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Petya
Telerik team
answered on 07 Aug 2014, 01:52 PM
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.

Regards,
Petya
Telerik
 
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.
 
0
Stacey
Top achievements
Rank 1
answered on 07 Aug 2014, 03:55 PM
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;
Tags
Miscellaneous
Asked by
Stacey
Top achievements
Rank 1
Answers by
Petya
Telerik team
Stacey
Top achievements
Rank 1
Share this question
or