locked
Stream PDF to browser RRS feed

  • Question

  • User1510859543 posted

    We are using the code below to create a PDF report in SSRS and saving it to a file.  Is it possible to modify the code to stream directly to the browser and not reuire the creation of a file?  Currently we are deleting the file after we print it. Thanks.

        Public Shared Function CreatePDFReport(ByVal strReportName As String, _
                                               ByVal intRecordID As Int32, _
                                               Optional ByVal intType As Int16 = 1) As String
            Dim strReturn As String = ""
            ' Output variables   
            Dim encoding As String
            Dim mimeType As String
            Dim extension As String
            Dim warnings As Warning() = Nothing
            Dim streamIDs As String() = Nothing
    
            ' ReportExecution object prepare   
            Dim rs As New ReportExecutionService()
            rs.Credentials = System.Net.CredentialCache.DefaultCredentials
            rs.Url = UtilClass.GetReportServerPath & "/ReportExecution2005.asmx"
    
            ' Render arguments   
            Dim result As Byte() = Nothing
            Dim strFileName As String = ""
            Dim reportPath As String = "/BodyShopReports/"
            Dim format As String = "PDF"
            Dim historyID As String = Nothing
            Dim devInfo As String = "<DeviceInfo><Toolbar>False</Toolbar></DeviceInfo>"
            Dim strOutputPath As String = HttpContext.Current.Server.MapPath("~/OfficeDocs/POFiles")
    
            ' Prepare report parameters   
            Dim parameters As ParameterValue()
            Select Case strReportName
                Case "WorkAuthorization"
                    parameters = New ParameterValue(1) {}
    
                    parameters(0) = New ParameterValue()
                    parameters(0).Name = "RecordID"
                    parameters(0).Value = intRecordID
                    parameters(1) = New ParameterValue()
                    parameters(1).Name = "ReportType"
                    parameters(1).Value = intType
    
                    strFileName &= "\WorkAuth" & intRecordID.ToString
    
                Case "CustomerBilling"
                    parameters = New ParameterValue(1) {}
    
                    parameters(0) = New ParameterValue()
                    parameters(0).Name = "RecordID"
                    parameters(0).Value = intRecordID
                    parameters(1) = New ParameterValue()
                    parameters(1).Name = "ReportType"
                    parameters(1).Value = intType
    
                    strFileName &= "\FinalBill" & intRecordID.ToString
    
                Case "TasksAtDropOff"
                    parameters = New ParameterValue(1) {}
    
                    parameters(0) = New ParameterValue()
                    parameters(0).Name = "RecordID"
                    parameters(0).Value = intRecordID
    
                    strFileName &= "\TasksAtDropOff" & intRecordID.ToString
    
                Case Else
    
            End Select
    
            Dim strFullPath As String = strOutputPath & strFileName & ".pdf"
    
            If File.Exists(strFullPath) Then
                '1-7-2015 added below to avoid exception error when same file name used
                Dim strTime As String = Microsoft.VisualBasic.Format(DateTime.Now, "hhmmss")
                strFileName &= strTime
                strFullPath = strOutputPath & strFileName & ".pdf"
            End If
    
            If File.Exists(strFullPath) = False Then
                ' Prepare render      
                Dim execInfo As New ExecutionInfo()
                Dim execHeader As New ExecutionHeader()
                rs.ExecutionHeaderValue = execHeader
                execInfo = rs.LoadReport(reportPath & strReportName, historyID)
                rs.SetExecutionParameters(parameters, "en-us")
                Dim SessionId As [String] = rs.ExecutionHeaderValue.ExecutionID
    
                ' Actual render   
                result = rs.Render(format, devInfo, extension, encoding, mimeType, warnings, streamIDs)
                execInfo = rs.GetExecutionInfo()
    
                ' Write to file   
                Dim stream As FileStream = File.Create(strOutputPath & strFileName & ".pdf", result.Length)
                stream.Write(result, 0, result.Length)
                stream.Close()
                stream.Dispose()
            End If
    
            strReturn = strFullPath
    
            Return strReturn
        End Function
    

    Monday, October 17, 2016 1:43 PM

Answers

  • User-6180675 posted

    I suppose you using as part of a webpage , then you use Context property to access HTTPContext. Follow the below code to write to browser response using the byte array which you retrieved . 

    Context.Response.Clear();
    Context.Response.Buffer = True
    Context.Response.Charset = ""
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName & ".pdf")
    
    Context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Context.Response.ContentType = "application/pdf"
    'the name of your byte array
    Context.Response.BinaryWrite(result) 
    Context.Response.Flush()
    Context.Response.End()

    or you can whether it's working without using the Context property otherwise you have create an instance of HTTPContext.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2016 8:48 PM
  • User-6180675 posted

    dlchase

    That worked.  Is there any way to suppress the prompt for open/save just before the PDF displays?

    Yes you can, just try with the below code in the Response.AddHeader section

    Context.Response.AddHeader("Content-Disposition", "inline; filename=" & strFileName & ".pdf")

    It's will force the browser to download the pdf file instead of showing the prompt for open/save dialog. Let me know if that works for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 18, 2016 5:41 PM

All replies

  • User-6180675 posted

    I suppose you using as part of a webpage , then you use Context property to access HTTPContext. Follow the below code to write to browser response using the byte array which you retrieved . 

    Context.Response.Clear();
    Context.Response.Buffer = True
    Context.Response.Charset = ""
    Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & strFileName & ".pdf")
    
    Context.Response.Cache.SetCacheability(HttpCacheability.NoCache)
    Context.Response.ContentType = "application/pdf"
    'the name of your byte array
    Context.Response.BinaryWrite(result) 
    Context.Response.Flush()
    Context.Response.End()

    or you can whether it's working without using the Context property otherwise you have create an instance of HTTPContext.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 17, 2016 8:48 PM
  • User1510859543 posted

    That worked.  Is there any way to suppress the prompt for open/save just before the PDF displays?

    Monday, October 17, 2016 9:49 PM
  • User-2057865890 posted

    Hi Dlchase,

    Is there any way to suppress the prompt for open/save just before the PDF displays?

    No, you can't. Note that this behavior varies from browser to browser though. Firefox has a dialog that gives you choices. Internet Explorer shows the yellow bottom bar asking whether you want to save the file. Chrome - depending on the options - will simply download the file to your Downloads folder without prompting for anything.

    Best Regards,

    Chris

    Tuesday, October 18, 2016 9:08 AM
  • User-6180675 posted

    dlchase

    That worked.  Is there any way to suppress the prompt for open/save just before the PDF displays?

    Yes you can, just try with the below code in the Response.AddHeader section

    Context.Response.AddHeader("Content-Disposition", "inline; filename=" & strFileName & ".pdf")

    It's will force the browser to download the pdf file instead of showing the prompt for open/save dialog. Let me know if that works for you.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 18, 2016 5:41 PM
  • User1510859543 posted

    Worked great, thanks.

    Tuesday, October 18, 2016 6:30 PM
  • User1510859543 posted

    See Nilishere answer.

    Tuesday, October 18, 2016 6:32 PM