| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- using System.Web;
- using AutoMapper;
- using BlackRock.Reporting.API.Core.Interfaces;
- 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();
-
- // url must be encoded on client side and here we do decode
- 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}");
- // Mapping from DTO class to Puppeteer PdfOptions class. For margins and paper format
- 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);
- }
-
-
-
- [HttpGet("isolate/{url}")]
- public async Task<IActionResult> GetIsolated([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.Isolate(result, path, options);
-
- FileStream stream = new FileStream(path, FileMode.Open);
- return File(stream, "application/pdf", fileName);
- }
- }
- }
|