If anyone needs a solution when you can press the button to select print format and language. Then attach it, here is the working code:
you have to change the doc and prefered language.
// Auto Attach PDF on Button Click for Sales Order
function store_pdf_file(frm, pdf_file) {
let xhr = new XMLHttpRequest();
xhr.open('POST', '/api/method/upload_file', true);
xhr.setRequestHeader('Accept', 'application/json');
xhr.setRequestHeader('X-Frappe-CSRF-Token', frappe.csrf_token);
let form_data = new FormData();
form_data.append('file', pdf_file, frm.doc.name + '.pdf');
form_data.append('is_private', 1);
form_data.append('doctype', frm.doc.doctype);
form_data.append('docname', frm.doc.name);
xhr.onerror = function() {
frappe.throw(__('An Error occurred while attaching the PDF of the submitted transaction! Please do attach the PDF manually.'));
};
xhr.onload = function() {
if (xhr.status < 200 || xhr.status > 299) {
let message = ''
try {
let response = JSON.parse(xhr.responseText);
let server_message = JSON.parse(response._server_messages);
message = JSON.parse(server_message[0]).message;
// If we reached the attachment limit for a doctype, the response is an error message which is not clear.
message += ' ' + __('Please remove old attachments and do <b>attach the PDF of the submitted transaction manually</b>.');
}
catch (e) {
message = 'An Error occurred while attaching the PDF of the submitted transaction! Please do attach the PDF manually.';
}
frappe.throw(__(message));
} else {
frappe.msgprint(__('PDF attached successfully.'));
frm.reload_doc();
}
};
xhr.send(form_data);
}
function show_pdf_options_dialog(frm) {
// Fetch available print formats
frappe.call({
method: 'frappe.client.get_list',
args: {
doctype: 'Print Format',
fields: ['name'],
filters: {
doc_type: frm.doc.doctype
}
},
callback: function(response) {
let print_formats = response.message.map(format => format.name);
// Fetch available languages
frappe.call({
method: 'frappe.client.get_list',
args: {
doctype: 'Language',
fields: ['name']
},
callback: function(response) {
let languages = response.message.map(language => language.name);
// Create the dialog with fetched options
let d = new frappe.ui.Dialog({
title: __('Select PDF Options'),
fields: [
{
label: __('Print Format'),
fieldname: 'print_format',
fieldtype: 'Select',
options: print_formats,
default: print_formats[0]
},
{
label: __('Language'),
fieldname: 'language',
fieldtype: 'Select',
options: ['en', 'pl', 'cs'], // Add more languages as needed
default: 'pl'
}
],
primary_action_label: __('Generate PDF'),
primary_action(values) {
generate_and_attach_pdf(frm, values.print_format, values.language);
d.hide();
}
});
d.show();
}
});
}
});
}
function generate_and_attach_pdf(frm, print_format, language) {
let xhr_pdf = new XMLHttpRequest();
xhr_pdf.open(
'GET',
`/api/method/frappe.utils.print_format.download_pdf?doctype=${frm.doc.doctype}&name=${frm.doc.name}&format=${print_format}&_lang=${language}`,
true
);
xhr_pdf.setRequestHeader('Accept', 'application/pdf');
xhr_pdf.setRequestHeader('X-Frappe-CSRF-Token', frappe.csrf_token);
xhr_pdf.responseType = 'blob';
xhr_pdf.onload = function() {
if (xhr_pdf.status == 200) {
store_pdf_file(frm, xhr_pdf.response);
} else {
frappe.throw(__('Failed to generate PDF.'));
}
};
xhr_pdf.send();
}
frappe.ui.form.on('Sales Order', {
refresh: function(frm) {
// Add custom button to generate PDF and attach
frm.add_custom_button(__('Generate PDF and Attach'), function() {
console.log('Button clicked: Generate PDF and Attach');
show_pdf_options_dialog(frm);
});
}
});