|
|
|
@@ -1,23 +1,24 @@ |
|
|
|
using System.Web; |
|
|
|
using BlackRock.Reporting.API.Core; |
|
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
|
using PuppeteerSharp; |
|
|
|
|
|
|
|
namespace BlackRock.Reporting.API.Controllers |
|
|
|
{ |
|
|
|
[Route("api/[controller]")] |
|
|
|
public class PDFGeneratorController: Controller |
|
|
|
public class PDFGeneratorController : Controller |
|
|
|
{ |
|
|
|
private readonly IHostEnvironment host; |
|
|
|
private readonly IGenerator generator; |
|
|
|
|
|
|
|
public PDFGeneratorController(IHostEnvironment host) |
|
|
|
public PDFGeneratorController(IHostEnvironment host, IGenerator generator) |
|
|
|
{ |
|
|
|
this.generator = generator; |
|
|
|
this.host = host; |
|
|
|
} |
|
|
|
// Pristize URL adresa izvora web stranice koju treba eksportovati u PDF fajl |
|
|
|
|
|
|
|
[HttpGet("{url}")] |
|
|
|
public async Task<IActionResult> Get(string url= "http://localhost:3000/#/dashboard") |
|
|
|
public async Task<IActionResult> Get(string url = "http://localhost:3000/#/dashboard") |
|
|
|
{ |
|
|
|
// Service ima endpint na koji stize zahtev sa urlom strance koju treba eksportovati u PDF |
|
|
|
if (string.IsNullOrEmpty(url)) |
|
|
|
return BadRequest(); |
|
|
|
|
|
|
|
@@ -26,82 +27,9 @@ namespace BlackRock.Reporting.API.Controllers |
|
|
|
var fileName = Guid.NewGuid().ToString() + ".pdf"; |
|
|
|
// Path to wwwroot folder combined with name of pdf file |
|
|
|
string path = Path.Combine(host.ContentRootPath, $"wwwroot/pdfs/{fileName}"); |
|
|
|
// string path = Path.Combine(host.WebRootPath, fileName); |
|
|
|
// Generate PDF file in wwwroot |
|
|
|
// Service obradjuje zahtev |
|
|
|
// U pozadini se podize chromium koji cemo koristiti za print |
|
|
|
// Pupeter otvara stranicu sa zadatim urlom |
|
|
|
// Pokrece se Engine Modul i priprema za procesiranje strance |
|
|
|
// Transformacija stranice u print ready stranicu |
|
|
|
// Daje se instrukija pupeteru da pokrene print nad print ready stranicom |
|
|
|
await GeneratePdf(result, path); |
|
|
|
// Prepare PDF file to download |
|
|
|
// Generisani PDF fails se vraca kao rezultat obrade zahteva |
|
|
|
await generator.Generate(result, path); |
|
|
|
FileStream stream = new FileStream(path, FileMode.Open); |
|
|
|
|
|
|
|
return File(stream, "application/pdf", fileName); |
|
|
|
} |
|
|
|
|
|
|
|
private async Task GeneratePdf(string url, string path) |
|
|
|
{ |
|
|
|
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); |
|
|
|
var options = new LaunchOptions |
|
|
|
{ |
|
|
|
Headless = true |
|
|
|
}; |
|
|
|
using (var browser = await Puppeteer.LaunchAsync(options)) |
|
|
|
using (var page = await browser.NewPageAsync()) |
|
|
|
{ |
|
|
|
await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded); |
|
|
|
|
|
|
|
|
|
|
|
var allResultsSelector = ".chartjs-render-monitor"; |
|
|
|
await page.WaitForSelectorAsync(allResultsSelector); |
|
|
|
await EvaluateScript(page, "dist/main.js"); |
|
|
|
// Transform HTML page before printing in PDF |
|
|
|
// TODO: Find solution to import some JS library |
|
|
|
// await page.EvaluateFunctionAsync("() => {document.getElementsByTagName('p')[0].style.display = 'none'}"); |
|
|
|
//await page.EvaluateFunctionAsync("() => metoda()"); |
|
|
|
//await page.ClickAsync("button#prepare"); |
|
|
|
// Ne moze da radi jer Puppeteer ne pristupa skrivenim elementima |
|
|
|
//await page.ClickAsync("button#modify"); |
|
|
|
// Resenje evaluate JS |
|
|
|
//await EvaluateScript(page, "transform.js"); |
|
|
|
//string present = await CheckIfTransformExists(page); |
|
|
|
//// provera da li je transformacija vec odradjena |
|
|
|
//if (string.IsNullOrEmpty(present)) |
|
|
|
//{ |
|
|
|
// await EvaluateScript(page, "transform.js"); |
|
|
|
//} |
|
|
|
|
|
|
|
//await EvaluateScript(page, "modify.js"); |
|
|
|
|
|
|
|
//await page.EvaluateFunctionAsync("() => document.querySelector('#modify').click()"); |
|
|
|
await page.PdfAsync(path, new PdfOptions() { PrintBackground = true, Format = PuppeteerSharp.Media.PaperFormat.Letter }); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// private async Task<string> CheckIfTransformExists(Page page) |
|
|
|
// { |
|
|
|
// string path4 = Path.Combine(host.WebRootPath, "check.js"); |
|
|
|
// FileStream fs4 = new FileStream(path4, FileMode.Open); |
|
|
|
// StreamReader streamReader4 = new StreamReader(fs4); |
|
|
|
// string result4 = ""; |
|
|
|
// while (!streamReader4.EndOfStream) |
|
|
|
// { |
|
|
|
// result4 += (streamReader4.ReadLine() + Environment.NewLine); |
|
|
|
// } |
|
|
|
// fs4.Close(); |
|
|
|
// var present = await page.EvaluateFunctionAsync<string>(result4); |
|
|
|
// return present; |
|
|
|
// } |
|
|
|
|
|
|
|
private async Task EvaluateScript(Page page,string fileName) |
|
|
|
{ |
|
|
|
string path2 = Path.Combine(host.ContentRootPath, fileName); |
|
|
|
var result = await System.IO.File.ReadAllTextAsync(path2); |
|
|
|
await page.EvaluateExpressionAsync(result); |
|
|
|
} |
|
|
|
} |
|
|
|
} |