Custom script on print event

Anybody know what is the trigger event when user click print button on each doctype?
I want to insert counter to know how many times the document is printed… Can we use custom js script to do this?

Anybody has suggestion?

You’ll find what you’re looking for in print.js:

1 Like
new_page_preview: function(printit) {
		var me = this;
		var doc = frappe.get_doc(me.frm.doc.doctype,
		doc.print = doc.print + 1;
		var w ="/printview?"
			+(printit ? "&trigger_print=1" : "")
			+"&no_letterhead="+(me.with_letterhead() ? "0" : "1")
			+(me.lang_code ? ("&_lang="+me.lang_code) : "")));
		if(!w) {
			msgprint(__("Please enable pop-ups")); return;

custom field ‘print’ was changed but cannot save…
get this exception:
frappe.DocstatusTransitionError(_(“Cannot change docstatus from 1 to 0”))
DocstatusTransitionError: Cannot change docstatus from 1 to 0

Hi, you need to allow custom field to be modified after submit by checking “Allow on Submit” in Customize Form.

I have checked “Allow on Submit” but did not save…when we click print open print dialog…the form is freeze so we cannot modify values. But if we change the field manually then we can save/update the form

Do you have any suggestion?

Where to put the code so I can update the field when user click print button on browser print dialog? not ‘print’ on preview form?

I’m using this api script inside print.js… the problem is the code is executed when user click print button (inside erpnext)…but not on browser print dialog… Anybody know how to update the field when user really clicks the print on browser print dialog so we can exactly know the document is printed??

new_page_preview: function(printit) {
		var me = this;
		//var doc = frappe.get_doc(me.frm.doc.doctype,

		if (me.frm.doc.print < 1){{
           			"method": "frappe.client.set_value",
		        	"args": {
		                		"doctype": me.frm.doc.doctype,
			        	        "fieldname": "print",
                				"value": me.frm.doc.print + 1

Maybe this will help:

Hello jof2jc,

You can use the javascript to handle button event for “Print” button in print preview like

function handlePrintButtonClickEvent() {
    var btn = document.getElementsByClassName('btn-print-print');
    if(btn) {
        for(var i = 0; i < btn.length; i++) {
            if (btn[i].textContent.trim() == "Print") {
                btn[i].addEventListener("click", function () {

Hi @Vijay_Kalathiya

Thank you for your answer, but when can I write this code, in custom script of the DocType or here /frappe/public/js/frappe/form/print.js

Thank you in advance.