Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trouble loading PDF on Chrome IOS

Tags:

We're getting the following message from Chrome when downloading (or attempting to download) a pdf in our mobile web application.

"Warning: Something's not right here!... The site you are trying to access is acting strangely, and Chrome is unable to verify that the URL is correct."

This is working fine in Safari and essentially we are doing this.

  1. On load do a call to verify that the document that we want to show is OK.
    • if the document is not ok message the user and then close the tab
  2. Direct the tab to navigate to an address which downloads the PDF.

Without posting too much code the Javascript is something like this:

DoRequest ("print_report",
           "VALIDATE",
           mycallback);

function mycallback (data,error) {

    var h_href = "";
    var h_widget = "";

    if(error == true) {
        window.close(); 
        return;
    }    

    h_href = GenerateHREF( "print_report", "PRINT" );

    window.location.href = h_href;        
}

The URL provided by GenerateHREF is for the same originating site and is relative to the original.
the mime type is set to application/pdf. The content-disposition is set to inline. I've tried setting the content-size header as well but it doesn't seem to have any effect.

Content-Disposition: attachment; filename="pp66.26.pdf"
Content-Length: 31706
Content-Type: application/pdf

I'm missing something ... just what?

like image 354
DuStorm Avatar asked Oct 30 '12 21:10

DuStorm


People also ask

Why is my PDF not opening in Chrome?

For Google Chrome Version 60 and above First, check if 'Download PDF files instead of automatically opening them in Chrome' is turned on in Chrome. When this is enabled, all PDF will be downloaded instead of view.

Why PDF file is not opening in IOS?

If you're trying to open a PDF on an iPad or iPhone and it appears blank, you need to set Adobe Reader as your default for opening PDF files on your device.

How do I open PDF in Chrome on iPhone?

Downloaded PDFs can be opened directly via the Files app itself. Just open the Files app, browse to the location where you saved the file, and then tap it. The Files app should instantly use iOS's native PDF-viewing capabilities — similar to iBooks — to immediately display the contents of the file.

Why are PDF links not opening in Chrome mobile?

Chrome for Android doesn't support plug-ins, so it doesn't have Chrome PDF Viewer, and because of this, it can't natively read PDF files (you'll need a separate app for PDFs). This is why the Android version doesn't have this ability, but the desktop version does. Hope this clears up any confusion for anyone.


1 Answers

Try to parse document to base64 and added to your document or iframe.

    function getAsyncBase64(fileName, callBack){
        var xhr = new XMLHttpRequest();
        xhr.open('GET', fileName, true);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function (e) {
        if (this.status == 200) {
            var uInt8Array = new Uint8Array(this.response || this.responseText);
            var i = uInt8Array.length;
            var binaryString = new Array(i);
            while (i--) {
                binaryString[i] = String.fromCharCode(uInt8Array[i]);
            }
            var dataBinary = binaryString.join('');
            var data64 = window.btoa(dataBinary);
            callback(data64);                
        }
        xhr.send();
    };
    function callback(base64){
        window.open(base64, "_blank");
        //or
        iframe.src = "data:application/pdf;base64,"+ base64;
    };

    getAsyncBase64(url,callback);
like image 130
sendler Avatar answered Jan 03 '23 02:01

sendler