How to limit the amount of impressions of an HTML document?

0

I need to limit the number of impressions of an HTML document .

Currently if the file is generated (via PHP), register in the bank a sts_impresso = 1 flag, however, there are times when the user did not print for some situations:

  • You clicked the cancel button;
  • Closed the window;
  • There were problems with internet connectivity at the time of downloading the file to be printed.

And in the three situations above, it was already marked as printed and the document can not be printed again.

Then I thought about performing the control via JavaScript:

function imprimir() {
    url = URL_DO_DOCUMENTO_IMPRESSO; // url do documento
    var nw = window.open(url, "popView" , 100, 100, "yes", "yes", true); // abro em nova janela
    $(nw).ready(function () { // assim que carregou, chama o print
        nw.print();
    });
}

How to detect whether the user clicked the Print button or the Cancel button on the browser print screen, or did he simply close the window?

From confirmation of printing, I want to make an AJAX request to mark it as printed and then close the window.

This is just an implementation idea to solve the problem described in the title of this question.

Different feasible implementations for solving the problem are welcome.

Note: I know that everything in the browser can be mocked and I can not trust the code coming from the Browser, but I need to at least minimize this problem.

    
asked by anonymous 03.09.2016 / 16:50

1 answer

2

Hello. I have a client that provides print certificates for those who take part in their lectures and workshops.

I use this code that works perfectly for me in almost all major browsers (IE 7+, Firefox 6+, Chrome 9+, and Safari 5.1+).

(function() {

var beforePrint = function() {
console.log('Função executada antes da impressão.');
// Aqui você pode detectar se a janela de impressão recebeu uma "call" e foi aberta e executar um pré script para enviar ao seu DB qual foi a ação.
};

var afterPrint = function() {
console.log('Função executada depois da impressão.');
// aqui você pode detectar se a impressão foi concluída e pode enviar a informação para o seu DB.
};

// agora aplicamos a função que detecta a requisição de impressão.
if (window.matchMedia) { // machMedia identifica recursos acionados pelo browser. Pois a janela de impressão é uma aplicação do Sistema operacional para gerenciar mídia de saída, neste caso a saída é via impressora (seja ela virtual - PDF - ou física).
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}

window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;

If the afterprint does not run, there may have been a connectivity problem or the printing has been canceled. You can then set a time counter (TimeOut) for this and call a script that tells your DB that the printout was not completed after X time interval. Expiring the print request, forcing the page to refresh or closing the modal / popup that contained the content to be printed.

I hope it's useful.

    
28.12.2016 / 17:01