diff --git a/server/AyaNova/Startup.cs b/server/AyaNova/Startup.cs index 0802fa16..27efd5af 100644 --- a/server/AyaNova/Startup.cs +++ b/server/AyaNova/Startup.cs @@ -119,6 +119,8 @@ namespace AyaNova _newLog.LogDebug("Ensuring user and backup folders exist and are separate locations..."); FileUtil.EnsureUserAndUtilityFoldersExistAndAreNotIdentical(_hostingEnvironment.ContentRootPath); + //TODO: ENsure report files here + #region DATABASE diff --git a/server/AyaNova/biz/ReportBiz.cs b/server/AyaNova/biz/ReportBiz.cs index 05c62bc1..1dc4e231 100644 --- a/server/AyaNova/biz/ReportBiz.cs +++ b/server/AyaNova/biz/ReportBiz.cs @@ -1,9 +1,12 @@ using System.Threading.Tasks; using System.Linq; +using System.IO; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Logging; using AyaNova.Util; using AyaNova.Api.ControllerHelpers; using AyaNova.Models; +using PuppeteerSharp; namespace AyaNova.Biz { @@ -230,6 +233,74 @@ namespace AyaNova.Biz so the route just calls the biz object which handles processing, getting data, checking rights and then making the report and either attaching it to an email (maybe I do need that temp server folder after all) or return to route to return to Client end */ + public async Task RenderReport(long reportId, long[] objectidarray, string apiUrl) + { + + //get report, vet security, see what we need before init in case of issue + + + //initialization + var log = AyaNova.Util.ApplicationLogging.CreateLogger("ReportBiz::RenderReport"); + log.LogDebug("Initializing report system"); + var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "rpt"); + var lo = new LaunchOptions { Headless = true }; + bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows); + if (!isWindows) + { + log.LogDebug($"Not Windows: setting executable path for chrome to expected '/usr/bin/chromium-browser'"); + lo.ExecutablePath = "/usr/bin/chromium-browser";//this is the default path for docker based alpine dist, but maybe not others, need to make a config setting likely + lo.Args = new string[] { "--no-sandbox" }; + } + else + { + log.LogDebug($"Windows: Calling browserFetcher download async now:"); + await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); + } + + + log.LogDebug($"Launching headless Chrome now:"); + using (var browser = await Puppeteer.LaunchAsync(lo)) + using (var page = await browser.NewPageAsync()) + { + log.LogDebug($"Preparing page: adding base reporting scripts to page"); + + //Add Handlebars JS for compiling and presenting + await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-hb.js") }); + + //add Marked for markdown processing + await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-md.js") }); + + //add stock helpers + await page.AddScriptTagAsync(new AddTagOptions() { Path = Path.Combine(ReportJSFolderPath, "ay-report.js") }); + + //execute to add to handlebars + await page.EvaluateExpressionAsync("ayRegisterHelpers();"); + + log.LogDebug($"Preparing page: adding this report's scripts, style and templates to page"); + + + + //compile and run handlebars template + var compileScript = $"Handlebars.compile({reportTemplate})({reportData});"; + var resultHTML = await page.EvaluateExpressionAsync(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 }); + + //useful for debugging purposes only + //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"); + + } + return null; + }