Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Chrome - PDF Download in an iFrame not working

So due to the recent Chrome update preventing a data URL from being opened directly using javaScript: "Not allowed to navigate top frame to data URL", I have some code that I need to modify. I have an ajax call that uses mPDF to generate a PDF, sends it back base 64 - and should open in a new tab. This always worked. However due to this issue, what I'm now trying to do is load that PDF into an iFrame in a new window to avoid the error. And that works, here's what I have:

$('.print-order-pdf').click(function(){

    var orderNumber = $(this).attr('order')
    var data = {
        pdfnonce: pdfAjax.pdfnonce,  
        action: 'getOrderPDF',
        orderNumber: orderNumber,
    }
    var win = window.open('', '_blank');
    $.post(pdfAjax.ajax_url, data, function(return_url){           
        win.document.write("<iframe id='pdf' src='"+ return_url +"'></iframe>");
        win.document.write("<style> html, body{ overflow:hidden; margin:0; } #pdf{ width:100%; height: 100%; }</style>");
    });

});

(This is a wordPress Ajax call). In any case - it all works and the new page with PDF opens. The problem is - in Chrome, the download link that appears as part of the PDF controls no longer works. I can print, rotate, whatever - but not download the file. I've tested in firefox, no issue triggering the download there. The Chrome console for the PDF window shows no errors at all. Any ideas? Is there something specific in Chrome preventing the download from inside the iFrame? Why? Is there a way around it?

Thanks so much in advance.

like image 972
JBoss Avatar asked Aug 29 '17 19:08

JBoss


People also ask

Why is my Google Chrome not downloading PDF?

Click on ''Site settings''. Click on the ”Advanced” button at the bottom. Click on the toggle switch of the heading ”Download PDF files instead of automatically opening them”.

Does iframe work with PDF?

Due to its wide compatibility, the iframe tag is widely used for embedding pdf. A browser that does not support PDF documents or the object tag can also use this tag to embed a pdf HTML code.

How do I enable auto download of PDF in Chrome instead of opening them in Chrome?

To Make Google Chrome Download PDF Files Instead of OpeningOn the right, go to the Content section, and click on Additional content settings. Click on PDF documents. On the next page, turn on (enable) the Download PDF files instead of automatically opening them in Chrome option. You are done.

Why are my PDFs not downloading?

Typically, this occurs for one of the following reasons: Your computer is not connected to the Internet, or there is a problem with your Internet settings. Your antivirus software needs to be updated. You may not be connected to the Adobe server.


1 Answers

My scenario is exactly that! I'm using chrome version 61.0.3163.100. I guess it's some browser flaw, but I did not find anything on google's forums. I am in doubt whether to download the file directly but would not want to give up the chrome PDF viewer.

Temporarily, I have found an ugly and inelegant solution until I have a definitive answer. I created a link in the generated window:

var newWindow = window.open("", "PDF", 'dependent=yes,locationbar=no,scrollbars=no,menubar=no,resizable,screenX=50,screenY=50,width=850,height=800');
newWindow.document.write(
'<html><body><center>' +
'<a title="Download File" style="font-family: \'Verdana\';color: #333;text-decoration: none;font-weight: 600;" download="File.PDF" href="data:application/pdf;base64,' + base64 + '">Download File</a>' +
'</center><br>' +
'<object width=100% height=100% type="application/pdf" data="data:application/pdf;base64,' + base64  + '">' +
'<embed type="application/pdf" src="data:application/pdf;base64,' + base64  + '" id="embed_pdf"></embed>' +
 '</object></body></html>');
 newWindow.window.focus();
like image 170
Geovani Anholete Avatar answered Sep 21 '22 16:09

Geovani Anholete