Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome 77 Not Auto-Printing PDFs

Chrome 77 has stopped respecting the print() embedded JS in PDFs to initiate/open the print dialog after a PDF has been loaded.

For example, open the below file in Firefox, Chrome 76, or in Acrobat and you'll see the print dialog appear. In Chrome 77 it is no longer appearing. Specifically, in my case and on three other computers I tested this on, version 77.0.3865.75.

https://cdn.dealrcloud.com/assets/test/Invoice-1003.pdf

Is this a new setting we can adjust/modify or is this a permanent breaking change that will prevent us from auto-triggering a print dialog for Chrome clients?

like image 523
forrestmid Avatar asked Sep 11 '19 18:09

forrestmid


3 Answers

This was deliberately removed.

Allow print() only in response to a user gesture

https://pdfium.googlesource.com/pdfium.git/+/2021804f1b414c97667c03d7ab19daf66f6a19ef

The issue was that the embedded JavaScript in PDF files did not respect the Content-Security-Policy of the embedding page. https://crbug.com/968914

like image 21
Josh Lee Avatar answered Sep 21 '22 16:09

Josh Lee


Ok, guys. I came across the same problem having auto print pdf feature not working on several laptops. This feature is very critical on several of our projects, so I thought this workaround for Chrome 77 might be very helpful for community as well:

var loadPDFAndPrint = function (id, url) {
    $("#"+id).remove();
    $("<iframe id='"+id+"' name='"+id+"'>")
        .hide()
        .attr("src", url)
        .appendTo("body");
    $("#"+id).on("load", function(){
        function getChromeVersion () {
            var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./); 
            return raw ? parseInt(raw[2], 10) : false;
        }
        if (getChromeVersion() >= 77) {
            window.frames[id].focus();
            window.frames[id].print();
        }
    })
}

This code requires jQuery, but you can easily adapt it to any js flavour you want.

Cheers!

like image 73
Vadim Podlevsky Avatar answered Sep 23 '22 16:09

Vadim Podlevsky


I commented here about this issue this is where it all started

https://bugs.chromium.org/p/chromium/issues/detail?id=968914

like image 23
Alexandre Calvario Avatar answered Sep 22 '22 16:09

Alexandre Calvario