Skip to content Skip to sidebar Skip to footer

How To Display A Pdf Stream In A Browser Using Javascript

I am accessing an existing WCF web service (which returns a PDF as a byte stream) using jquery's ajax methods. When the call to the service completes, I end up with a javascript va

Solution 1:

Code you wrote does not display anything, simply open a blank window (location.href is an hash for browsing history, not the content of the page).

To display a PDF you have, at least, following options:

× Embed the PDF viewer inside an object tag. It may not be as straightforward as you may imagine, take a look to this post for sample code. In short it should be something like this:

<object data="your_url_to_pdf"type="application/pdf">
    <div>No PDF viewer available</div>
</object>

That's basic code but I suggest to follow what I say in the linked post if you need higher cross-browser compatibility (it also contains a few examples about how you might try to detect support for a PDF viewer).

× Download the file to local computer (simply add the full URL of your web service method that produces the file, do not forget to add the proper Content-Disposition in the header).

× Open the file into a new browser window. Create a normala tag as you point to a PDF file on-line that you want to display in a new window. Change the href to javascript:functionName and in that function produce the URI you'll use to call the web service method.

Whatever you'll do, do not forget to set the proper MIME type in your response moreover you method shouldn't return a byte stream (even if encoded) but a valid response for your web browser.

Solution 2:

If you are using <embed> or <object> tag to display a streamed PDF file (or other file types) from a server as in:

<objectdata="SomeServlet?do=get_doc&id=6" type="application/pdf" width="800" height="400">

make sure the server sends the proper http content-disposition value, which in this case would be inline.

Post a Comment for "How To Display A Pdf Stream In A Browser Using Javascript"