Try ERPNext Buy Support Partners Foundation

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, me.frm.doc.name)
		doc.print = doc.print + 1;
		refresh_field(doc.print);	
		console.log('print===='+doc.print);	
		me.frm.save();
		var w = window.open(frappe.urllib.get_full_url("/printview?"
			+"doctype="+encodeURIComponent(me.frm.doc.doctype)
			+"&name="+encodeURIComponent(me.frm.doc.name)
			+(printit ? "&trigger_print=1" : "")
			+"&format="+me.selected_format()
			+"&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, me.frm.doc.name)

		if (me.frm.doc.print < 1){
			frappe.call({
           			"method": "frappe.client.set_value",
		        	"args": {
		                		"doctype": me.frm.doc.doctype,
	                			"name": me.frm.doc.name,
			        	        "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 () {
                    console.log("print");
                });
                break;
            }
        }
    }
}

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.