Show PDF in web application

I searched for it, searched for it, browsed through SO and other sites (I tried to read on this issue for hours now), but I still cannot find a satisfactory solution to what seems to be a simple, general programming problem.

Let me set the scene:

  • I have a website / web application that allows a user to search the SQL Server library.
  • Most documents related to search results are PDF files.
  • PDF files are stored inside the SQL Server BLOB database.
  • I want to be able to dynamically extract PDF from a database and display it to the user.
  • To save the user's search progress, I would like to open the file in another browser window / tab
  • I figured out how to save the PDF file on the server in the specified directory.
  • I DO NOT want the user to see the path to the file.
  • For a reason, I want a solution that works in all major browsers:
    • Internet explorer
    • Firefox
    • Chrome
    • Safari (including iPhone Safari Mobile Safari)
  • I would prefer not to buy a third-party component, but I am ready to go along this route, if necessary.
  • I don’t think I want to send the file as a downloadable file (which, I think, I figured out how to do it) because it won’t work on the iPhone / iPad?

Each solution I have tried so far contains some basic problems:

  • Using iFrames seems to fail on iPhone / iPad
  • Using Server.Transfer (I use ASP.NET) shows gibberish instead of PDF
  • I tried a couple of third-party demos, but they stink too!

I can't figure it out! I am truly a desktop developer and it was EASY on Windows! Why is it so complicated with the internet?

Am I really stupid, and is this really an easy exercise, or is this basic task really so difficult?

Please help point me in the right direction!

Thanks!!!

+4
source share
5 answers

This link may be useful to you,

http://nilangshah.wordpress.com/2007/05/28/successfully-stream-a-pdf-to-browser-through-https/

You can open the PDF in a new tab by specifying target = "_ blank" for the link. The ByteArray mentioned in the blog is your database BLOB. Hope this helps.

+5
source

Have you tried adding an http header using content-disposition-inline? You can also record the result directly in response to the output, and not save it. This ensures that the actual file path will not be displayed as you write it directly in response.

For instance,

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

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

+6
source

ASP.Net has a ReportViewer server element that can be used to display PDF files.

A significant part of the documentation about this feature is how to create a report and export it to PDF. MSDN is really not very useful. I suppose everyone relies on Adobe Reader and isn't looking for an alternative?

But you can also import and display PDF . This code seems to have worked for this user :

 public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload) { string path = (string.IsNullOrEmpty(reportViewer.LocalReport.ReportPath)) ? reportViewer.ServerReport.ReportPath : reportViewer.LocalReport.ReportPath; RenderToPdf(reportViewer, forceDownload, System.IO.Path.GetFileNameWithoutExtension(path)); } public static void RenderToPdf(ReportViewer reportViewer, Boolean forceDownload, string fileNameWithoutExtension) { HttpContext context = HttpContext.Current; if (!context.Response.Buffer) { return; //can not clear the buffer, so exit } //define out properties Warning[] warnings; string mimeType, encoding, fileNameExtension; string[] streams; //get pdf content Byte[] pdfContent = reportViewer.LocalReport.Render("PDF", null, out mimeType, out encoding, out fileNameExtension, out streams, out warnings); //cancel and clear the existing output! context.Response.Clear(); context.Response.ContentType = "application/pdf"; //add a header so that the user can save the target as a downloaded file if (forceDownload) { context.Response.AddHeader("Content-disposition", string.Format("attachment; filename={0}.pdf", fileNameWithoutExtension)); } context.Response.BinaryWrite(pdfContent); context.Response.End(); } 
+1
source

Here is how I do to open a PDF in a browser from an ASP.NET (aspx) page. On the OnLoad page:

  this.Response.Clear(); this.Response.Buffer = true; this.Response.ContentType = "application/pdf"; this.Response.AddHeader("content-length", pdfReportStream.Length.ToString()); this.Response.BinaryWrite(pdfReportStream.ToByteArray()); this.Response.End(); 
0
source

If you are looking for the pdf client's client client, you can look at PDF.js , which can display any PDF with the viewer turned on by default (you will have to convert viewer.html to a view if in MVC, but it’s pretty simple). It is open source and lively. I had to adapt it a bit to show pdf annotations on iphone (digital signatures), commenting out some lines in js.

0
source

Source: https://habr.com/ru/post/1400735/


All Articles