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 Overflow
Discussions

html - Problems Embedding PDF Byte Stream In Page - Stack Overflow
I am generating in-memory PDF's on the server and want to send the result as a byte stream to be displayed as a PDF on a web page. I don't seem to have everything in order as none of the major bro... More on stackoverflow.com
🌐 stackoverflow.com
How to display PDF file in HTML? - Stack Overflow
I have an auto generated PDF file by itext and I need to display that PDF file in HTML. My question is: How to display a local PDF file in HTML using pdf.js? Should that PDF file be generated by s... More on stackoverflow.com
🌐 stackoverflow.com
c# - Show byte[] as pdf with html5 - Stack Overflow
Hi i have a big problem. I have a byte[] receveid from a Wcf service. The byte array represents a pdf file. In the Controller, i would like to do this: More on stackoverflow.com
🌐 stackoverflow.com
Displaying Binary PDF file on a webpage using Generic Handler
In my old Visual Studio, I was able to retrieve and display a pdf file saved in the database but in this latest visual studio the file does not display, any help in displaying it please? HTML More on learn.microsoft.com
🌐 learn.microsoft.com
2
0
January 25, 2024
🌐
Stack Overflow
stackoverflow.com › questions › 47472429 › how-to-display-the-byte-of-a-pdf-data-in-html
How to display the byte[] of a PDF data in HTML
November 24, 2017 - Explore Stack Internal ... Closed 8 years ago. ... <!DOCTYPE html> <html> <head> <base target="_top"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script> <script> function displayStream(bytestream){ var data = btoa(bytestream); console.log(data); var obj ='<object type="application/pdf" width="100%" height="100%" border="2" class="pdfObject" data="data:application/pdf;base64,'+data+'" ></object>'; $('#divContainer').append(obj); } </script> <title>Development </title> </head> <body> <p id='output'> Below is the PDF </p> <div id="divContainer"> </div> </body> </html>
🌐
DEV Community
dev.to › letsbelopez › how-to-load-a-pdf-in-a-browser-from-a-pdf-byte-array-56ic
How to Load a PDF in a Browser from a PDF Byte Array - DEV Community
March 23, 2020 - Here's my solution. I ended up using an embed tag and make the src attribute equal to the api endpoint that was returning the byte array. <div class="pdf"> <embed src="https://api_url/path/to/endpoint" type="application/pdf" /> </div>
Top answer
1 of 2
13

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
MiA5OS45MiA0OS45MiA5OS45MiBjCjIyLjMyIDk5LjkyIC0wLjA4IDc3LjUyIC0wLjA4IDQ5Ljky
IGMKLTAuMDggMjIuMzIgMjIuMzIgLTAuMDggNDkuOTIgLTAuMDggYwo3Ny41MiAtMC4wOCA5OS45
MiAyMi4zMiA5OS45MiA0OS45MiBjCkIKMjI0Ljk2IDQ5LjkyIG0KM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bQplbmRvYmoKNiAwIG9iago8PCAvVHlwZSAv
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.

2 of 2
-1

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.

🌐
iSkysoft
pdf.iskysoft.com › convert-pdf › display-pdf-in-html.html
How to Display PDF in HTML Page with Easy
PDFelement ProPDFelement Pro is a tool that allows for HTML display PDF from byte array. The versatile tool comes with advanced, easy to use features that allow you to display PDF in a HTML file. The tool can be used for direct conversion, or for creation of a PDF file before conversion.
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1512272 › displaying-binary-pdf-file-on-a-webpage-using-gene
Displaying Binary PDF file on a webpage using Generic Handler - Microsoft Q&A
January 25, 2024 - using System; using System.Web; namespace WebForms1 { public class Handler4 : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.Cache.SetCacheability(HttpCacheability.NoCache); context.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime()); context.Response.Cache.SetMaxAge(new TimeSpan(0, 0, 0, 0)); context.Response.ContentType = "application/pdf"; // byte array is obtained from the file in Files holder (not from DB) string filename = "JBL4312G.pdf"; string path = context.Server.MapPath("~/Files") + "\\" + filename; byte[] data = System.IO.File.ReadAllBytes(p
🌐
GitHub
github.com › mozilla › pdf.js › issues › 9142
Display pdf as byte-array · Issue #9142 · mozilla/pdf.js
November 16, 2017 - You must be signed in to change notification settings · Fork 10.5k · Star 52.3k · New issueCopy link · New issueCopy link · Closed · Closed · Display pdf as byte-array#9142 · Copy link · abrasat · opened · on Nov 16, 2017 · Issue body actions · Is it possible to display a pdf from a byte-array in memory, instead from a Uri ?
Author   abrasat
🌐
Telerik
telerik.com › forums › display-pdf-from-byte-array
Display PDF from byte array | Telerik Forums
January 20, 2011 - Hi Raymond, If you have the byte stream of an HTML page, you can display it in the RadHtmlPlaceholder using the HtmlSource property: ... htmlPlaceHolder.HtmlSource = htmlSource; Also, you can use the same approach to display HTML files saved in an isolated storage: htmlPlaceHolder.HtmlSource ...
🌐
GitHub
github.com › VadimDez › ng2-pdf-viewer › issues › 88
Display PDF from Byte Array · Issue #88 · VadimDez/ng2-pdf-viewer
May 10, 2017 - I'm hitting a service that generates a pdf based on data sent it and returns the pdf in a byte array. Is there a way to use this plugin to display the pdf?
Published   May 10, 2017
Author   jjacobs33589
🌐
Nutrient
nutrient.io › blog › sdk › open pdf in your web app
Five easy ways to embed and display a PDF in HTML (no JavaScript needed)
June 3, 2025 - Discover five quick methods to embed or display PDF files directly in your HTML pages using iframe, embed, and object tags. Includes pros, cons, and best practice tips for responsive, accessible PDFs.
🌐
Aspose
forum.aspose.com › aspose.pdf product family
Stream pdf and then convert that to html as bytes array .. (pdf to html) using stream - Free Support Forum - aspose.com
October 15, 2015 - (1) reading pdf in stream and then convert it to html (2) output byte[] resultHtmlAsBytes. See attached file for detail code
🌐
GitHub
github.com › mozilla › pdf.js › issues › 8308
How to display ("data:application/pdf;base64," + pdfData) in Viewer.html using PDF.js · Issue #8308 · mozilla/pdf.js
April 18, 2017 - iframe.src = "data:application/pdf;base64," + pdfbytesData + '#search=' + $('#hidden-searchKey').val().trim() + "&phrase=true"; What is the expected behavior? (add screenshot) I am getting empty Viewer.html. The PDF is not getting loaded with base64 string. What went wrong?
Author   AshwinShriwas