问题
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?
回答1:
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
回答2:
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!
回答3:
I commented here about this issue this is where it all started
https://bugs.chromium.org/p/chromium/issues/detail?id=968914
回答4:
Chrome direct printing has been replaced by the WebApp Hardware Bridge. The git repository is here: https://github.com/imTigger/webapp-hardware-bridge
I downloaded last week and am currently testing.
来源:https://stackoverflow.com/questions/57895126/chrome-77-not-auto-printing-pdfs