Insert data byte PDF to HTML

This should work, but it seems that something I was missing.

I have a pdf file on the local disk. It is clear that Chrome and other browsers can not get the local address of the URL, because it is isolated, so I have a servlet that retrieves data from a local disk and sends it back through the ajax call a web client. I do not get any errors, and it seems that a PDF viewer is loaded, but the file is not displayed. I encode it in advance, but it still won't work. My numerous approaches are listed below, but I will not include the servlet code as it works.

Run 1:

function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); $("#embedHolder").append("<object id='embedder' width='80%' height='600px'><embed width=100% height=100%" + ' type="application/pdf"' + ' src="data:application/pdf;base64,' + base64EncodedPDF + "></embed></object>"); }); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } 

Run 2:

 function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } id = "repObjID" type = "application / pdf" width = " function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } ; function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } ; function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } type = "application / pdf"> </ embed>'); function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } ; function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } (embed).; function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } (obj).; function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } (/% ([0-9A-F] { function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } , p1) { function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } ); function embedFile(){ $.get("FileGetter", function(pdfText){ var base64EncodedPDF = b64EncodeUnicode(pdfText); var obj = $('<object id="repObjID" type="application/pdf" width="100%" height="600" border="2"></object>'); obj.attr('data',base64EncodedPDF); var embed = $('<embed type="application/pdf"></embed>'); embed.attr('src',base64EncodedPDF); $('#repObjID').append(embed); $("#docHolder").html(obj); } function b64EncodeUnicode(str) { return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) { return String.fromCharCode('0x' + p1); })); } 
+5
source share
1 answer

1. Download the binary data using ajax in arraybuffer

 var xhr = new XMLHttpRequest; xhr.open('get', '/path/to/pdf, true); xhr.responseType = 'arraybuffer'; xhr.send(); path / to / pdf, true); var xhr = new XMLHttpRequest; xhr.open('get', '/path/to/pdf, true); xhr.responseType = 'arraybuffer'; xhr.send(); 

2. Create a URL-address and Blob Object

 var blob = new Blob([xhr.response], {type: 'application/pdf'}); var url = URL.createObjectURL(blob); ], {type: 'application / pdf'}); var blob = new Blob([xhr.response], {type: 'application/pdf'}); var url = URL.createObjectURL(blob); 

3. Download URL-address in the iframe

 iframe.src = url; 

Demo: https://webblocks.nl/tests/ajax-pdf.html

0
source

All Articles