| FileStream stream = new FileStream(path, FileMode.Open); | FileStream stream = new FileStream(path, FileMode.Open); | ||||
| return File(stream, "application/pdf", fileName); | 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); | |||||
| } | |||||
| } | } | ||||
| } | } |
| public interface IGenerator | public interface IGenerator | ||||
| { | { | ||||
| Task Generate(string url, string path,PdfOptions options); | Task Generate(string url, string path,PdfOptions options); | ||||
| Task Isolate(string url, string path,PdfOptions options); | |||||
| } | } | ||||
| } | } |
| } | } | ||||
| } | } | ||||
| public async Task Isolate(string url, string path, PdfOptions options) | |||||
| { | |||||
| await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision); | |||||
| var opts = new LaunchOptions | |||||
| { | |||||
| Headless = true | |||||
| }; | |||||
| using (var browser = await Puppeteer.LaunchAsync(opts)) | |||||
| 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/main2.js"); | |||||
| await page.PdfAsync(path,options | |||||
| // new PdfOptions{ | |||||
| // PrintBackground = true, | |||||
| // Format = PuppeteerSharp.Media.PaperFormat.Letter | |||||
| // } | |||||
| ); | |||||
| } | |||||
| } | |||||
| private async Task EvaluateScript(Page page, string fileName) | private async Task EvaluateScript(Page page, string fileName) | ||||
| { | { | ||||
| string path2 = Path.Combine(host.ContentRootPath, fileName); | string path2 = Path.Combine(host.ContentRootPath, fileName); |
| { | { | ||||
| public PdfOptions Convert(OptionsForPdf source, PdfOptions destination, ResolutionContext context) | public PdfOptions Convert(OptionsForPdf source, PdfOptions destination, ResolutionContext context) | ||||
| { | { | ||||
| if (source == null) | |||||
| { | |||||
| return null; | |||||
| } | |||||
| destination = new PdfOptions() | |||||
| { | |||||
| DisplayHeaderFooter = source.DisplayHeaderFooter, | |||||
| HeaderTemplate = source.HeaderTemplate, | |||||
| Height = source.Height, | |||||
| Landscape = source.Landscape, | |||||
| OmitBackground = source.OmitBackground, | |||||
| PageRanges = source.PageRanges, | |||||
| PreferCSSPageSize = source.PreferCSSPageSize, | |||||
| PrintBackground = source.PrintBackground, | |||||
| Scale = source.Scale, | |||||
| Width = source.Width | |||||
| }; | |||||
| if(source == null) return destination; | |||||
| if(destination == null) | |||||
| destination = new PdfOptions(); | |||||
| destination.DisplayHeaderFooter = source.DisplayHeaderFooter; | |||||
| destination.HeaderTemplate = source.HeaderTemplate; | |||||
| destination.Height = source.Height; | |||||
| destination.Landscape = source.Landscape; | |||||
| destination.OmitBackground = source.OmitBackground; | |||||
| destination.PageRanges = source.PageRanges; | |||||
| destination.PreferCSSPageSize = source.PreferCSSPageSize; | |||||
| destination.PrintBackground = source.PrintBackground; | |||||
| destination.Scale = source.Scale; | |||||
| destination.Width = source.Width; | |||||
| destination.MarginOptions = new PuppeteerSharp.Media.MarginOptions() | destination.MarginOptions = new PuppeteerSharp.Media.MarginOptions() | ||||
| { | { | ||||
| Bottom = source.MarginBottom, | Bottom = source.MarginBottom, |
| const isolated = document.querySelector('[isolate="true"]'); | |||||
| const root = document.getElementById('root'); | |||||
| root.childNodes.forEach(element => { | |||||
| element.remove() | |||||
| }); | |||||
| root.appendChild(isolated); |
| { | { | ||||
| "format": 1, | "format": 1, | ||||
| "restore": { | "restore": { | ||||
| "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\BlackRock.Reporting.API.csproj": {} | |||||
| "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\BlackRock.Reporting.API.csproj": {} | |||||
| }, | }, | ||||
| "projects": { | "projects": { | ||||
| "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\BlackRock.Reporting.API.csproj": { | |||||
| "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\BlackRock.Reporting.API.csproj": { | |||||
| "version": "1.0.0", | "version": "1.0.0", | ||||
| "restore": { | "restore": { | ||||
| "projectUniqueName": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\BlackRock.Reporting.API.csproj", | |||||
| "projectUniqueName": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\BlackRock.Reporting.API.csproj", | |||||
| "projectName": "BlackRock.Reporting.API", | "projectName": "BlackRock.Reporting.API", | ||||
| "projectPath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\BlackRock.Reporting.API.csproj", | |||||
| "projectPath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\BlackRock.Reporting.API.csproj", | |||||
| "packagesPath": "C:\\Users\\safet.purkovic\\.nuget\\packages\\", | "packagesPath": "C:\\Users\\safet.purkovic\\.nuget\\packages\\", | ||||
| "outputPath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\obj\\", | |||||
| "outputPath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\obj\\", | |||||
| "projectStyle": "PackageReference", | "projectStyle": "PackageReference", | ||||
| "configFilePaths": [ | "configFilePaths": [ | ||||
| "C:\\Users\\safet.purkovic\\AppData\\Roaming\\NuGet\\NuGet.Config", | "C:\\Users\\safet.purkovic\\AppData\\Roaming\\NuGet\\NuGet.Config", |
| { | { | ||||
| "version": 2, | "version": 2, | ||||
| "dgSpecHash": "CWZgWYM1eCSk38SuYkv3dxqETT/4ZhBGYJ0BC0Js2XjwANoY+dTh7urgBxbU/o5glmzspiqosf7ZSf+k+lOuyw==", | |||||
| "dgSpecHash": "u5LCzMsSc98tqejgvIS7vh2sBsOKVmvwB91LkGNhpRK/OTJT4TBibJo14VN9KPzUL1djIsrbKAcg0IlFWE3yWg==", | |||||
| "success": true, | "success": true, | ||||
| "projectFilePath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\BlackRock.Reporting.API\\BlackRock.Reporting.API.csproj", | |||||
| "projectFilePath": "C:\\Users\\safet.purkovic\\Desktop\\PDFEngineAPI\\blackrock.reporting.api\\BlackRock.Reporting.API.csproj", | |||||
| "expectedPackageFiles": [ | "expectedPackageFiles": [ | ||||
| "C:\\Users\\safet.purkovic\\.nuget\\packages\\automapper\\10.1.1\\automapper.10.1.1.nupkg.sha512", | "C:\\Users\\safet.purkovic\\.nuget\\packages\\automapper\\10.1.1\\automapper.10.1.1.nupkg.sha512", | ||||
| "C:\\Users\\safet.purkovic\\.nuget\\packages\\automapper.extensions.microsoft.dependencyinjection\\8.1.1\\automapper.extensions.microsoft.dependencyinjection.8.1.1.nupkg.sha512", | "C:\\Users\\safet.purkovic\\.nuget\\packages\\automapper.extensions.microsoft.dependencyinjection\\8.1.1\\automapper.extensions.microsoft.dependencyinjection.8.1.1.nupkg.sha512", |