Save SSRS Report as PDF in C# programmatically
Asked Answered
T

2

12

I've read through multiple articles regarding this issue however they have all ended up with it not working, or are in vb.net.

What I currently have:

The reports are accessed via a URL which renders them as a PDF and saves them in the downloads folder when the user clicks on a button, these are given generic names such as OrderReport, OrderReport(1)... and so on.

var orderNum = 1;

"http://Server/ReportServer_Name/Pages/ReportViewer.aspx?%2fOrderReport&rs:Command=Render&OrderID=" + orderNum + "&rs:ClearSession=true&rs:Format=PDF"

What I am trying to acheive:

  • I would like to use C# to fetch this report if possible, and then specify a name for the PDF file and save it in the correct location.

so for example I would like to save this report in a temporary folder for now "C:\temp" with the name OrderID-1. I am using C#

I have added in a ServiceReference into the Project i am using called ReportTestings so the reference is

using ReportTestings;

and the Web Reference URL:

http://Server/ReportServer_Name/ReportExecution2005.asmx

(removed the actual names for security reasons)

so based on all of this information above could someone point me in the right direction or give an example part of code, Thankyou for all that read this post or help

using this code i get this error :(+ e

{"Access to the path 'C:\\Program Files (x86)\\IIS Express\\report1one.pdf' is denied."}    System.Exception {System.UnauthorizedAccessException})

code:

    ReportExecutionService rs = new ReportExecutionService();
    rs.Credentials = new NetworkCredential("username", "password", "domain");
    rs.Url = "http://Server/ReportServer_Name/reportexecution2005.asmx";

    // Render arguments
    byte[] result = null;
    string reportPath = "/Invoice";
    string format = "PDF";
    string historyID = null;
    string devInfo = @"<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";

    // Prepare report parameter.
    ParameterValue[] parameters = new ParameterValue[3];
    parameters[0] = new ParameterValue();
    parameters[0].Name = "InvoiceID";
    parameters[0].Value = "2";

    DataSourceCredentials[] credentials = null;
    string showHideToggle = null;
    string encoding;
    string mimeType;
    string extension;
    Warning[] warnings = null;
    ParameterValue[] reportHistoryParameters = null;
    string[] streamIDs = null;

    ExecutionInfo execInfo = new ExecutionInfo();
    ExecutionHeader execHeader = new ExecutionHeader();

    rs.ExecutionHeaderValue = execHeader;

    execInfo = rs.LoadReport(reportPath, historyID);

    rs.SetExecutionParameters(parameters, "en-us");
    String SessionId = rs.ExecutionHeaderValue.ExecutionID;

    Console.WriteLine("SessionID: {0}", rs.ExecutionHeaderValue.ExecutionID);


    try
    {
        result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

        execInfo = rs.GetExecutionInfo();

        Console.WriteLine("Execution date and time: {0}", execInfo.ExecutionDateTime);


    }
    catch (SoapException e)
    {
        Console.WriteLine(e.Detail.OuterXml);
    }
    // Write the contents of the report to an MHTML file.
    try
    {
        FileStream stream = File.Create("report1one.pdf", result.Length);
        Console.WriteLine("File created.");
        stream.Write(result, 0, result.Length);
        Console.WriteLine("Result written to the file.");
        stream.Close();
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
Tardy answered 7/5, 2015 at 8:15 Comment(0)
S
10

The webservice URL you are using (ReportService2012) is for managing the report server objects.

If you need to render reports, you should be using the ReportExecution2005 webservice.

To get started, you should take a look at the Render method.


To specify credentials you can add the folowing line (I'm the same variable name used in your link: RS2005):

RS2005.Credentials = new System.Net.NetworkCredential("username", "password", "domain");

EDIT:

Your access denied error occurs when your application try to save the file with your web application, so you should use an absolute path or resolve it using Server.MapPath

Sebiferous answered 7/5, 2015 at 8:55 Comment(9)
ive been trying this but i keep getting access denied when trying to save the report anywhere.. look at the above example ill add in now, if you could get past the access denied error that would help an awful lotTardy
The example in the link I provided (msdn.microsoft.com/en-us/library/…) is pretty straightforward, have you looked at it?Throughcomposed
Ok, so please edit your post and add the error detailsThroughcomposed
added in the code im working with and added in the part you have included regarding the Credentials but unfortunately, still the same error messageTardy
Can you try with an absolute path and see if it works? If you are using a relative path you should be using something like Server.MapPath to resolve it.Throughcomposed
ill have to look into what you mean by that, still very much a beginner at programming so ill get back to you once ive given it a goTardy
I did and unfortunately I didn't succeed in the Application I was creating, however I did get it working in a standalone application which is more annoying haha... Thanks for the helpTardy
I am getting this issue can some one help me The path of the item 'C:\SampleRDL' is not valid. The full path must be less than 260 characters long; other restrictions apply. If the report server is in native mode, the path must start with slashKrauss
@SébastienSevrin Can you have a look at: https://mcmap.net/q/1010061/-asp-net-core-download-ssrs-reports-in-pdf-format-on-client-browser/7124761 I m stuck from 7-8 hrsIbrahim
C
3

I recommend an amazingly simple implementation that I found at Crafted For Everyone. The author shows how to add a Service Reference to the SSRS server, and then provides sample code that worked for me on the first try.

It saves the report to a local file, but it is easy to send the report in an email (not included in sample.

Celisse answered 13/8, 2020 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.