This commit is contained in:
2020-08-24 17:27:50 +00:00
parent c462d1e977
commit 70fb429fd1
2 changed files with 25 additions and 9 deletions

View File

@@ -1,5 +1,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.AspNetCore.Authorization;
@@ -8,6 +9,7 @@ using AyaNova.Models;
using AyaNova.Api.ControllerHelpers;
using AyaNova.Biz;
using AyaNova.Util;
using PuppeteerSharp;
namespace AyaNova.Api.Controllers
@@ -79,16 +81,16 @@ namespace AyaNova.Api.Controllers
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
// byte[] buffer = readfromdatabase();
// return new FileContentResult(buffer, "text/html");
var ReportJSFolderPath = Path.Combine(ServerBootConfig.AYANOVA_CONTENT_ROOT_PATH, "resource", "reportjs");
if (!Directory.Exists(ReportJSFolderPath))
throw new System.Exception($"E1012: \"reportjs\" folder not found where expected: \"{ReportJSFolderPath}\", installation damaged?");
var hbspath = Path.Combine(ReportJSFolderPath, "ayhandlebars.js");
if (!System.IO.File.Exists(hbspath))
throw new System.Exception($"E1012: \"ayhandlebars.js\" file not found where expected: \"{hbspath}\", installation damaged?");
//first test, just render a web page to pdf and return it
//return PhysicalFile(filePath, mimetype, dbObject.DisplayFileName);
//http://www.puppeteersharp.com/api/index.html
//https://github.com/hardkoded/puppeteer-sharp
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
//sample CSS
@@ -101,15 +103,29 @@ namespace AyaNova.Api.Controllers
margin: 2cm;
}";
//sample HTML
var templateHtml = "<div><h1>Hello world!</h1></div>";
//sample template
var templateHtml = "<div>{{#with person}}{{firstname}} {{lastname}}{{/with}}</div>";
//data object
var jsonData = "{person: {firstname: \"Yehuda\",lastname: \"Katz\",}}";
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
using (var page = await browser.NewPageAsync())
{
//Add handlebars JS for compiling and presenting
await page.AddScriptTagAsync(new AddTagOptions() { Path = hbspath});
await page.AddStyleTagAsync(new AddTagOptions { Content = templateCSS });
await page.SetContentAsync(templateHtml);
//compile into function
//get result by running hb function against data
//open page with result
//pdf-ize and return
var pdfBuffer = await page.PdfDataAsync();
return new FileContentResult(pdfBuffer, "application/pdf");