Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PdfGenerator.cs 2.5KB

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