Custom print button to print using specific format ( Or change print format selection programmatically)

For a doc, i have multiple print format that I need to use in multiple scenarios, currently what I am doing is selecting the required print format from the drop-down menu in the print preview list.

Is there any way where I can create a multiple print button for each print format which will automatically select required print format and open the print preview when clicked ? Or any other method / solution for this?

This will be a lot more convenient compared to what I am currently doing.

Solved it.

  1. added a field in doctype called "print_format_selector

  2. created custom button to call print function and set it to load on page refresh

     function custom_print(frm){
         cur_frm.add_custom_button(__("Button Name 1"), function() {
             cur_frm.set_value("print_format_selector","format_1")
             cur_frm.print_doc();
         }); 
     cur_frm.add_custom_button(__("Button Name 2"), function() {
         cur_frm.set_value("print_format_selector","format_2")
             cur_frm.print_doc();
         }); 
    }
    
  3. Overrided the default print format fetching function and added an if condition to check if there exists any value in that field and if there exists any, return that value as the selected print format

     $.extend(frappe.meta, {
     get_print_formats: function(doctype) {
         var print_format_list = ["Standard"];
         var default_print_format = locals.DocType[doctype].default_print_format;
         let enable_raw_printing = frappe.model.get_doc(":Print Settings", "Print Settings").enable_raw_printing;
         var print_formats = frappe.get_list("Print Format", {doc_type: doctype})
             .sort(function(a, b) { return (a > b) ? 1 : -1; });
         $.each(print_formats, function(i, d) {
             if (
                 !in_list(print_format_list, d.name)
                 && d.print_format_type !== 'JS'
                 && (cint(enable_raw_printing) || !d.raw_printing)
             ) {
                 print_format_list.push(d.name);
             }
         });
    
         if(default_print_format && default_print_format != "Standard") {
             var index = print_format_list.indexOf(default_print_format);
             print_format_list.splice(index, 1).sort();
             print_format_list.unshift(default_print_format);
         }
    
         if(cur_frm.doc.print_format_selector){ //newly added if condition
             var print_format = [cur_frm.doc.print_format_selector]
             return print_format
         }
         else{
             return print_format_list;
         }
     },
    });
    

To override, i created a new file in apps/appname/appname/public/js/over_rided_func.js and included that file in build.json and also in hooks.py did a bench build once, then bench --site sitename migrate, bench clear-cache, bench restart

in build.json

{
    "js/over_rided_func.js": [
	    "public/js/over_rided_func.js"
     ]
}

in hooks.py

doctype_js = {"MyDoctype" : "public/js/over_rided_func.js"}
6 Likes

Hi, i have this case but i don’t now how to enter your solution. The “$.extend” must be included on custom script doctype or inside the frappe proyect?

Best regards