Download Xlsx,Xml file on click of button - ashish-greycube/help GitHub Wiki

Step 1 : Create custom button

purchase_invoice.js

frappe.ui.form.on("Purchase Invoice",{
    refresh: function(frm) {
        frm.add_custom_button(__("Download XML"), function() {
            window.open(
		`/api/method/tally_xml.api.download_xml?invoice_id=${encodeURIComponent(
			frm.doc.name)}`,
		"_blank"
	);
        });
    }
})

api.py

@frappe.whitelist()
def download_xml(invoice_id: str):
	frappe.local.response.filename = f"{invoice_id}.xml"
	invoice_doc = frappe.get_doc("Purchase Invoice", invoice_id)
	frappe.local.response.filecontent = create_xml_file(invoice_doc,  method=None)  #function to create xml file
	frappe.local.response.type = "download"


def create_xml_file(doc, replace=False):
	invoice_xml = frappe.render_template(
		"xyz.xml",
		context={"doc": doc, "item_meta": doc.items},
		is_path=True,
	)
	
	return invoice_xml