PDF request service

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • evilme

    PDF request service


    greetings and salutations, o smarter than i.
    i've been working on a solution to secure the delivery of pdfs to client
    browsers. we're introducing an public-website element, so i've decided to use
    a webservice on the internal application webserver to pass a pdf as a byte
    array to a public webserver outside the LAN, where it can be forwarded on the
    response to the client browser.

    i could use some advice how to improve this: i'm not entirely satisfied with
    the memory useage--i'm trying to shy away from taking on making a ISAPI
    extention if i can get this efficient enough as is.

    more vexingly, every so often, a local user will experience an error where
    the PDF is never saved/opened after they make their choice on the file
    open/save dialog. further, msie's progress meter remains and looks like it's
    very slowly filling (or timing out) even after the pdf has successfully been
    saved/opened--is there a final signal i'm failing to send to the browser..?

    I designed the web service as follows:

    Public Class DocumentRequest
    Inherits System.Web.Serv ices.WebService

    Private EXAMPLE_DIR As String =
    Server.mappath( "/ExportedPDFs/ExampleSheets/")

    Private Function BinaryGetPDFDoc ument(ByVal FileDIR As String, ByVal
    FileNum As String) As Byte()
    Dim FileStreamIn As New System.IO.FileS tream(FileDIR + FileNum +
    ".pdf", System.IO.FileM ode.Open, System.IO.FileA ccess.Read,
    System.IO.FileS hare.Read)
    Dim PDFData(FileStr eamIn.Length) As Byte
    Dim i As Integer = FileStreamIn.Re ad(PDFData, 0,
    CInt(FileStream In.Length))
    While (i < CInt(FileStream In.Length))
    System.Threadin g.Thread.Sleep( 200)
    End While
    FileStreamIn.Cl ose()
    Return PDFData
    End Function

    Private Function CheckExistsPDFD ocument(ByVal FileDIR As String, ByVal
    FileNum As String) As Boolean
    Try
    If (System.IO.File .Exists(FileDIR + FileNum + ".pdf")) Then
    Return True
    Else
    Return False
    End If
    Catch
    Return False
    End Try
    End Function

    <WebMethod()> _
    Public Function GetExampleSheet (ByVal FileNum As String) As Byte()
    Return BinaryGetPDFDoc ument(EXAMPLE_D IR, FileNum)
    End Function

    <WebMethod()> _
    Public Function CheckExistsExam pleSheet(ByVal FileNum As String) As
    Boolean
    Return CheckExistsPDFD ocument(EXAMPLE _DIR, FileNum)
    End Function

    End Class


    and i wrote a consume function as follows:

    Friend Sub PushPDFtoClient (ByRef Response As System.Web.Http Response,
    ByVal FileNum As String, ByVal TYPE As String)
    Dim ws As New PDFRequest.Docu mentRequest
    Dim PDFData() As Byte

    Try
    If (TYPE = "EXAMPLE") Then
    PDFData = ws.GetExampleSh eet(FileNum)
    End If
    Catch ex As Exception
    'EDITED - removed some private logging functions
    Response.Write( "<script language=""java script""
    type=""text/javascript"">al ert('There was an unexpected error accessing the
    PDF')</script>")
    Exit Sub
    End Try

    Dim ms As System.IO.Memor yStream = New System.IO.Memor yStream(PDFData )
    Response.Clear( )
    Response.Conten tType = "applicatio n/octet-stream"
    Response.AddHea der("Content-Disposition", "attachment ; filename=" &
    TYPE & "-" & FileNum & ".pdf; size=" & PDFData.Length. ToString)
    Response.AddHea der("Content-Length", PDFData.Length. ToString)
    Response.Flush( )

    Dim chunkSize As Integer = 1024
    Dim i As Integer
    For i = 0 To PDFData.Length Step chunkSize

    If (Not Response.IsClie ntConnected) Then
    Exit For
    End If

    Dim size As Integer = chunkSize
    If (i + chunkSize >= PDFData.Length) Then
    size = (PDFData.Length - i)
    End

    Dim chunk(size - 1) As Byte
    ms.Read(chunk, 0, size)
    Response.Binary Write(chunk)
    Response.Flush( )
    Next
    ms.Close()
    Response.End()
    End Sub
  • Random

    #2
    Re: PDF request service

    First, you need to change your line...

    Response.Conten tType = "applicatio n/octet-stream"

    ..to...

    Response.Conten tType = "applicatio n/pdf"

    Then, you have to actually write the byte array to the stream, like so...

    Response.Binary Write(PDFData)

    Then, you finish it off...

    Response.Flush( )
    Response.Close( )

    If you still have problems, try changing the .AddHeader statement to read
    'inline' instead of 'attachment'

    "evilme" <evilme@discuss ions.microsoft. com> wrote in message
    news:D7688966-53D3-4A22-A812-1D73C87F75D7@mi crosoft.com...[color=blue]
    >
    > greetings and salutations, o smarter than i.
    > i've been working on a solution to secure the delivery of pdfs to client
    > browsers. we're introducing an public-website element, so i've decided to
    > use
    > a webservice on the internal application webserver to pass a pdf as a byte
    > array to a public webserver outside the LAN, where it can be forwarded on
    > the
    > response to the client browser.
    >
    > i could use some advice how to improve this: i'm not entirely satisfied
    > with
    > the memory useage--i'm trying to shy away from taking on making a ISAPI
    > extention if i can get this efficient enough as is.
    >
    > more vexingly, every so often, a local user will experience an error where
    > the PDF is never saved/opened after they make their choice on the file
    > open/save dialog. further, msie's progress meter remains and looks like
    > it's
    > very slowly filling (or timing out) even after the pdf has successfully
    > been
    > saved/opened--is there a final signal i'm failing to send to the
    > browser..?
    >
    > I designed the web service as follows:
    >
    > Public Class DocumentRequest
    > Inherits System.Web.Serv ices.WebService
    >
    > Private EXAMPLE_DIR As String =
    > Server.mappath( "/ExportedPDFs/ExampleSheets/")
    >
    > Private Function BinaryGetPDFDoc ument(ByVal FileDIR As String, ByVal
    > FileNum As String) As Byte()
    > Dim FileStreamIn As New System.IO.FileS tream(FileDIR + FileNum +
    > ".pdf", System.IO.FileM ode.Open, System.IO.FileA ccess.Read,
    > System.IO.FileS hare.Read)
    > Dim PDFData(FileStr eamIn.Length) As Byte
    > Dim i As Integer = FileStreamIn.Re ad(PDFData, 0,
    > CInt(FileStream In.Length))
    > While (i < CInt(FileStream In.Length))
    > System.Threadin g.Thread.Sleep( 200)
    > End While
    > FileStreamIn.Cl ose()
    > Return PDFData
    > End Function
    >
    > Private Function CheckExistsPDFD ocument(ByVal FileDIR As String, ByVal
    > FileNum As String) As Boolean
    > Try
    > If (System.IO.File .Exists(FileDIR + FileNum + ".pdf")) Then
    > Return True
    > Else
    > Return False
    > End If
    > Catch
    > Return False
    > End Try
    > End Function
    >
    > <WebMethod()> _
    > Public Function GetExampleSheet (ByVal FileNum As String) As Byte()
    > Return BinaryGetPDFDoc ument(EXAMPLE_D IR, FileNum)
    > End Function
    >
    > <WebMethod()> _
    > Public Function CheckExistsExam pleSheet(ByVal FileNum As String) As
    > Boolean
    > Return CheckExistsPDFD ocument(EXAMPLE _DIR, FileNum)
    > End Function
    >
    > End Class
    >
    >
    > and i wrote a consume function as follows:
    >
    > Friend Sub PushPDFtoClient (ByRef Response As System.Web.Http Response,
    > ByVal FileNum As String, ByVal TYPE As String)
    > Dim ws As New PDFRequest.Docu mentRequest
    > Dim PDFData() As Byte
    >
    > Try
    > If (TYPE = "EXAMPLE") Then
    > PDFData = ws.GetExampleSh eet(FileNum)
    > End If
    > Catch ex As Exception
    > 'EDITED - removed some private logging functions
    > Response.Write( "<script language=""java script""
    > type=""text/javascript"">al ert('There was an unexpected error accessing
    > the
    > PDF')</script>")
    > Exit Sub
    > End Try
    >
    > Dim ms As System.IO.Memor yStream = New
    > System.IO.Memor yStream(PDFData )
    > Response.Clear( )
    > Response.Conten tType = "applicatio n/octet-stream"
    > Response.AddHea der("Content-Disposition", "attachment ; filename=" &
    > TYPE & "-" & FileNum & ".pdf; size=" & PDFData.Length. ToString)
    > Response.AddHea der("Content-Length", PDFData.Length. ToString)
    > Response.Flush( )
    >
    > Dim chunkSize As Integer = 1024
    > Dim i As Integer
    > For i = 0 To PDFData.Length Step chunkSize
    >
    > If (Not Response.IsClie ntConnected) Then
    > Exit For
    > End If
    >
    > Dim size As Integer = chunkSize
    > If (i + chunkSize >= PDFData.Length) Then
    > size = (PDFData.Length - i)
    > End
    >
    > Dim chunk(size - 1) As Byte
    > ms.Read(chunk, 0, size)
    > Response.Binary Write(chunk)
    > Response.Flush( )
    > Next
    > ms.Close()
    > Response.End()
    > End Sub[/color]


    Comment

    Working...