| 12345678910111213141516171819202122232425262728293031323334353637383940414243 |
- using System.Web;
- using AutoMapper;
- using BlackRock.Reporting.API.Core;
- using BlackRock.Reporting.API.Core.Models;
- using Microsoft.AspNetCore.Mvc;
- using PuppeteerSharp;
-
- namespace BlackRock.Reporting.API.Controllers
- {
- [Route("api/[controller]")]
- public class PDFGeneratorController : Controller
- {
- private readonly IHostEnvironment host;
- private readonly IGenerator generator;
- private readonly IMapper mapper;
-
- public PDFGeneratorController(IHostEnvironment host, IGenerator generator, IMapper mapper)
- {
- this.mapper = mapper;
- this.generator = generator;
- this.host = host;
- }
-
- [HttpGet("{url}")]
- public async Task<IActionResult> Get([FromQuery] OptionsForPdf pdfOptions,string url = "http://localhost:3000/#/dashboard")
- {
- 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}");
-
- var options = mapper.Map<OptionsForPdf,PdfOptions>(pdfOptions);
-
- await generator.Generate(result, path, options);
- FileStream stream = new FileStream(path, FileMode.Open);
- return File(stream, "application/pdf", fileName);
- }
- }
- }
|