| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- using System.Web;
- using Microsoft.AspNetCore.Mvc;
- using PuppeteerSharp;
-
- namespace BlackRock.Reporting.API.Controllers
- {
- [Route("api/[controller]")]
- public class PDFGeneratorController: Controller
- {
- private readonly IHostEnvironment host;
-
- public PDFGeneratorController(IHostEnvironment host)
- {
- 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")
- {
- // Service ima endpint na koji stize zahtev sa urlom strance koju treba eksportovati u PDF
- if (string.IsNullOrEmpty(url))
- return BadRequest();
-
- var result = HttpUtility.UrlDecode(url);
- // Temporary name of file which will be downloaded
- 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
- 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 });
-
- }
- }
-
- // 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);
- }
- }
- }
|