0

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?

Andy
  • 3,228
  • 8
  • 40
  • 65

1 Answers1

0

You could try as below in your controller:

public async Task<IActionResult> IndexPdf()
    {
        using StringWriter sw = new StringWriter();
        IView view = _viewEngine.FindView(ControllerContext, "Index",true).View??default!;
        ViewContext viewContext = new ViewContext(ControllerContext, view, ViewData, TempData, sw, new HtmlHelperOptions());
        await view.RenderAsync(viewContext);
        var content = sw.GetStringBuilder().ToString();

        ChromePdfRenderer renderer = new ChromePdfRenderer();

        PdfDocument pdf = renderer.RenderHtmlAsPdf(content);
        return File(pdf.Stream, "application/pdf", "Index.pdf");
        

    }

inject ICompositeViewEngine into your controller:

private readonly ICompositeViewEngine _viewEngine;

        public HomeController(ILogger<HomeController> logger, ICompositeViewEngine viewEngine)
        {
            .......
            _viewEngine = viewEngine;
        }

Result:

enter image description here

Notice:

1,HtmlToPdf is obsolote,you could try with ChromePdfRenderer

2,You have to input full address for your css files,or the css won't work

enter image description here

the content string is OK,I checked with return Ok(content):

enter image description here

3,You could just try with

renderer.RenderUrlAsPdfAsync("targeturl");

directly,without getting the html of the ViewResult.

Ruikai Feng
  • 6,823
  • 1
  • 2
  • 11