iframe content gets cut off when printing

前端 未结 5 969
谎友^
谎友^ 2021-01-05 11:56

I have a page with an iframe in it. The iframe\'s content is a couple of pages long, I\'ve set the iframe\'s height to match it\'s content. When I try to print the page, the

5条回答
  •  抹茶落季
    2021-01-05 12:31

    This source from 2012 says you can detect print requests in IE 5+, Firefox 6+, Chrome 9+, and Safari 5.1+ (Opera didn't work).

    (function() {
        var beforePrint = function() {
            console.log('Functionality to run before printing.');
        };
        var afterPrint = function() {
            console.log('Functionality to run after printing');
        };
    
        if (window.matchMedia) {
            var mediaQueryList = window.matchMedia('print');
            mediaQueryList.addListener(function(mql) {
                if (mql.matches) {
                    beforePrint();
                } else {
                    afterPrint();
                }
            });
        }
    
        window.onbeforeprint = beforePrint;
        window.onafterprint = afterPrint;
    }());
    

    Having caught the events you can then do something special for your iframe where it says

    console.log('Functionality to run before printing.');
    

提交回复
热议问题