Home / Community & Support / Knowledge Base / Telerik Reporting / Designing Reports / Exporting a report to PDF programmatically

Exporting a report to PDF programmatically

Article Info

Rating: 4

Article information

Article relates to

Telerik Reporting v6.x

Created by

Chavdar, Telerik

Last modified

August 24, 2012

Last modified by

Steve, Telerik



HOW-TO
Export a report to PDF programmatically

DESCRIPTION
Sometimes it is needed to export a report directly to PDF, without the need to go through the web viewer.

SOLUTION
You can use the following code snippet to programmatically export the report into "PDF" format from a Web Site or Web Application project:

C#

void ExportToPDF(Telerik.Reporting.Report reportToExport)
    {
        ReportProcessor reportProcessor = new ReportProcessor();
        Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
        instanceReportSource.ReportDocument = reportToExport;
        RenderingResult result = reportProcessor.RenderReport("PDF", instanceReportSource, null);
  
        string fileName = result.DocumentName + "." + result.Extension;
  
        Response.Clear();
        Response.ContentType = result.MimeType;
        Response.Cache.SetCacheability(HttpCacheability.Private);
        Response.Expires = -1;
        Response.Buffer = true;
  
        Response.AddHeader("Content-Disposition",
                           string.Format("{0};FileName=\"{1}\"",
                                         "attachment",
                                         fileName));
  
        Response.BinaryWrite(result.DocumentBytes);
        Response.End();
    }
 
VB.NET

Sub ExportToPDF(ByVal reportToExport As Telerik.Reporting.Report)   
   
        Dim reportProcessor As New ReportProcessor()
        Dim instanceReportSource As New Telerik.Reporting.InstanceReportSource()
        instanceReportSource.ReportDocument = reportToExport
        Dim result As RenderingResult = reportProcessor.RenderReport("PDF", instanceReportSource, Nothing)
  
        Dim fileName As String = result.DocumentName + "." + result.Extension 
        Response.Clear()   
        Response.ContentType = result.MimeType   
        Response.Cache.SetCacheability(HttpCacheability.Private)   
        Response.Expires = -1   
        Response.Buffer = True  
        Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName))   
        Response.BinaryWrite(result.DocumentBytes)   
        Response.End()   
    End Sub

Comments

  • Phil , Jul 17, 2007

    In VB?

  • Telerik Admin , Jul 18, 2007

    VB code added!

  • Phil , Jul 18, 2007

    Many thanks! I converted the code myself (and it worked, I might make a proper programmer yet!) ... but great response, as ever.

  • Jason McIntyre , Sep 4, 2007

    Exactly what we needed.

  • Jeff P , Sep 14, 2007

    Any way to get this report to come up in a new IE window automatically instead of the current one?

  • Telerik Admin , Sep 16, 2007

    Actually, jalmto, the report appears in a new window. In case you experience troubles wiht the code in the article, please open a support ticket and describe your case in detail. Thanks.

If you'd like to comment on this KB article, please, send us a Support Ticket.
Thank you!

Please Sign In to rate this article.