22

I am trying to view a PDF document in my MVC web page, but I cant make it to work.

I would like the PDF to be displayed as a part of the other stuff on the page (header, footer etc.). Currently I have a solution where the PDF is shown, but on the entire page.

Has anybody done this, if yes then how?

Yurii
  • 4,811
  • 7
  • 32
  • 41
Peter
  • 253
  • 1
  • 2
  • 6
  • what is the language / platform of your MVC? Ruby on Rails? Django? CakePHP? – OnesimusUnbound Jun 22 '11 at 12:17
  • Ok, a bit more info... The language is C# and I am currently playing around with a standard project i VS2010. I am generating a PDF runtime and storing in a stream. I would like to be able to view this PDF 'inside' a the page using a custom control or alike. I can embedded the PDf nicely using: Click here to view the file, but this is not using a stream. – Peter Jun 22 '11 at 13:02

1 Answers1

48

Why don't you try using iframe like this :

<iframe src="even file stream action url"></iframe>

I suggest to use object tag if it's possible, use iframe just for testing.

If you want to render PDF as part of the page as you just did

src='<% Html.RenderAction("GetPDF"); %>'

Then this is your option

If you need complete control over PDF content using CSS or whatsoever, like Google books and so on, then you need tools that help you to convert each requested page of PDF to Plain Text, HTML or even image. tools like PDFsharp. Search Google For Tools

If you want display PDF as part of the page then this is what you have to do

ASPX: src="<%= Url.Action("GetPDF") %>"
Razor: src="@Url.Action("GetPDF")"

And final answer could be

<object data="<%= Url.Action("GetPDF") %>" type="application/pdf" width="300" height="200">
    alt : <a href="data/test.pdf">test.pdf</a>
</object>

And in the case that you want to return PDF as Stream then you need

public FileStreamResult GetPDF()
{
    FileStream fs = new FileStream("c:\\PeterPDF2.pdf", FileMode.Open, FileAccess.Read);
    return File(fs, "application/pdf");
}
Beygi
  • 1,918
  • 1
  • 16
  • 22
  • I need the feature to work in IE! If I use IFrame I can load and show a PDF from a file, but I can't seem to make it work If I runtime create the PDF (using IText or PDFWriter). Could you provide an example ? – Peter Jun 23 '11 at 12:22
  • @Peter client browser do not care if the url is a file in disk or even in memory, you just have to tell client brower what kind of content you are streaming, so the problem is the Content-Type HTTP header. and can you tell me if you have any problem with creating an action method which will return new FileContentResult(bytes, "application/pdf"); – Beygi Jun 23 '11 at 13:27
  • @Peter if again this is not the case, i wanted you know that I'm doing my best to help you. so please if it's possible provide us your action method, and make us clear that the problem is displaying PDF from stream, or displaying it in part of the page. and yes i can provide sample if you give me your action method source – Beygi Jun 23 '11 at 13:39
  • @Beygi Thank you very much! I tried what you wrote, and it worked fine. Then I tried with my 'run-time' generated PDF (changing Filestream to memstream), and it failed with the statement file not starting with '%PDF ...'. The problem was that I forgot to set the position of the memstream to 0. After that all worked just fine ! – Peter Jun 27 '11 at 10:36
  • it's my pleasure to help you. – Beygi Jun 27 '11 at 10:51
  • @Beygi Sorry to open this again, but I have run into a problem with this solution, hope you can help. When I publish this on a remote server (still on my intranet) it woun't work. It just shows a large PDF icon. The site is published on an IIS version 6.. – Peter Jun 27 '11 at 12:34
  • As you might already know, iis 6 will not handle actions, without some effort(the '.aspx'). try loading the action url that has been rendered in browser for PDF in new window, and see what it says, maybe this time it's about the permission thing. – Beygi Jun 27 '11 at 12:49
  • If it's yellow screen that says, the page cannot be found, or something like that, take a look at following link:http://go.microsoft.com/?LinkId=9394801 – Beygi Jun 27 '11 at 12:50
  • @peter tell me what's the result.when ever possible – Beygi Jun 27 '11 at 14:29
  • @Beygi sorry I just had some other things to do. I have tried it on my Windows 7 machine and it dosen't work when I put it on the IIS, which means it only works inside VS2010. I have another function where I get an image Image this works fine, but if I change it to it doesen't work. My conclusion is therefore that it is the object tag that makes the problem. Any sugestions ? – Peter Jun 28 '11 at 06:27
  • Did you try to load the url of object tag that has been rendered into browser(using view source) in new window?and what was the result? – Beygi Jun 28 '11 at 10:34
  • @Peter actually i'd tried what you needed(display pdf) under iis-7(integrated mode) in ie(7,8 and 9) and the result is ok, using the same object tag that i provided in answer. – Beygi Jun 28 '11 at 10:44
  • @Beygi You are right it works fine when loading from a file (what you wrote), but I had trouble generating the PDF. I found an error, some files missing when published, but now it works Great !! Thanks again ! – Peter Jun 28 '11 at 12:49
  • Life savior! I've never thought about this. Great! – Leandro Soares Feb 11 '16 at 18:09
  • Dude thanks so much. Was gonna try PDF.js next but this did the trick. – Vishav Premlall Jul 02 '18 at 11:50