Returning a pdf file

G

Guest

Hello;

When I get a request for a page, how can I return a pdf as the page contents?

In java the way to do this is for the response set it's type to
"application/pdf" and then get a byte (not character) output stream from the
response and write the bit image of the pdf file to it.
 
N

Neo Geshel

David said:
Hello;

When I get a request for a page, how can I return a pdf as the page contents?

In java the way to do this is for the response set it's type to
"application/pdf" and then get a byte (not character) output stream from the
response and write the bit image of the pdf file to it.

I am assuming that the PDF is inside the database, and that you need to
extract it and return it as a part of the browser window, rather than a
separate download.

When you want a separate download, You need to store a lot more
information, such as the desired file name (for the downloaded file to
have, rather than the name of the ASPX file that called it), the mime
type, the length and a few other things.

When you want the PDF to appear in the browser window (to replace the
page that called it), you need to write directly to the ASPX page. As an
example, I have a "pdf.aspx" page that I use to embed any PDF file I
call directly into the browser window. If there is a mistake (or if I
have an inquisitive hacker poking around the site), an "unknown pdf"
image is called by the script.

<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script runat=server>
Public Sub Page_Load(sender As Object, e As EventArgs)
Dim intID as Integer = Request.QueryString("id")
Dim strTable as String = Request.QueryString("tbl")
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
myConn.Open()
Dim myCmd as New OleDbCommand("SELECT Count(PDFData) FROM [tbl" &
strTable & "] WHERE [ID] = " & intID, myConn)
If myCmd.ExecuteScalar() <> 0 Then
LoadPDF(intID, strTable)
Else
LoadPlaceholder()
End If
MyConn.Close()
End Sub
Sub LoadPDF(intID as Integer, strTable as String)
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myComd as New OleDbCommand("SELECT * FROM tbl" & strTable & "
WHERE [ID] = " & intID, myConn)
myConn.Open()
Dim myDR as OleDbDataReader =
myComd.ExecuteReader(CommandBehavior.CloseConnection)
myDR.Read()
Response.ClearContent()
Response.ClearHeaders()
Response.ContentType = "application/pdf"
Response.BinaryWrite(myDR.Item("PDFData"))
Response.Flush()
Response.Close()
End Sub
Sub LoadPlaceHolder()
Dim myConn as New
OleDbConnection(ConfigurationSettings.AppSettings("strConn"))
Dim myImage As System.Drawing.Image =
System.Drawing.Image.FromFile(ConfigurationSettings.AppSettings("imgDir")
& "no-pdf.png")
Dim tempStream As New MemoryStream()
myImage.Save(tempStream, ImageFormat.Png)
Response.ClearContent()
Response.ContentType = "image/png"
Response.BinaryWrite(tempStream.ToArray())
Response.End()
End Sub
</script>


As you can see, it first checks to see if the PDF entry even exists in
the database. If it does not, the “no-pdf.png†image is called from the
file system (with the Image Directory specified in the Web.Config file).
If the entry DOES exist, the PDF is called, and written directly to the
ASPX page.

The mime types for both files are hard-coded, because you will control
the “no-pdf†image (it can be almost any kind of image you want, really,
but you can use mine if you want:
http://beta.getmetz.com/images/no-pdf.png ), and the database
(hopefully) will hold only PDF files in that table. If you want to store
ANY kind of file type in that database table, it gets a bit more
complicated, as you need to store the mime type and possibly the file
length as well (I’m not sure on that one).

It should work in all browsers.

Cheers.
...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
“Anyone who believes in Intelligent Design (“creationismâ€) is just as
ignorant and ill-educated as someone who believes that the world is
flat, that the Sun circles the Earth or that there really is a tooth
fairy. Darwinism has an overwhelming foundation of evidence that can be
tested and reproduced. Intelligent Design, on the other hand, has no
evidence at all; not one single shred of testable proof. As such,
Intelligent Design is Religious Mythology, and has no right whatsoever
to be in our Science classrooms.†- 99.99+% of Scientists
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 
N

Neo Geshel

David said:
Hi;

Yes - this is exactly what I need.
Glad to be of help.

Notice, tho - the connection string is able to take an ID and a table
name, so that you can have more than one table holding PDF’s. If you
have just one table that holds PDF’s, consider hard-coding the table
name in the SQL query. If you have a *limited number* of *unchanging*
tables, consider calling the tables by number rather than by name (that
is, use tbl=3 in the connection string, and filter it in the code (using
CASE or IF...Then) to make the proper number-to-table association for
the SQL string). Calling by a number (or some other not easily
identifiable method) reduces the chances of attracting a hacker’s
interest, and makes your app more robust.

My app isn’t yet finished, so the tables are still called by name. Once
I have the database finalized, I’m moving to calling the tables by
number or some such method.

I hope this helps.
...Geshel
--
***********************************************************************
* My reply-to is an automatically monitored spam honeypot. Do not use *
* it unless you want to be blacklisted by SpamCop. Please reply to my *
* first name at my last name dot org. *
***********************************************************************
“Anyone who believes in Intelligent Design (“creationismâ€) is just as
ignorant and ill-educated as someone who believes that the world is
flat, that the Sun circles the Earth or that there really is a tooth
fairy. Darwinism has an overwhelming foundation of evidence that can be
tested and reproduced. Intelligent Design, on the other hand, has no
evidence at all; not one single shred of testable proof. As such,
Intelligent Design is Religious Mythology, and has no right whatsoever
to be in our Science classrooms.†- 99.99+% of Scientists
***********************************************************************
Mignon McLaughlin once said that “A nymphomaniac is a woman [who is] as
obsessed with sex as the average man.†Unfortunately, since true
nymphomaniacs are so rare, this means that it takes an extraordinary
woman to keep up with an ordinary man.
***********************************************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top