This commit is contained in:
@@ -161,11 +161,12 @@ namespace AyaNova.Api.Controllers
|
||||
|
||||
|
||||
|
||||
//======================================================================================================
|
||||
/// <summary>
|
||||
//======================================================================================================
|
||||
/// <summary>
|
||||
/// Render Report
|
||||
/// </summary>
|
||||
/// <param name="reportId"></param>
|
||||
/// <param name="reportId">Id of report template saved to db</param>
|
||||
/// <param name="objectIdArray">An array of id values for the object type specified by the report template</param>
|
||||
/// <param name="apiVersion">From route path</param>
|
||||
/// <returns></returns>
|
||||
[HttpPost("render")]
|
||||
@@ -178,16 +179,16 @@ namespace AyaNova.Api.Controllers
|
||||
return StatusCode(403, new ApiNotAuthorizedResponse());
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
|
||||
|
||||
var httpConnectionFeature = HttpContext.Features.Get<IHttpConnectionFeature>();
|
||||
var API_URL = $"http://127.0.0.1:{httpConnectionFeature.LocalPort}/api/v8/";
|
||||
|
||||
var result = await biz.RenderReport(reportId,objectIdArray,API_URL);
|
||||
var API_URL = $"http://127.0.0.1:{httpConnectionFeature.LocalPort}/api/v8/";
|
||||
|
||||
var result = await biz.RenderReport(reportId, objectIdArray, API_URL);
|
||||
if (result == null)
|
||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||
else
|
||||
return new FileContentResult(result.RenderedOutput,result.MimeType);
|
||||
|
||||
return new FileContentResult(result.RenderedOutput, result.MimeType);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -267,11 +267,12 @@ namespace AyaNova.Biz
|
||||
|
||||
|
||||
//Get data
|
||||
log.LogDebug($"Instantiating biz object handler for {report.ObjectType}");
|
||||
var biz = BizObjectFactory.GetBizObject(report.ObjectType, ct);
|
||||
var data = ((IReportAbleObject)biz).GetReportData(objectidarray);
|
||||
log.LogDebug($"Fetching data for {objectidarray.Length} {report.ObjectType} items");
|
||||
var data = ((IReportAbleObject)biz).GetReportData(objectidarray).ToString();
|
||||
|
||||
//initialization
|
||||
|
||||
log.LogDebug("Initializing report system");
|
||||
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
|
||||
var lo = new LaunchOptions { Headless = true };
|
||||
@@ -309,28 +310,30 @@ namespace AyaNova.Biz
|
||||
|
||||
log.LogDebug($"Preparing page: adding this report's scripts, style and templates to page");
|
||||
|
||||
|
||||
//TODO: CUSTOM HELPERS
|
||||
|
||||
//compile and run handlebars template
|
||||
var compileScript = $"Handlebars.compile({reportTemplate})({reportData});";
|
||||
var compileScript = $"let reportData=ayPreRender({data});Handlebars.compile({report.Template})(reportData);";
|
||||
var resultHTML = await page.EvaluateExpressionAsync<string>(compileScript);
|
||||
|
||||
//render report as HTML
|
||||
await page.SetContentAsync(resultHTML);
|
||||
|
||||
//add style (after page or it won't work)
|
||||
await page.AddStyleTagAsync(new AddTagOptions { Content = reportCSS });
|
||||
if (!string.IsNullOrWhiteSpace(report.Style))
|
||||
{
|
||||
await page.AddStyleTagAsync(new AddTagOptions { Content = report.Style });
|
||||
}
|
||||
|
||||
//useful for debugging purposes only
|
||||
//If need the generated page content
|
||||
//var pagecontent = await page.GetContentAsync();
|
||||
|
||||
//render to pdf and return
|
||||
var pdfBuffer = await page.PdfDataAsync();
|
||||
log.LogInformation($"returning results now:");
|
||||
return new FileContentResult(pdfBuffer, "application/pdf");
|
||||
log.LogDebug($"returning results now:");
|
||||
return new RenderedReport() { MimeType = "application/pdf", RenderedOutput = pdfBuffer };
|
||||
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public class RenderedReport
|
||||
|
||||
@@ -3,7 +3,17 @@ function ayRegisterHelpers() {
|
||||
return aString.toUpperCase();
|
||||
});
|
||||
|
||||
Handlebars.registerHelper("aymarkdown",function(astring){
|
||||
return marked(astring, { breaks: true })
|
||||
})
|
||||
Handlebars.registerHelper("aymarkdown", function (astring) {
|
||||
return marked(astring, { breaks: true });
|
||||
});
|
||||
}
|
||||
|
||||
function ayPreRender(theReportData) {
|
||||
if (reportPrerender != null) {
|
||||
return reportPreRender(theReportData);
|
||||
}else{
|
||||
return theReportData;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user