This commit is contained in:
2020-08-26 19:25:32 +00:00
parent a939203f4e
commit 795c5893cf
2 changed files with 73 additions and 0 deletions

View File

@@ -119,6 +119,8 @@ namespace AyaNova
_newLog.LogDebug("Ensuring user and backup folders exist and are separate locations..."); _newLog.LogDebug("Ensuring user and backup folders exist and are separate locations...");
FileUtil.EnsureUserAndUtilityFoldersExistAndAreNotIdentical(_hostingEnvironment.ContentRootPath); FileUtil.EnsureUserAndUtilityFoldersExistAndAreNotIdentical(_hostingEnvironment.ContentRootPath);
//TODO: ENsure report files here
#region DATABASE #region DATABASE

View File

@@ -1,9 +1,12 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Linq; using System.Linq;
using System.IO;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using AyaNova.Util; using AyaNova.Util;
using AyaNova.Api.ControllerHelpers; using AyaNova.Api.ControllerHelpers;
using AyaNova.Models; using AyaNova.Models;
using PuppeteerSharp;
namespace AyaNova.Biz 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) 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 or return to route to return to Client end
*/ */
public async Task<byte[]> 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<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 });
//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;
}