This commit is contained in:
@@ -161,11 +161,12 @@ namespace AyaNova.Api.Controllers
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
//======================================================================================================
|
//======================================================================================================
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Render Report
|
/// Render Report
|
||||||
/// </summary>
|
/// </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>
|
/// <param name="apiVersion">From route path</param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
[HttpPost("render")]
|
[HttpPost("render")]
|
||||||
@@ -182,11 +183,11 @@ namespace AyaNova.Api.Controllers
|
|||||||
var httpConnectionFeature = HttpContext.Features.Get<IHttpConnectionFeature>();
|
var httpConnectionFeature = HttpContext.Features.Get<IHttpConnectionFeature>();
|
||||||
var API_URL = $"http://127.0.0.1:{httpConnectionFeature.LocalPort}/api/v8/";
|
var API_URL = $"http://127.0.0.1:{httpConnectionFeature.LocalPort}/api/v8/";
|
||||||
|
|
||||||
var result = await biz.RenderReport(reportId,objectIdArray,API_URL);
|
var result = await biz.RenderReport(reportId, objectIdArray, API_URL);
|
||||||
if (result == null)
|
if (result == null)
|
||||||
return BadRequest(new ApiErrorResponse(biz.Errors));
|
return BadRequest(new ApiErrorResponse(biz.Errors));
|
||||||
else
|
else
|
||||||
return new FileContentResult(result.RenderedOutput,result.MimeType);
|
return new FileContentResult(result.RenderedOutput, result.MimeType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -267,11 +267,12 @@ namespace AyaNova.Biz
|
|||||||
|
|
||||||
|
|
||||||
//Get data
|
//Get data
|
||||||
|
log.LogDebug($"Instantiating biz object handler for {report.ObjectType}");
|
||||||
var biz = BizObjectFactory.GetBizObject(report.ObjectType, ct);
|
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
|
//initialization
|
||||||
|
|
||||||
log.LogDebug("Initializing report system");
|
log.LogDebug("Initializing report system");
|
||||||
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
|
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt");
|
||||||
var lo = new LaunchOptions { Headless = true };
|
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");
|
log.LogDebug($"Preparing page: adding this report's scripts, style and templates to page");
|
||||||
|
|
||||||
|
//TODO: CUSTOM HELPERS
|
||||||
|
|
||||||
//compile and run handlebars template
|
//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);
|
var resultHTML = await page.EvaluateExpressionAsync<string>(compileScript);
|
||||||
|
|
||||||
//render report as HTML
|
//render report as HTML
|
||||||
await page.SetContentAsync(resultHTML);
|
await page.SetContentAsync(resultHTML);
|
||||||
|
|
||||||
//add style (after page or it won't work)
|
//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();
|
//var pagecontent = await page.GetContentAsync();
|
||||||
|
|
||||||
//render to pdf and return
|
//render to pdf and return
|
||||||
var pdfBuffer = await page.PdfDataAsync();
|
var pdfBuffer = await page.PdfDataAsync();
|
||||||
log.LogInformation($"returning results now:");
|
log.LogDebug($"returning results now:");
|
||||||
return new FileContentResult(pdfBuffer, "application/pdf");
|
return new RenderedReport() { MimeType = "application/pdf", RenderedOutput = pdfBuffer };
|
||||||
|
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class RenderedReport
|
public class RenderedReport
|
||||||
|
|||||||
@@ -3,7 +3,17 @@ function ayRegisterHelpers() {
|
|||||||
return aString.toUpperCase();
|
return aString.toUpperCase();
|
||||||
});
|
});
|
||||||
|
|
||||||
Handlebars.registerHelper("aymarkdown",function(astring){
|
Handlebars.registerHelper("aymarkdown", function (astring) {
|
||||||
return marked(astring, { breaks: true })
|
return marked(astring, { breaks: true });
|
||||||
})
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ayPreRender(theReportData) {
|
||||||
|
if (reportPrerender != null) {
|
||||||
|
return reportPreRender(theReportData);
|
||||||
|
}else{
|
||||||
|
return theReportData;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user