How to Open a PDF File in ASP

By Susan Hare

Files are written to browser windows in the binary format.
i code image by charles taylor from Fotolia.com

The Portable Document Format (PDF) is popular on the Web to display data and files to end users, because it is a standard format that is not browser-specific. Most browsers support in-line display of PDF files, and the creators of the format, Adobe Systems, offer free PDF Reader applications. To open a PDF file in ASP, you have to use a scripting language. The default language in ASP is VBScript, which is Microsoft's server side scripting language. The only time this language can be used on the client side is in Microsoft Internet Explorer.

Step 1

Create a new file in Notepad or your favorite programming interface. Name the file \"openPDF.asp\" and save it with your website.

Step 2

Add code to your ASP file to create the body and HTML structure. Copy the code below to accomplish this:

<% %>

Step 3

Create a variable for your file name and add it to the code section of your ASP file. Copy the example below:

Dim strPDF strPDF= \"C:\myDocument.pdf\"

Step 4

Create a file stream object and load the file into it. The VBScript example below shows how to do this:

Set oFileStream = Server.CreateObject(\"ADODB.Stream\") oFileStream.Open oFileStream.Type = 1 'Binary oFileStream.LoadFromFile strPDF

Step 5

Set the content type of the file to PDF and write the file to the browser. Use the following code to do these tasks:

Response.ContentType = \"application/pdf\" Response.AddHeader(\"Content-Disposition\", \"inline; filename=\" + strPDF) Response.BinaryWrite(oFileStream.Read)

Step 6

Close your file stream and release the memory:

oFileStream.Close Set oFileStream= Nothing

×