The code below works perfectly in my local environment..however, the same code online does not work. I receive no error whatsoever, the iFrame just doesn't popup as expected.
The src
attribute is always changed in the iframe as expected...
if($('#iFramePdf').length > 0)
$('#iFramePdf').attr('src', json.pdf);
else
$('body').append('<iframe class="hidden" id="iFramePdf" src="' + json.pdf + '"></iframe>');
// enough time for the src being changed
setTimeout(function()
{
$('#iFramePdf').get(0).focus();
$('#iFramePdf').get(0).contentWindow.print();
}, 1000);
What can be wrong?
Edit 1: This seems that the problem might be due to content-type
headers?
the json.pdf has the absolute url like so:
http://mywebsite.com/public/files/somefile.pdf
And it doesn't open the chrome preview dialog. But if I remove http://mywebsite.com
and send only public/files/somefile.pdf
- like a relative path, the chrome preview dialog opens but it displays my 404 page.
The function ajax_get
that retrieves the PDF has the following response headers:
Cache-Control:no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Encoding:gzip
Content-Length:166
Content-Type:text/html; charset=UTF-8
Date:Tue, 26 Jan 2016 09:24:42 GMT
Expires:0
Pragma:no-cache
Server:nginx
Vary:User-Agent,Accept-Encoding
X-Powered-By:PHP/5.6.17
And retrieves the following json:
{"status":1,"msg":"Added successfully", "pdf":"http:\/\/mywebsite.com\/public\/files\/somefile.pdf"}
After the json is received and after I send the path of pdf into the iFrame in the network tab of chrome console, the pdf retrieves as response headers:
Accept-Ranges:bytes
Cache-Control:no-cache, no-store, must-revalidate
Connection:keep-alive
Content-Length:52611
Content-Type:application/pdf
Date:Tue, 26 Jan 2016 09:24:42 GMT
Expires:0
Last-Modified:Tue, 26 Jan 2016 09:24:42 GMT
Pragma:no-cache
Server:nginx
Vary:User-Agent
Edit 2: I just tried with no success to retrieve the PDF from a PHP page, like so:
function display_pdf($url)
{
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="testing.pdf"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
echo file_get_contents(base_url() . $url);
}
And in javascript before sending to iFramePdf
:
json.pdf = json.pdf.replace('http://mywebsite.com/', '');
json.pdf = base_url + 'backend/files/display_pdf/' + json.pdf;
The final url is something like: http://mywebsite/backend/files/display_pdf/public/files/somefile.pdf
If I enter that link in my browser it works..but in iframe it continues not to open the print preview from chrome.
Edit 3: I run a few more tests and the problem continues on iframe because it works in other ways. For example, the below code opens a new tab and automatically opens print preview dialog of chrome without any problem.
var win=window.open('about:blank');
win.document.write('<html><head></head><body>');
win.document.write('<iframe frameBorder="0" align="center" src="' + json.pdf + '" onload="test()" style="width: 100%; height: 100%"></iframe>');
win.document.write('<scr'+'ipt>function test(){window.focus();window.contentWindow.print()}</sc'+'ript></body></html>');
win.document.close();
if (window.focus) {win.focus()}
But I don't want to open a new tab. So I have tried to open a tab within the tab I'm in like so:
var printWindow = window.open(json.pathToPdf, "printWindow", "scrollbars=yes");
printWindow.focus();
printWindow.print();
And works, it opens a small tab within the tab I'm in, however it does not focus the chrome print preview dialog and if I'm working on fullscreen it shuts that off.
Edit 4: The problem is really because of PDF. If I change the code from:
$('body').append('<iframe class="hidden" id="iFramePdf" src="' + json.pdf + '"></iframe>');
to
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/public/images/someimage.png"></iframe>');
or
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/somepage.php"></iframe>');
It works without any problem.
If I set the PDF directly into the src
the print preview dialog doesn't open but the PDF is loaded into the iframe..
$('body').append('<iframe class="hidden" id="iFramePdf" src="http://mywebsite.com/public/files/somepdf.pdf"></iframe>');