locked
Issue exporting report to pdf RRS feed

  • Question

  • User789306677 posted

    OK, I have a report where in code, I click on a button, I kick off the report and have it export automatically to pdf. I has been working fine but I had to add a second optional parameter and now the report works fine for some data but other times it runs and then when I try to download the pdf I get the message that Adobe Acrobat reader can't open the pdf since it is either not a supported file type or because the file has been damaged.  If I run the report from within SSRS it will work and randomly it will work from with the application.   I have tried changing the properties of the optional parameter inside the report and I have tried making the file name a random string to see if there was some sort of file.

    Below is my code for kicking off the report. Please offer any suggestions.

    ReportViewer1.ServerReport.ReportServerCredentials = New MyReportServerCredentials()
    ReportViewer1.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote
    ReportViewer1.ServerReport.ReportServerUrl = New Uri(ReportServerURL)
    ReportViewer1.ServerReport.ReportPath = ReportServerPath

    Dim paramList As New Generic.List(Of ReportParameter)
    Dim pInfo As ReportParameterInfoCollection
    pInfo = ReportViewer1.ServerReport.GetParameters()

    paramList.Add(New ReportParameter("paraPM_ID", Session("PM_ID").ToString(), True))

    ReportViewer1.ServerReport.SetParameters(paramList)
    ReportViewer1.ServerReport.Refresh()

    Dim warnings As Warning() = Nothing
    Dim streamids As String() = Nothing
    Dim mimeType As String = Nothing
    Dim encoding As String = Nothing
    Dim extension As String = Nothing
    Dim deviceInfo As String
    Dim bytes As Byte()

    Dim fileName As String = "Post_Mortem_Single_" + Session("PM_ID") + ".pdf"

    Dim basePath As String = HttpContext.Current.Server.MapPath("~/App_Data")
    deviceInfo = "<DeviceInfo><SimplePageHeaders>True</SimplePageHeaders></DeviceInfo>"
    bytes = ReportViewer1.ServerReport.Render("PDF", Nothing, mimeType, encoding, extension, streamids, warnings)

    Dim fs As New FileStream(basePath & "/" + fileName, FileMode.Create)
    fs.Write(bytes, 0, bytes.Length)
    fs.Close()

    Response.Clear()
    Response.AddHeader("content-disposition", "attachment;filename=" + fileName)
    Response.Charset = ""
    Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Response.ContentType = "application/vnd.text"
    Response.TransmitFile(basePath & "/" + fileName)
    Response.Flush()
    Response.Close()

    fs.Dispose()

    Monday, May 8, 2017 1:35 PM

Answers

  • User-2057865890 posted

    Hi Rusty1968,

    Try adding Response.End(). When you fail to call Response.End(), the web form appends it's HTML markup to the PDF output stream and corrupts the document.

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 9, 2017 7:28 AM

All replies

  • User-2057865890 posted

    Hi Rusty1968,

    Try adding Response.End(). When you fail to call Response.End(), the web form appends it's HTML markup to the PDF output stream and corrupts the document.

    Best Regards,

    Chris

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 9, 2017 7:28 AM
  • User789306677 posted

    It strange thing. I started yesterday by creating a new page which I linked from the original. I had that going inline instead of attachment. When I added the line to that code. it worked. but when I added to the original code which is a copy it doesn't. I will keep looking into fixing the original page but if worse comes to worse I have something that will work.

    Tuesday, May 9, 2017 12:20 PM