You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PdfGenerator.cs 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using BlackRock.Reporting.API.Core;
  2. using PuppeteerSharp;
  3. namespace BlackRock.Reporting.API.Persistence
  4. {
  5. public class PdfGenerator : IPdfGenerator
  6. {
  7. private readonly IHostEnvironment host;
  8. public PdfGenerator(IHostEnvironment host)
  9. {
  10. this.host = host;
  11. }
  12. public async Task Generate(string url, string path)
  13. {
  14. await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision);
  15. var options = new LaunchOptions
  16. {
  17. Headless = true
  18. };
  19. using (var browser = await Puppeteer.LaunchAsync(options))
  20. using (var page = await browser.NewPageAsync())
  21. {
  22. await page.GoToAsync(url, WaitUntilNavigation.DOMContentLoaded);
  23. var allResultsSelector = ".chartjs-render-monitor";
  24. await page.WaitForSelectorAsync(allResultsSelector);
  25. await EvaluateScript(page, "dist/main.js");
  26. await page.PdfAsync(path, new PdfOptions()
  27. {
  28. PrintBackground = true,
  29. Format = PuppeteerSharp.Media.PaperFormat.Letter,
  30. Scale = 1,
  31. DisplayHeaderFooter = true
  32. });
  33. }
  34. }
  35. private async Task EvaluateScript(Page page, string fileName)
  36. {
  37. string path2 = Path.Combine(host.ContentRootPath, fileName);
  38. var result = await System.IO.File.ReadAllTextAsync(path2);
  39. await page.EvaluateExpressionAsync(result);
  40. }
  41. }
  42. }