How to export report to PDF programatically, then do a redirect

5 posts, 1 answers
  1. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 23 Jul 2010 Link to this post

    I have a function for exporting reports directly to pdf that works great but I want to be able to redirect the user once the report is generated and can't seem to make that happen. I either can redirect the user or print the report and the user stays on the page. Here is the code I use for the export, taken from a blog on this site. Works great.

    Public Function ExportToPDF(ByVal reportToExport As Telerik.Reporting.Report) As Telerik.Reporting.Report
           Dim reportProcessor As New Telerik.Reporting.Processing.ReportProcessor()
           Dim result As RenderingResult = reportProcessor.RenderReport("PDF", reportToExport, Nothing)
           Dim fileName As String = result.DocumentName + ".pdf"
           HttpContext.Current.Response.Clear()
           HttpContext.Current.Response.ContentType = result.MimeType
           HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Private)
           HttpContext.Current.Response.Expires = -1
           HttpContext.Current.Response.Buffer = True
           'Uncommenting the following line will prompt user to Open Save or Cancel. Otherwise report opens in new window
           HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0};FileName=""{1}""", "attachment", fileName))
           HttpContext.Current.Response.BinaryWrite(result.DocumentBytes)
           HttpContext.Current.Response.End()
       End Function
    I was told to remove the last line that ends the request and add this:
    HttpContext.Current.Response.Redirect("../WorkOrders/OpenWorkOrdersSummary.aspx", True)
    But that didn't work, it only redirected me to the new page but no report got generated. Here is the code I use to generate the report:
    If ChkPrint.Checked = True Then
               Dim tF As New TelerikFunctions
               Dim rv = New FacilitiesReportsLibrary.WorkOrder
               rv.setWorkOrderID = Session("WorkOrderID")
               tF.ExportToPDF(rv)
           Else
               Response.Redirect("../WorkOrders/OpenWorkOrdersSummary.aspx")
           End If
    Any help would be appreciated.
  2. Answer
    Steve
    Admin
    Steve avatar
    10941 posts

    Posted 27 Jul 2010 Link to this post

    Hello AkAlan,

    We're not aware of out of the box solution for your requirement. As you have found out, not ending the response and redirecting would not execute the export operation and this is expected as both operations are running in the same context. Probably some "hacks" with javascript redirect with applied timeout would be the solution for your case.

    Greetings,
    Steve
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft banner
  4. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 27 Jul 2010 Link to this post

    Thanks, I appreciate you validating what I suspected.
  5. neebs
    neebs avatar
    106 posts
    Member since:
    Apr 2007

    Posted 31 Jul 2010 Link to this post

    The reason this does not work is that when you invoke Response.End() or Response.Close(), your web page terminates, and any action taken in the code-behind is not executed. I ran into this with a project where I needed to log downloads. However the logging never occurred, even if I updated the database with the logging info prior to any Response.Write's or Response.End. To accomplish what you want to do, you should create a separate form that performs the response.write's and load that form in an invisible iFrame. I'll attach some code to illustrate. Basically I need to update a button in a grid with a client-side on click event handler that launches the iframe and loads the page:

    ib = (ImageButton) e.Item.FindControl("ImageButtonDownload");
    ib.Attributes["href"] = "#";
    ib.Attributes["onclick"] =
        string.Format("return InitializeDownload3('{0}');",
                          info.ItemId);

    Then in the aspx file:

    function InitializeDownload3(jobid) {
        // Create an IFRAME.
        var iframe = document.createElement("iframe");
     
        // Point the IFRAME to GenerateFile, with the
        //   desired region as a querystring argument.
        iframe.src = "<%= CFGWebRoot %>Download.aspx?f=-1&j=" + jobid;
     
        // This makes the IFRAME invisible to the user.
        iframe.style.display = "none";
     
        // Add the IFRAME to the page.  This will trigger
        //   a request to GenerateFile now.
        document.body.appendChild(iframe);
    }


    you will need to write a form called Download.aspx that takes the appropriate parameters as querystrings, and it then performs the Response.Write's and Response.Close().

    Hope this helps

    Steve
  6. AkAlan
    AkAlan avatar
    121 posts
    Member since:
    Jun 2009

    Posted 31 Jul 2010 Link to this post

    Hi Neebs, Thanks for the post. I am working on a similar solution using javascript to open a new window and add the report generating code to the page and passing the parameter to it. It does work but I haven't tested yet to see if the timing works for what I need. What I am trying to accomplish is this. A user is adding information to a job, either taking time against it or closing it out. I have it set up that when they click an update button the data is saved and they are redirected to page they came from. The users want to have a printout of the work they just entered without having to select the job again and then print the report. They are at remote sites in Alaska and the latency of page loads is killing them. I wanted to provide a way for them to update the job in the database then provide a printout on the same button click. I'm worried the print job will run before the data they just entered is updated. I would think that it will work but as I said, haven't had time to test it. I'm thinking that using a timer of some sort on the report form might work. I'll post back once I get a solid soultion tested. Thanks again, glad to see my thinking is valid.
Back to Top
DevCraft banner