0

We have a servlet that will deliver PDF reports to a browser. We also have a IIS server running .net apps and we want to return the PDF from the servlet as a stream to the .Net app and then the .Net app would render the PDF to the browser (we are using this technique for reason I don't need to go into here). I am not much of a VB/ Visual Studio devloper by this code works by using a web request:

    Dim BUFFER_SIZE As Integer = 1024
    ' Create a request for the URL. 
    Dim serveraction As String = "https://OurSeverName/ServletContext/Dispatch?action=ajaxRunReport&reportName="
    Dim request As WebRequest = _
      WebRequest.Create(serveraction + ReportName.Text)

    ' Get the response.
    Dim res As WebResponse = request.GetResponse()

    ' Get the stream containing content returned by the server.
    Dim dataStream As Stream = res.GetResponseStream()

    ' Open the stream using a BinaryReader for easy access.
    Dim reader As New BinaryReader(dataStream)

    ' Read the content.
    Response.ContentType = "application/pdf"

    Response.AddHeader("content-disposition", "inline; filename=reportfile.pdf")
    Dim bytes = New Byte(BUFFER_SIZE - 1) {}

    While reader.Read(bytes, 0, BUFFER_SIZE) > 0
        Response.BinaryWrite(bytes)
    End While
    reader.Close()
    ' Clean up the streams and the response.
    Response.Flush()
    Response.Close() 

The only issue is, even though the code runs quickly, it takes 20-30 seconds to render the PDF in Chrome and IE but only a few seconds in FireFox. Any idea why there is a delay in rendering the PDF? Is there a better way to stream a PDF from one server to another?

Pete Helgren
  • 438
  • 1
  • 7
  • 21
  • Let me just add (after that downvote) that I don't post UNLESS I thoroughly reserach before I post. This is the result of two days of trial and error to get something working. If you want the wasteful clutter of all my research and every iteration of code then you have more time on your hands to read than I do....BTW if you DO downvote, would you be courteous enough to ask for more information so I can respond... – Pete Helgren Aug 14 '13 at 15:27

1 Answers1

1

There were just a couple of very subtle tweaks that were needed (and they seem pretty insignificant and non-intuitive to me).

I added the following before setting the content type:

Response.Clear()

Response.ClearHeaders()

And I added the following after reader.Close()

Response.End()

That was it. Now the PDF files stream nicely from the Java servlet to the IIS server and to the end user's browser.

Pete Helgren
  • 438
  • 1
  • 7
  • 21