| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- using BlackRock.Reporting.API.Core;
- 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)
- {
- 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");
-
- await page.PdfAsync(path, new PdfOptions()
- {
- PrintBackground = true,
- Format = PuppeteerSharp.Media.PaperFormat.Letter,
- Scale = 1,
- DisplayHeaderFooter = true
- });
-
- }
- }
- 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);
- }
- }
- }
|