This commit is contained in:
2020-09-04 19:24:17 +00:00
parent e40b8cfd00
commit e9341ea972
2 changed files with 62 additions and 47 deletions

View File

@@ -264,7 +264,14 @@ namespace AyaNova.Api.Controllers
}
catch (System.Exception ex)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, null, ex.Message));
//Don't send the full stack trace, just the initial error message from the javascript eval
// "Evaluation failed: ReferenceError: reportdata is not defined\n at reportPreRender (<anonymous>:5:17)\n at ayPreRender (C:\\data\\code\\raven\\server\\AyaNova\\resource\\rpt\\ay-report.js:13:12)\n at __puppeteer_evaluation_script__:10:25"
var v = ex.Message;
int positionOfNewLine = v.IndexOf("\n at");
if (positionOfNewLine >= 0)
v = v.Substring(0, positionOfNewLine);
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, null, v));
}
}

View File

@@ -331,54 +331,62 @@ namespace AyaNova.Biz
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");
//add report pre-render, helpers and style
//TODO: Add code to vet / evaluate these scripts and see if they are safe and contain valid methods expected
await page.AddScriptTagAsync(new AddTagOptions() { Content = report.JsPrerender });
await page.AddScriptTagAsync(new AddTagOptions() { Content = report.JsHelpers });
await page.AddStyleTagAsync(new AddTagOptions() { Content = report.Style });
var pagecontent = await page.GetContentAsync();
//compile and run handlebars template
// var compileScript = $"let ayReportData=ayPreRender({ReportData});Handlebars.compile(`{report.Template}`)(ayReportData);";
var compileScript = $"Handlebars.compile(`{report.Template}`)({{ayReportData:ayPreRender({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)
if (!string.IsNullOrWhiteSpace(report.Style))
try
{
await page.AddStyleTagAsync(new AddTagOptions { Content = report.Style });
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");
//add report pre-render, helpers and style
//TODO: Add code to vet / evaluate these scripts and see if they are safe and contain valid methods expected
await page.AddScriptTagAsync(new AddTagOptions() { Content = report.JsPrerender });
await page.AddScriptTagAsync(new AddTagOptions() { Content = report.JsHelpers });
await page.AddStyleTagAsync(new AddTagOptions() { Content = report.Style });
var pagecontent = await page.GetContentAsync();
//compile and run handlebars template
// var compileScript = $"let ayReportData=ayPreRender({ReportData});Handlebars.compile(`{report.Template}`)(ayReportData);";
var compileScript = $"Handlebars.compile(`{report.Template}`)({{ayReportData:ayPreRender({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)
if (!string.IsNullOrWhiteSpace(report.Style))
{
await page.AddStyleTagAsync(new AddTagOptions { Content = report.Style });
}
//If need the generated page content
//var pagecontent = await page.GetContentAsync();
string outputFileName = StringUtil.ReplaceLastOccurrence(FileUtil.NewRandomFileName, ".", "") + ".pdf";
string outputFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputFileName);
//render to pdf and return
await page.PdfAsync(outputFullPath);
log.LogDebug($"returning results now:");
return outputFileName;
}
catch (System.Exception ex)
{
// var v=await page.GetContentAsync();
throw ex;
}
//If need the generated page content
//var pagecontent = await page.GetContentAsync();
string outputFileName = StringUtil.ReplaceLastOccurrence(FileUtil.NewRandomFileName, ".", "") + ".pdf";
string outputFullPath = System.IO.Path.Combine(FileUtil.TemporaryFilesFolder, outputFileName);
//render to pdf and return
await page.PdfAsync(outputFullPath);
log.LogDebug($"returning results now:");
return outputFileName;
}
}