DWQA QuestionsCategory: Programcall window.print () method, how to get the print or cancel status?
caroline_PP asked 2 months ago

The printing function is needed in the project, and the printing function is used pdf.js Plug in for;
Click Print to call window.print () method, and then the browser’s own print preview page will appear with print button and cancel button. How to get whether the print button or cancel button has been clicked? To facilitate the execution of the printed callback function.
The page of print preview is shown in the figure below. The page is provided by the browser, and the method triggered by print and Cancel buttons cannot be obtained from outside.
图片描述

fuguxux replied 2 months ago

Has the landlord’s problem been solved

fuguxux replied 2 months ago

Ask the same question

fuguxux replied 2 months ago

Has the problem been solved

6 Answers
woxiexienia answered 2 months ago

The solution is as follows

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;
Lake Baikal replied 2 months ago

It doesn’t solve the problem

Wang Doudou's Vest replied 2 months ago

It doesn’t work, boss

TJone replied 2 months ago

When you click the print event of the page, you call before. The print and cancel of the pop-up box are called after.

Tang Dynasty answered 2 months ago

Brother, have you found a way

watermelon answered 2 months ago

Click trigger event to activate the browser to print. If other click events are triggered immediately before calling up the browser to print, this event will be suspended. When you click the cancel printing button, this event will be executed immediately.

How heavy it is to have a good sleep answered 2 months ago

Archaeology before 2020
var pwin = window.open (); / / open a new window
pwin.document.write (printstr); / / write the printed content
pwin.document.close (); / / er… I don’t know what that means, ha ha
pwin.print (); / / call printer
pwin.close () / / click Cancel and print to close the newly opened window

user_0OYBoVtd replied 2 months ago

What is it? Have you reviewed it

AyoDosahard replied 2 months ago

Your line of thinking is JS to write the body of the new window and then a set of operations. Now we may have opened a page that is specially used for printing, and then we can also turn on the browser’s own printing. Now we are one step away from how to get the user to click the print or cancel button, and then do some other operations

Rest and be indifferent answered 2 months ago

Man, have you found a way

Aliya answered 2 months ago

Brother, have you found a way