This commit is contained in:
@@ -38,7 +38,7 @@ namespace AyaNova.Api.Controllers
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("render/{test}")]
|
[HttpGet("render-test")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public async Task<IActionResult> GetTestReport([FromRoute] string test)
|
public async Task<IActionResult> GetTestReport([FromRoute] string test)
|
||||||
{
|
{
|
||||||
@@ -70,29 +70,58 @@ namespace AyaNova.Api.Controllers
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
[HttpGet("render/pdf/{url}")]
|
|
||||||
|
//---------------------------------------------------------------------
|
||||||
|
[HttpGet("render")]
|
||||||
[AllowAnonymous]
|
[AllowAnonymous]
|
||||||
public async Task<IActionResult> GetUrlPdfReport([FromRoute] string url)
|
public async Task<IActionResult> GetPdfReport([FromRoute] string url)
|
||||||
{
|
{
|
||||||
if (!serverState.IsOpen)
|
if (!serverState.IsOpen)
|
||||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
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
|
//first test, just render a web page to pdf and return it
|
||||||
//return PhysicalFile(filePath, mimetype, dbObject.DisplayFileName);
|
//return PhysicalFile(filePath, mimetype, dbObject.DisplayFileName);
|
||||||
outputFile += ".pdf";
|
|
||||||
//http://www.puppeteersharp.com/api/index.html
|
//http://www.puppeteersharp.com/api/index.html
|
||||||
//https://github.com/hardkoded/puppeteer-sharp
|
//https://github.com/hardkoded/puppeteer-sharp
|
||||||
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
|
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
|
|
||||||
});
|
await page.AddStyleTagAsync(new AddTagOptions { Content = templateCSS });
|
||||||
var page = await browser.NewPageAsync();
|
await page.SetContentAsync(templateHtml);
|
||||||
await page.GoToAsync(url);
|
|
||||||
await page.PdfAsync(outputFile);
|
var pdfBuffer = await page.PdfDataAsync();
|
||||||
return PhysicalFile(outputFile, "application/pdf");
|
return new FileContentResult(pdfBuffer, "application/pdf");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//-----------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//------------
|
//------------
|
||||||
/*
|
/*
|
||||||
NOTES/TODO during testing
|
NOTES/TODO during testing
|
||||||
|
|||||||
Reference in New Issue
Block a user