So I'm trying to add a new endpoint to my controller which can return the html response of another endpoint at pdf. The reason for this is deprecate a 3rd party service which involves all kinds of other problems.
I've found a library called IronPDF which seems to do the job, but I'm now struggling to get the html of the original endpoint. It looks something like this:
public class HomeController : Controller
{
public HomeController() : base() { }
public IActionResult Index()
{
return View();
}
public IActionResult IndexPdf()
{
ViewResult view = Index() as ViewResult;
HtmlToPdf renderer = new HtmlToPdf();
// How do I get the html content of view as a string here?
string html = ??
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);
using (var stream = pdf.Stream)
{
return File(stream, "application/pdf", "Index.pdf");
}
}
}
How do I get the html of the ViewResult?
This is just an example, I'm hoping to adapt it to handle dynamic pages in future. Is there a better way of doing this?