override Render to create pdf then want to continue loading page

Cindy Mello 62 Reputation points
2021-06-11T20:32:48.597+00:00

protected override void Render(HtmlTextWriter writer)
{

        var strWr = new StringWriter();  
        var htmlWr = new HtmlTextWriter(strWr);  
        base.Render(htmlWr);  
        var htmlToPdf = new HtmlToPdfConverter();  
        Response.ContentType = "application/pdf";  
        htmlToPdf.GeneratePdf(strWr.ToString(), null, @"c:\AlertAttachments\example2.pdf");  
       Response.ContentType = "text/html";    
    }  

if I do not add last line page tries to load a pdf, I want to only get pdf copy of form then continue loading page. Old application. Using AjaxToolKit

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,451 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,820 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Viorel 116.6K Reputation points
    2021-06-11T20:48:05.353+00:00

    What happens if you remove 'Response.ContentType = "application/pdf"' but keep 'Response.ContentType = "text/html"'?

    Or try something like this:

    protected override void Render(HtmlTextWriter writer)
    {
       var strWr = new StringWriter();
       var htmlWr = new HtmlTextWriter(strWr);
       base.Render(htmlWr);
       htmlWr.Flush();
       var htmlToPdf = new HtmlToPdfConverter();
       htmlToPdf.GeneratePdf(strWr.ToString(), null, @"c:\AlertAttachments\example2.pdf");
    
       base.Render(writer);
       Response.ContentType = "text/html";
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.