1

I have a pdf that i returned by stream in a json result by my GetTestReport action:

return this.Json(pdfInBytes);

And in my view:

$.post('@Url.Action("GetTestReport", "Reports")', function(data) {
    // What have i put here?
});

How can i show the pdf in a entire page, or in a specific div?

Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64

2 Answers2

3

The PDF can't be embedded in a document, it's a document itself, and you can't put it in a JSON result, you'll put its URL in JSON.

Then you can still place it in an iFrame (it will be the source src). Or you do window.open('you_pdf_url.pdf'); to show it in a new tab.

Nabab
  • 2,608
  • 1
  • 19
  • 32
  • But with this, i have to create it physically the file right? And i cannot do this, so i have to create a stream or convert in bytes to pass to the browser like we do with txt files, for exemple, but with FileResult not with JsonResult. So, what you are saying is that we cannot do this with JsonResult, only with FileResult? – Vinicius Ottoni Feb 08 '12 at 19:07
  • No, you can still pass the URL which will dynamically create it. – Nabab Feb 08 '12 at 19:10
  • My pdf does not exists physically, i have it in bytes, so, the url will point to where? – Vinicius Ottoni Feb 08 '12 at 19:15
  • What do you mean in bytes? I understand it's not a PDF file which exists somewhere, that it's generated by a piece of software. Well, the script generating your PDF shouldn't send its whole content to JSON. Its content, its bytes as you say (if I understand well), should be served through a URL, and this URL should be sent to JSON, then directly loaded in a window or an iFrame. If you want to show it in a browser environment it's the only way. – Nabab Feb 08 '12 at 19:21
  • When i say in bytes, i mean that i have the pdf exported to a stream (MemoryStream for exemple, or Stream), in other words a byte array. – Vinicius Ottoni Feb 08 '12 at 19:24
  • I never tried, but maybe you can give this string as the src of the iframe... It works for images. It must have to be base64 encoded I guess. Mmmm... Just Googled a bit and found out I might have been wrong. Look at this: http://stackoverflow.com/questions/291813/best-way-to-embed-pdf-in-html – Nabab Feb 08 '12 at 19:32
  • "The only solution I can recall is to serve not PDF but Flash that will display your PDF document. For example have a look at www.scribd.com. But the problem is that your documents have to be hosted on third-party server and you can't convert them on fly. If these limitations don't affect you, then iPaper is way to go." I have tried, but this works only with images, not with pdf (convert to base64 and etc..) – Vinicius Ottoni Feb 08 '12 at 19:36
  • Which language generates the PDF? – Nabab Feb 08 '12 at 21:09
  • Well, can't you have an ASP page which returns direcly the PDF stream (and only that)? In this case you put the ASP script's URL as the src of your document (iFrame or window) – Nabab Feb 09 '12 at 16:26
0

Nabab is correct, you can't convert a PDF to JSON.

Your action 'GetTestReport' should return the pdf itself by using a FileStreamResult:

return new FileStreamResult(pdfMemoryStream, "application/pdf"); 

You don't specify the name of your controller, so let's assume it's called 'home'. That means if you browse to /Home/GetTestReport, your pdf is served to to client and shown in the browser.

That url is what you need! You can use it in your ViewModel or send it to the browser using JSON.

NetWave
  • 379
  • 3
  • 12