Skip to content

Instantly share code, notes, and snippets.

@NikolayIT
Last active May 30, 2025 13:13
Show Gist options
  • Save NikolayIT/151b39bd1fe53804fa83a844b3abc5fb to your computer and use it in GitHub Desktop.
Save NikolayIT/151b39bd1fe53804fa83a844b3abc5fb to your computer and use it in GitHub Desktop.
private readonly IViewRenderService viewRenderService;
private readonly IHtmlToPdfConverter htmlToPdfConverter;
private readonly IHostingEnvironment environment;
public DashboardController(
IViewRenderService viewRenderService,
IHtmlToPdfConverter htmlToPdfConverter,
IHostingEnvironment environment)
{
this.viewRenderService = viewRenderService;
this.htmlToPdfConverter = htmlToPdfConverter;
this.environment = environment;
}
[HttpGet]
public async Task<IActionResult> GetPdf(SomeInputModel input)
{
var model = this.GetViewModel(input);
var htmlData = await this.viewRenderService.RenderToStringAsync("~/Views/Dashboard/GetPdf.cshtml", model);
var fileContents = this.htmlToPdfConverter.Convert(this.environment.ContentRootPath, htmlData);
return this.File(fileContents, "application/pdf");
}
@vikasuhag
Copy link

Could not find an IRouter associated with the ActionContext. If your application is using endpoint routing then you can get a IUrlHelperFactory with dependency injection and use it to create a UrlHelper, or use Microsoft.AspNetCore.Routing.LinkGenerator.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment