1. Download the binary data with ajax, into an arraybuffer
var xhr = new XMLHttpRequest;
xhr.open('get', '/path/to/pdf', true);
xhr.responseType = 'arraybuffer';
xhr.send();
2. Create Blob and Object URL
var blob = new Blob([xhr.response], {type: 'application/pdf'});
var url = URL.createObjectURL(blob);
3. Load URL in iframe
iframe.src = url;
Demo: https://webblocks.nl/tests/ajax-pdf.html
Answer from Rudie on Stack Overflowhtml - Problems Embedding PDF Byte Stream In Page - Stack Overflow
How to display PDF file in HTML? - Stack Overflow
c# - Show byte[] as pdf with html5 - Stack Overflow
Displaying Binary PDF file on a webpage using Generic Handler
You're missing one step. The embedded PDF needs to be encoded.
The following is from a similar smallish PDF. I encoded it into base 64 by uploading the PDF to site http://www.motobit.com/util/base64-decoder-encoder.asp.
I then pasted the base64 result into my embedded element as follows:
<embed width="100%" height="100%" src="data:application/pdf;base64,JVBERi0xLjMKJaWx6woxIDAgb2JqCjw8IC9UeXBlIC9DYXRhbG9nIC9QYWdlcyAyIDAgUiA+Pgpl
bmRvYmoKMiAwIG9iago8PCAvVHlwZSAvUGFnZXMgL0NvdW50IDEgL0tpZHMgWyAzIDAgUiBdIC9S
ZXNvdXJjZXMgPDwgL1BhdHRlcm4gPDwgL1B0MSA1IDAgUiA+PiAvUHJvY3NldCBbIC9QREYgL1Rl
eHQgXSA+PiA+PgplbmRvYmoKMyAwIG9iago8PCAvVHlwZSAvUGFnZSAvQ29udGVudHMgNCAwIFIg
L01lZGlhQm94IFsgMCAwIDIzMCAyMTAgXSAvUGFyZW50IDIgMCBSID4+CmVuZG9iago0IDAgb2Jq
Cjw8IC9MZW5ndGggNjE5ID4+CnN0cmVhbQoKcQowIEcKMSAxIDAgcmcKMjUgMTc1IDE3NSAtMTUw
IHJlCmYKL1BhdHRlcm4gY3MKL1B0MSBzY24KOTkuOTIgNDkuOTIgbQo5OS45MiA3Ny41MiA3Ny41
MiA5OS45MiA0OS45MiA5OS45MiBjCjIyLjMyIDk5LjkyIC0wLjA4IDc3LjUyIC0wL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TAuMDA3IFRECjAgMSAwIHJnCihcMjUyKSBU
agotMC43MzIzIDAuNzgxMyBURAowIDAgMSByZwooXDI1MSkgVGoKMC42OTEzIDAuMDA3IFRECjAg
MCAwIHJnCihcMjUwKSBUagpFVApRCmVuZHN0cmVhbQplbmRvYmoKNiAwIG9iago8PCAvVHlwZSAv
Rm9udCAvU3VidHlwZSAvVHlwZTEgL0Jhc2VGb250IC9aYXBmRGluZ2JhdHMgPj4KZW5kb2JqCnhy
ZWYKMCA3CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDAxNCAwMDAwMCBuIAowMDAwMDAwMDYz
IDAwMDAwIG4gCjAwMDAwMDAxODkgMDAwMDAgbiAKMDAwMDAwMDI3OCAwMDAwMCBuIAowMDAwMDAw
OTQ4IDAwMDAwIG4gCjAwMDAwMDEzNTAgMDAwMDAgbiAKdHJhaWxlcgo8PCAvUm9vdCAxIDAgUiAv
U2l6ZSA3ID4+CnN0YXJ0eHJlZgoxNDIzCiUlRU9G"/>
If you save the above as html then open it with a browser you should see the following one-page PDF.

I'd suggest trying a similar process to encoded your PDF as base 64 and embed it as above.
I realize the question was about embedding a PDF using a base64 encoding, but the question is looking for a way to send an in-memory PDF to the browser using a byte stream. I struggled for a while with this (in C#) and came up with the following code solution:
[HttpGet("download/{id}/raw")]
public ActionResult GetDownloadAttachmentRaw(long id)
{
var rules = BusinessRules.Rules.ExtractFrom(HttpContext);
var attachment = rules.GetAttachment(id) ?? throw new Exception("Attachment not found");
var fileBytes = System.IO.File.ReadAllBytes(GetFileName(attachment));
var mimeType = attachment.MimeType ?? "application/octet-stream";
return new FileContentResult(fileBytes, mimeType);
}
When the user requests a valid PDF attachment, the file is read from the file system (into memory) and then returned to the user a FileContentResult. This many seem obvious, but I was first using a FileStreamResult (code not shown) and the HTML was downloading the file and not displaying it.
Again, I know this is not a direct answer to the question but I think it may be helpful to others searching for a C# solution to streaming in-memory PDFs.
Implementation of a PDF file in your HTML web-page is very easy.
<embed src="file_name.pdf" width="800px" height="2100px" />
Make sure to change the width and height for your needs.
1. Browser-native HTML inline embedding:
<embed
src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
type="application/pdf"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></embed>
<iframe
src="http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&navpanes=0&scrollbar=0"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></iframe>
Pro:
- No PDF file size limitations (even hundreds of MB)
- It’s the fastest solution
Cons:
- It doesn’t work on mobile browsers
2. Google Docs Viewer:
<iframe
src="https://drive.google.com/viewerng/viewer?embedded=true&url=http://infolab.stanford.edu/pub/papers/google.pdf#toolbar=0&scrollbar=0"
frameBorder="0"
scrolling="auto"
height="100%"
width="100%"
></iframe>
Pro:
- Works on desktop and mobile browser
Cons:
- 25MB file limit
- Requires additional time to download viewer
3. Other solutions to embed PDF:
- https://mozilla.github.io/pdf.js
- https://pdfobject.com
- https://viewerjs.org
IMPORTANT NOTE:
Please check the X-Frame-Options HTTP response header. It should be SAMEORIGIN.
X-Frame-Options SAMEORIGIN;
I would recommend creating another action on the controller designed specifically for rendering the PDF data. Then it's just a matter of referencing it within your data attribute:
data="@Url.Action("GetPdf", "PDF")"
I'm not sure you can dump raw PDF data within the DOM without hitting some kind of encoding issue. You may be able to do like images, though I've never tried. But, for demo's sake and image would look like:
src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"
So, assuming the same could be done it would probably look like the following:
@{
String base64EncodedPdf = System.Convert.ToBase64String(Model.pdfInBytes);
}
@* ... *@
<object data="data:application/pdf;base64,@base64EncodedPdf"
width="900" height="600" type="application/pdf"></object>
I still stand by my original response to create a new action however.
protected ActionResult InlineFile(byte[] content, string contentType, string fileDownloadName)
{
Response.AppendHeader("Content-Disposition", string.Concat("inline; filename=\"", fileDownloadName, "\""));
return File(content, contentType);
}
Add this code to your base controller or the controller itself and call this function to show file in the browser itself.
To prevent 'InvalidCharacterError' error, you need to do this:
var base64EncodedStr = btoa(unescape(encodeURIComponent(rawData)));
Use a FileReader to encode your image as a data URL:
jQuery.ajax({...})
.done(function (r) {
var reader = new FileReader(
reader.onload = (function(self) {
return function(e) {
document.getElementById("img").src = e.target.result;
}
})(this);
reader.readAsDataURL(new Blob([r]));
});
This is quick, easy, to the point and doesn't require any third-party script:
<embed src="http://example.com/the.pdf" width="500" height="375"
type="application/pdf">
UPDATE (2/3/2021)
Adobe now offers its own PDF Embed API.
(That requires registering at Adobe and get clientID to use within js)
https://www.adobe.io/apis/documentcloud/dcsdk/pdf-embed.html
UPDATE (1/2018):
The Chrome browser on Android no longer supports PDF embeds. You can get around this by using the Google Drive PDF viewer
<embed src="https://drive.google.com/viewerng/
viewer?embedded=true&url=http://example.com/the.pdf" width="500" height="375">
Probably the best approach is to use the PDF.JS library. It's a pure HTML5/JavaScript renderer for PDF documents without any third-party plugins.
Online demo: https://mozilla.github.io/pdf.js/web/viewer.html
GitHub: https://github.com/mozilla/pdf.js
Found the solution here it is, i was sending byte array from spring controller which is in the form like %PDF-1 %����. So i send base64 encoded string from spring controller and send the base64 encoded string to browser and it works.
javascript code :
var arrrayBuffer = base64ToArrayBuffer(data); //data is the base64 encoded string
function base64ToArrayBuffer(base64) {
var binaryString = window.atob(base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++) {
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
var blob = new Blob([arrrayBuffer], {type: "application/pdf"});
var link = window.URL.createObjectURL(blob);
window.open(link,'', 'height=650,width=840');
convert byte array to base64 encoded string in spring controller
String encodedString = Base64.getEncoder().encodeToString(bytearrayofpdf);
You can use PDFObject JavaScript utility to view PDF files in your browser. Just create a div with some id and insert the byte array that you get into that div.
PDFObject.embed(<byte array>, "#pdfObjectViewer");
You need to download the PDFObject library and include it in your project from their site for this to work. Or you can use this CDN.
I use Google Docs embeddable PDF viewer. The docs don't have to be uploaded to Google Docs, but they do have to be available online.
<iframe src="http://docs.google.com/gview?url=http://path.com/to/your/pdf.pdf&embedded=true"
style="width:600px; height:500px;" frameborder="0"></iframe>
instead of using iframe and depending on the third party`think about using flexpaper, or pdf.js.
I used PDF.js, it works fine for me. Here is the demo.
You would need to pass the responseType in your service call
$http.post('/Service-URL', dataTO, {responseType: 'arraybuffer'});
then in the success of your data call this should open up pdf in a new window:-
getDocument()
.success(function(data) {
var file = new Blob([data], { type: 'application/pdf' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
})
From this answer :- https://stackoverflow.com/a/21730535/3645957 by https://stackoverflow.com/users/2688545/michael
If anyone still looks for that, here is what I'm doing (and working) :
var pdfAsDataUri = "data:application/pdf;base64,"+byteArray;
window.open(pdfAsDataUri);
Where byteArray is the data you receive. It's maybe not a nice solution (byte array is visible in the URL), but it works...