0

My issue is I'm currently receiving from a web service response a string that is the binary data of a PDF file. I need to display this PDF file embedded in an MVC view. I'm using C#.

Any pointers are appreciated.

tereško
  • 58,060
  • 25
  • 98
  • 150
Gary2112
  • 1
  • 2
  • 1
  • I should've mentioned that I'm looking to embed the PDF in an – Gary2112 May 16 '13 at 19:07
  • the concept is pretty much the same, you just need to create a page that contains an iframe and make that iframe point to the view that renders the pdf. See my updated answer – Ulises May 20 '13 at 13:28

2 Answers2

1

You can just return it using File. Something like this:

public ActionResult ShowPDF()
{
  byte[] pdf = myService.GetPDF();
  return File(pdf, "application/pdf");      
}

UPDATE

Create a page containing a iframe element and set the src attribute to point to your view that renders the PDF file. Here is an example taken from here

<iframe src="ShowPDF" width="100%" style="height:20em">

 [Your browser does <em>not</em> support <code>iframe</code>,
 or has been configured not to display inline frames.
 You can access <a href="ShowPDF">the document</a>
 via a link though.]

</iframe>
Ulises
  • 13,229
  • 5
  • 34
  • 50
  • Thanks for your help. I've found another way to do this is to set a ViewData entry to: ViewData["PDF_src"] = "data:application/pdf;base64," + Convert.ToBase64String(PDF_src); and to have the iframe src attribute set to this value. – Gary2112 May 20 '13 at 15:11
  • Yep, same thing. Our solutions, in essence, are the same. I'd stay away from ViewData though. – Ulises May 21 '13 at 19:32
0

HTML5 has added the embed tag which allows a variety of rich content to be embedded into the page as follows:<embed src="your file path here" type="application/pdf" />

user2378769
  • 309
  • 2
  • 6