This commit is contained in:
2020-08-24 16:23:20 +00:00
parent 3db5734317
commit c462d1e977

View File

@@ -38,7 +38,7 @@ namespace AyaNova.Api.Controllers
[HttpGet("render/{test}")]
[HttpGet("render-test")]
[AllowAnonymous]
public async Task<IActionResult> GetTestReport([FromRoute] string test)
{
@@ -70,29 +70,58 @@ namespace AyaNova.Api.Controllers
}
[HttpGet("render/pdf/{url}")]
//---------------------------------------------------------------------
[HttpGet("render")]
[AllowAnonymous]
public async Task<IActionResult> GetUrlPdfReport([FromRoute] string url)
public async Task<IActionResult> GetPdfReport([FromRoute] string url)
{
if (!serverState.IsOpen)
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
string outputFile = FileUtil.NewRandomTempFilesFolderFileName;
// byte[] buffer = readfromdatabase();
// return new FileContentResult(buffer, "text/html");
//first test, just render a web page to pdf and return it
//return PhysicalFile(filePath, mimetype, dbObject.DisplayFileName);
outputFile += ".pdf";
//http://www.puppeteersharp.com/api/index.html
//https://github.com/hardkoded/puppeteer-sharp
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
var browser = await Puppeteer.LaunchAsync(new LaunchOptions
//sample CSS
var templateCSS = @"
@page {
margin: 1cm;
}
@page :first {
margin: 2cm;
}";
//sample HTML
var templateHtml = "<div><h1>Hello world!</h1></div>";
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
using (var page = await browser.NewPageAsync())
{
Headless = true
});
var page = await browser.NewPageAsync();
await page.GoToAsync(url);
await page.PdfAsync(outputFile);
return PhysicalFile(outputFile, "application/pdf");
await page.AddStyleTagAsync(new AddTagOptions { Content = templateCSS });
await page.SetContentAsync(templateHtml);
var pdfBuffer = await page.PdfDataAsync();
return new FileContentResult(pdfBuffer, "application/pdf");
}
}
//-----------------------------------------
//------------
/*
NOTES/TODO during testing