Export SSRS Report to PDF without using ReportViewer in ASP.Net

Last Reply on Jun 03, 2015 08:09 AM By EmadKhan

Posted on Jun 01, 2015 02:30 AM

Hi! I am using one report viewer and one asp button. On button click i am passing parameters to the report and then generating it in report viewer. Now i want that instead of showing it in report viewer it should be shown in any pdf viewer in pdf format. Also it would have a download pdf option so that user can download that pdf.

Posted on Jun 03, 2015 08:09 AM

This is the solution i came up with :

 public void ExportToPDF(string path, List<ReportParameter> reportParams,string fileName)
    {

        Warning[] warnings;
        string[] streamIds;
        string mimeType = string.Empty;
        string encoding = string.Empty;
        string extension = string.Empty;

        // Setup the report viewer object and get the array of bytes
        var viewer = new ReportViewer();
        viewer.ProcessingMode = ProcessingMode.Remote;
        viewer.ServerReport.ReportServerUrl = new Uri("http://localhost/ReportServer");
        viewer.ServerReport.ReportPath = "/"+path;
        viewer.ServerReport.SetParameters(reportParams);
 
        byte[] bytes = viewer.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension,
            out streamIds, out warnings);


        // Now that you have all the bytes representing the PDF report, buffer it and send it to the client.
        Response.Buffer = true;
        Response.Clear();
        Response.ContentType = mimeType;
        Response.AddHeader("content-disposition", "attachment; filename="+fileName+"." + extension);
        Response.BinaryWrite(bytes); // create the file
        Response.Flush(); // send it to the client to download
    }