using BlackRock.Reporting.API.Core; using BlackRock.Reporting.API.Core.Models; using PuppeteerSharp; namespace BlackRock.Reporting.API.Persistence { public class PdfGenerator : IPdfGenerator { private readonly IHostEnvironment host; public PdfGenerator(IHostEnvironment host) { this.host = host; } public async Task Generate(string url, string path, PdfOptions options) { await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); var opts = new LaunchOptions { Headless = true }; using (var browser = await Puppeteer.LaunchAsync(opts)) 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"); await page.PdfAsync(path,options // new PdfOptions{ // PrintBackground = true, // Format = PuppeteerSharp.Media.PaperFormat.Letter // } ); } } 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); } } }