Embed PDF Byte Data in HTML
<p>This should be working, but it seems I am missing something.</p>

<p>I have a pdf file on a local drive. Obviously Chrome and other browsers can't get that local URL since it's sandboxed, so I have a servlet pulling the data from the local drive and sending it back through an ajax call to the web client. I'm not getting any errors, and it seems a pdf viewer is loading, but the file itself won't display. I am encoding it ahead of time, but it still won't work. I will display my numerous approaches below, but I won't include the servlet code since that seems to be working.</p>

<p><strong>Attempt 1:</strong></p>

<pre><code>function embedFile(){
    $.get("FileGetter", function(pdfText){

        var base64EncodedPDF = b64EncodeUnicode(pdfText);
        $("#embedHolder").append("&lt;object id='embedder' width='80%' height='600px'&gt;&lt;embed width=100% height=100%"
                                     + ' type="application/pdf"'
                                     + ' src="data:application/pdf;base64,'
                                     + base64EncodedPDF
                                     + "&gt;&lt;/embed&gt;&lt;/object&gt;");

            });
        }

 function b64EncodeUnicode(str) {
     return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
                return String.fromCharCode('0x' + p1);
            }));
        }
</code></pre>

<p><strong>Attempt 2:</strong></p>

<pre><code>function embedFile(){
    $.get("FileGetter", function(pdfText){

        var base64EncodedPDF = b64EncodeUnicode(pdfText);
        var obj = $('&lt;object id="repObjID" type="application/pdf" width="100%" height="600" border="2"&gt;&lt;/object&gt;'); 
        obj.attr('data',base64EncodedPDF); 
        var embed = $('&lt;embed type="application/pdf"&gt;&lt;/embed&gt;'); 
        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);
            }));
}    
</code></pre>