Reputation: 1287
I am trying to pass a PDF generated from a SQL ReportServer back to a UWP application as a PdfDocument. I keep getting an Exception when trying to create the StorageFile
[HttpGet]
[Route("invoicereport/{report}/{invoice}")]
public async Task<IHttpActionResult> GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
{
try
{
Debug.WriteLine("Test");
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = CredentialCache.DefaultCredentials;
rs.Url = "http://localhost/reportserver/reportexecution2005.asmx";
rs.ExecutionHeaderValue = new ExecutionHeader();
var executionInfo = new ExecutionInfo();
executionInfo = rs.LoadReport($"{ReportsDir}/Invoice/{report}", null);
List<ParameterValue> parameters = new List<ParameterValue>();
parameters.Add(new ParameterValue { Name = "SOPNUMBER", Value = invoice });
parameters.Add(new ParameterValue { Name = "SOPTypeString", Value = "Invoice" });
rs.SetExecutionParameters(parameters.ToArray(), "en-US");
string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
string mimeType;
string encoding;
string[] streamId;
Warning[] warning;
var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId);
FileStream stream = File.Create(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf", result.Length);
//write file with rendered result
stream.Write(result, 0, result.Length);
//close stream
stream.Close();
StorageFile file = StorageFile.GetFileFromPathAsync(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf").GetResults();
var pdf = PdfDocument.LoadFromFileAsync(file).GetResults();
return Ok(pdf);
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Reporting Error");
}
return Ok();
}
the Exception gets logged as:
System.IO.FileNotFoundException: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
at Windows.Foundation.IAsyncOperation`1.GetResults()
at Prism.Web.Queries.Controllers.ReportController.<GetInvoiceReport>d__6.MoveNext() in C:\Projects\PointOfSaleBeta\Prism.Web.Queries\Controllers\ReportController.cs:line 183
Line 183 corresponds to StorageFile file = StorageFile.GetFileFromPathAsync(System.Web.HttpRuntime.CodegenDir + $"/{invoice}.pdf").GetResults();
I have verified (many times) that the referenced PDF file actually got created. Am I not using the correct syntax for 'GetFileFromPathAsync`?
Upvotes: 0
Views: 214
Reputation: 1287
After I didn't get any answers or comments I tried a different approach. I installed the FreeSpire.PDF nuget package:
[HttpGet]
[Route("invoicereport/{report}/{invoice}")]
public async Task<IHttpActionResult> GetInvoiceReport([FromUri] string report, [FromUri] string invoice)
{
try
{
Debug.WriteLine("Test");
ReportExecutionService rs = new ReportExecutionService();
rs.Credentials = CredentialCache.DefaultCredentials;
rs.Url = "http://localhost/reportserver/reportexecution2005.asmx";
rs.ExecutionHeaderValue = new ExecutionHeader();
var executionInfo = new ExecutionInfo();
executionInfo = rs.LoadReport($"{ReportsDir}/Invoice/{report}", null);
List<ParameterValue> parameters = new List<ParameterValue>();
parameters.Add(new ParameterValue { Name = "SOPNUMBER", Value = invoice });
parameters.Add(new ParameterValue { Name = "SOPTypeString", Value = "Invoice" });
rs.SetExecutionParameters(parameters.ToArray(), "en-US");
string deviceInfo = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>";
string mimeType;
string encoding;
string[] streamId;
Warning[] warning;
var result = rs.Render("PDF", deviceInfo, out mimeType, out encoding, out encoding, out warning, out streamId);
PdfDocument pdf = new PdfDocument();
pdf.LoadFromBytes(result);
return Ok(pdf);
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Reporting Error");
}
return Ok();
}
This solved (worked around) my problem.
Upvotes: 0