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

Asked

Viewed 645 times

0

Precise limit the number of printouts of an HTML document.

Currently if the file is generated (via PHP), record in the database a flag sts_impresso = 1, however, there are times when the user has not printed for some situations:

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

And in the three situations above, it had already been marked as printed and the document is unable to be printed again.

So I thought to carry out 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 if user clicked button Print out or on the button Cancel on the browser print screen, or if it simply closed the window?

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

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

Different viable implementations for problem solving will be welcome.

Note: I know that everything in the browser can be swindled and I cannot trust the code coming from the CLIENT (Browser), but I need to at least minimize this problem.

  • 2

    You just discovered one of the problems of doing web stuff. Actually anything that depends on the client controlling and informing the server will have problems. Have you heard that can never trust what comes from the customer? Even if you create a mechanism that helps, you cannot trust it. It can fail and it can be swindled. It will continue with the problem. In a native application it is more difficult to fail, easier to detect and more difficult to circumvent (assuming that it doesn’t matter much if it is printed several times, since this can always be circumvented by the customer).

  • That’s exactly what @bigown said, it also has the option to print in PDF, and so it will be able to print as many times as you want afterwards, from the generated PDF..

  • Also that the bigown and @Marcogiovanni already said, you don’t even know how many copies the person chose in each print "request".

  • @bigown, Marcogiovanni and Bacco... would there be any way to at least minimize the problem? ... at least detect if user clicked to print or cancel?

  • No. Nor is it universalized among browsers. Anything that needs minimal custom control will suffer from the limits of Web applications, as @bigown commented. Even a print-only media would only be accessed by the person seeing the preview. There’s not much to do.

1 answer

1

Hello. I have a client who provides certificates for printing for those who participate in his 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 is not run, possibly there was connectivity problem or the print was canceled. You can then set a time counter (Timeout) for this and call a script that tells your DB that printing was not completed after X time interval. Expire the print request, forcing the page to be updated or closing the modal/pop-up that contained the content to be printed.

I hope it will be useful.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.