Home > Enterprise >  How to download a pdf file from SSRS (Sql Server Reporting Service) using Vue and C# controllers?
How to download a pdf file from SSRS (Sql Server Reporting Service) using Vue and C# controllers?

Time:01-08

Looking to add a clickable link to a C# web application that is connect to SSRS via an API. When clicking the link, the application should download the pdf file from SSRS.

The current implementation downloads the file, but the contents are blank. Would appreciate some help to make it so that the data appears in the pdf.

Controller.cs

[HttpGet]
[Route("ssrs")]
public async Task<IHttpActionResult> GetSsrsReportAsync()
{
    var clientHandler = new HttpClientHandler();
    clientHandler.UseDefaultCredentials = true;
    clientHandler.PreAuthenticate = true;
    clientHandler.ClientCertificateOptions = ClientCertificateOption.Automatic;
    var client = new HttpClient(clientHandler);

    byte[] response = await client.GetByteArrayAsync("http://***.***.local/ReportServer?/proj_20800014/SE_SurfaceWater_Tbl&rs:Command=Render&rs:Format=PDF"); // returns html cause its a webpage
                                                                                                                                                                          
    return new FileResult(new MemoryStream(response), Request, "HelloWorld.pdf");
}

reports.vue

methods: {
    GetSsrsReport() {            
        axios.get(`/api/library/portfolio/ssrs`)
            .then(results => {
                console.log(results.data);
                const fileName = 'HelloWorld.pdf';
                let theNewFile = atob(results.data);
                let blob = new Blob([theNewFile]);
                if (navigator.msSaveBlob) {
                     return navigator.msSaveBlob(blob, fileName);
                }
                const url = window.URL.createObjectURL(blob);
                const link = document.createElement('a');
                link.href = url;
                link.setAttribute('download', fileName);
                document.body.appendChild(link);
                link.click();
                blob = null;
            })
            .catch(error => {
                console.error(error);
            });
    },
}

CodePudding user response:

The native ReportServer combined with the rs:Format=PDF parameter should ALWAYS be a PDF doc, not html.

The issue is probably that you have combined the rs:Command=Render argument which is processed first so the rs:Format is ignored.

Just try this URL instead:

http://***.***.local/ReportServer?/proj_20800014/SE_SurfaceWater_Tbl&rs:Format=PDF

To verify, just save the byte[] response to a file and verify you can open it as the expected PDF.

This is ambiguous in the documentation: Export a Report Using URL Access

CodePudding user response:

I tried

 byte[] response = await client.GetByteArrayAsync("http://europa.ddms.local/ReportServer?/proj_20800014/SE_SurfaceWater_Tbl_pdf&rs:Format=PDF");
 var result = Convert.ToBase64String(response);
 return Json(result);

but then having issues converting that string to a pdf file on the js side of things. running into a "Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded error"

  •  Tags:  
  • Related