Display PDF in Web Application

asp.netpdfweb

I have googled this, searched this, looked through SO and other sites (I've been trying to read on this issue for hours now), but I still can't seem to find a satisfactory solution to what seems to be a simple, common programming issue.

Let me set the scene:

  • I've got a website/web application that allows the user to search a SQL Server library.
  • Most of the documents related to the search results are PDF files.
  • The PDF files are stored inside of the SQL Server database a BLOBs.
  • I want to be able to dynamically pull a PDF from the database and display it to the user.
  • In order to preserve the user's search progress, I think I'd like to open the file in another browser window/tab
  • I've figured out how to save the PDF to the server in a specified directory.
  • I DON'T want the user to see the path to the file.
  • Within reason, I want a solution that works in all major browsers:
    1. Internet Explorer
    2. FireFox
    3. Chrome
    4. Safari (including iPhone/iPad Mobile Safari)
  • I'd prefer not to buy a 3rd party component, but I'm willing to go that route if necessary.
  • I don't think I want to send the file as a download (which I think I've figured out how to do), because wouldn't this fail on iPhone/iPad?

Every solution I've tried so far has some basic problems wrong with it:

  • Using iFrames seems to fail miserably on iPhones/iPad
  • Using Server.Transfer (I'm using ASP.NET) shows gibberish instead of PDF
  • I've tried a couple of demo 3rd party solutions, but they stink, too!

I can't figure it out! I'm really a desktop developer, and this was EASY in Windows! Why is it so hard with the web?

Am I stupid and this is really an easy exercise, or is this basic task really that hard?

Please help point me in the right direction!

Thanks!!!

Best Answer

Have you tried adding a http header with content-disposition-inline? Also you may want to write the result directly to the output response instead of saving it. This would ensure the files actual path is not displayed as your writing it to the response directly.

Eg

Response.ContentType = "application/pdf";
Response.AddHeader("Content-disposition","inline");
Response.BinaryWrite(myfilestream.ToArray());

Where myfilestream is a memory stream or if you already have a byte array from your blob you can write it directly to the response without the toarray