Telerik Telerik
The Telerik Blogs

Programmatic export to PDF from Silverlight application

Wednesday, February 03, 2010 by Reporting Team | Comments 3

Ever wanted to export a Telerik report from a Silverlight application?

This is easily accomplished by instantiating the ReportServiceClient class, which plays the role of proxy to the Telerik Report Service.

To specify that you want to render the report we use the RenderAsync method of the ReportServiceClient, which invokes the respective method on the server (remember that it serves as proxy) that would return the rendered report.

Since we’ve used an asynchronous method and we do not know when the render would finish, we need to handle the RenderCompleted event. In the RenderCompleted event we get the result from the rendering and decide what to do with it -  in this case write down the report.\

An interesting part to note here is that we invoke the ShowDialog method prior to having the rendered report, because the dialog box, can only be called from user-initiated code, such as a button Click event. If ShowDialog is called from code that is not user-initiated, a SecurityException is thrown. More info on SaveFileDialog is available in this MSDN article.

Here is the code we've used:

public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }
  
        Stream file;
  
        private void Button_Click(object sender, RoutedEventArgs e)
        { 
            var fileDlg = new SaveFileDialog();
            fileDlg.Filter = "PDF files|*.pdf|All files|*.*";
              
            if (fileDlg.ShowDialog() == true)
            {
                this.file = fileDlg.OpenFile();
                var serviceClient = new ReportServiceClient(new Uri(App.Current.Host.Source, "../ReportService.svc"));
                serviceClient.RenderAsync("PDF", 
                    "Telerik.Reporting.Examples.CSharp.Report1, CSharp.ReportLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", 
                    null, 
                    null);
                serviceClient.RenderCompleted += new EventHandler<RenderEventArgs>(serviceClient_RenderCompleted);
            }
        }
  
        void serviceClient_RenderCompleted(object sender, RenderEventArgs e)
        {
            var result = e.RenderingResult;
            if (this.file != null)
            {
                this.file.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
                this.file.Close();
                this.file.Dispose();
                this.file = null;
            }
        }    
    }

 

and you'll also find attached a demo application.

[SilverlightExport]

P.S. Information about programmatic export and save of Telerik reports for web and windows forms applications is available in our documentation and KB sections:

Check the demo & enjoy!

Steve

3 Comments

  • Ben Hayat 05 Feb
    Steve, I like to ask you a question regarding the following scenario:

    Let's say we have a SL application that will generate a list of customers as PDF and saves it on the server at some URL location, so other apps can point to that PDF. So basically, this SL job is to create PDF and not display it.
    So, to do so, the user will have a series of check boxes to select the query of the customers and then a button to call the Report Service method to generate the report.

    Having that in mind, is there an event that will report back to SL client how much of the job has been completed, so the SL can display a progress bar to user about the completion of the job?
    OR
    Should the SL client based on some timer to check with the service to see how much of the job has been complete?

    What should be direction to show the progress?
    Secondly, is it possible for the service not to send the PDF to client and just save it on the server?

    Thanks!
    ..Ben
  • Aidi Minoccheri 28 Sep
    Hi Steve,
    I have the same problem of Ben, I'd like to know if is it possible for the service not to send the PDF to client but just save it on the server?

    Thanks
    Aidi
  • Aidi Minoccheri 28 Sep
    Hi Steve,
    I have the same problem of Ben, I'd like to know if is it possible for the service not to send the PDF to client but just save it on the server?

    Thanks
    Aidi

Add comment

  1. Formatting options
       
     
     
     
     
       
  2. (optional, emails won't be shown on public pages)
  3. (optional)