1

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.

2
  • I should've mentioned that I'm looking to embed the PDF in an <iframe> or <object> since there's going to be other content in the page. Is there a way to do this? Commented May 16, 2013 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 Commented May 20, 2013 at 13:28

2 Answers 2

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>
Sign up to request clarification or add additional context in comments.

2 Comments

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.
Yep, same thing. Our solutions, in essence, are the same. I'd stay away from ViewData though.
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" />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.