7

I have one SSRS report URL which if I enter that URL in the browser then it shows the SSRS report with Print and Save option like:

enter image description here

Which works fine on the browser.

What I want, there is one button on .cshtml page so by clicking on that button I need to download the report on client browser.

URL: http://xyz/ReportS/report/UAT/SampleReport_V1?Srno=X123

What I tried:

by setting &rs:Format=PDF and made a HTTP Request from Asp.Net Core Application but it generated PDF but in corrupt format.

Code:

public void Download_SSRSInPDF()
{
    string URL = "http://xyz/ReportS/report/UAT/SampleReport_V1";
    string Command = "Render";
    string Format = "PDF";
    URL = URL + "?SrNo=X123&rs:Command=" + Command + "&rs:Format=" + Format;
    System.Net.HttpWebRequest Req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);

    Req.Credentials = System.Net.CredentialCache.DefaultCredentials;
    Req.Method = "GET";
    string path = @"E:\New folder\Test.pdf";
    System.Net.WebResponse objResponse = Req.GetResponse();
    System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create);
    System.IO.Stream stream = objResponse.GetResponseStream();
    byte[] buf = new byte[1024];
    int len = stream.Read(buf, 0, 1024);

    while (len > 0)
    {
        fs.Write(buf, 0, len);
        len = stream.Read(buf, 0, 1024);
    }
    stream.Close();
    fs.Close();
}

How to do it in Asp.Net Core?

EDIT::

I have that asmx url of my report:

http://server-name/ReportServer/reportexecution2005.asmx

But unable to proceed with this, can someone points me towards documentation?

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84

3 Answers3

5

If you want a simple URL to download the report in PDF format, don't add parameters to the web portal URL but add them to an URL that points to the web service:

string URL = "http://xyz/reportserver/?/UAT/SampleReport_V1";
string Command = "Render";
string Format = "PDF";
URL = URL + "&SrNo=X123&rs:Command=" + Command + "&rs:Format=" + Format;
Wolfgang Kais
  • 4,010
  • 2
  • 10
  • 17
  • @PrashantPimpale: It's not the same code. I replaced `ReportS/report` by `reportserver`, which is the default name of the web service. Also, I inserted the question mark there, because the path to the report should be the first parameter of the web service URL. At last, I replaced your question mark (in the last line of the snippet to be replaced) by an ampersand (`&`). What comes out when you replace your 4 lines by mine? – Wolfgang Kais Sep 24 '19 at 17:35
  • @PrashantPimpale: The complete URL should look like this: `http://xyz/reportserver/?/UAT/SampleReport_V1&SrNo=X123&rs:Command=Render&rs:Format=PDF`, for testing, paste it in your browser. – Wolfgang Kais Sep 24 '19 at 17:42
  • Yup, its generating the expected PDF! Why we have used that `?` – Prashant Pimpale Sep 25 '19 at 05:38
  • + as mentioned by Nick it gives me error when I try that URL in the another PC (Who don't have access to the SSRS report to view), how do I pass some default credentials? – Prashant Pimpale Sep 25 '19 at 05:53
  • @PrashantPimpale The `?` is the separator between the web service URL and everything that is "parameters" for the request. As for the credentials: You are passing the "DefaultCredentials". If those aren't sufficient to access the report and you want to bypass the permissions granted to the user, just fill the `Req.Credentials` with credentials that allow access to the report. – Wolfgang Kais Sep 25 '19 at 08:39
  • Okay, will try and let you know – Prashant Pimpale Sep 25 '19 at 08:42
  • Done! Now the file has generated successully in the specified folder! Just one more thing if you can help, it is possible to generated the file on the browser itself, instead of generating in folder? – Prashant Pimpale Sep 25 '19 at 09:09
  • @PrashantPimpale Great to hear that. To display the PDF in the browser, I suggest to use some `Response.Redirect` to the generated URL. You might find a solution [here](https://stackoverflow.com/questions/104601/response-redirect-to-new-window) (meaning that the user must be permitted to see the report). – Wolfgang Kais Sep 25 '19 at 09:43
  • In asp.net we use `Response.Write()` and `Response.Clear()` which will generate a file on the client browser by using ByteArray, so just want to know if that is possible in asp.net core with about code – Prashant Pimpale Sep 25 '19 at 09:56
  • Hi, I need to write that returned bytes on the client's browser – Prashant Pimpale Oct 09 '19 at 13:07
  • @PrashantPimpale Probably [this](https://stackoverflow.com/a/40488246) will help you. – Wolfgang Kais Oct 10 '19 at 16:30
  • I tried but none of those worked..i m stuck.. Need your help..i have also posted a new question for this – Prashant Pimpale Oct 11 '19 at 01:39
3

You're better off doing this using the supported ReportExecution2005.asmx endpoint with something like WCF, instead of trying to use HTTP web request to impersonate a user interaction in a browser.

Once you've got it setup, there are Render methods on the report execution instance that make it really easy to get the report as a PDF.

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
1

you are converting to pdf with default converter of code. we do not follow this method to convert reports in to pdf. If you wanna ask more option which is lengthy but not tough to do. You can use WKHTMLtopdf method to convert your html in to pdf. it is easy to use. I am not saying that the method you are using is wrong. but we need to use latest technologies. Asp.netcore is working fine with wkhtmltopdf to convert html page in to pdf.

Love Pandey
  • 330
  • 2
  • 9