PDF Download Button -Option

Hi community
I want PDF Download Button (below to Print) to download PDF for selected documents directly.

@Humming_Bird Click On Print Button Then Wait For Complete For Process Then Automatically Pop up Download PDF Button

I know this process. Actually I don’t want to print document, I want Download PDF Button (below print)

Write Your custom script on sales order page list for add your custom button and add custom download pdf function in server script doctype

1 Like

Hi @Humming_Bird,

Please apply it.

frappe.listview_settings['Sales Order'] = {
    onload: function(listview) {
        listview.page.add_action_item(__("Download PDF"), () => {
            download_pdf(listview);
        });

        let $print_button = $('.actions-btn-group .dropdown-menu .dropdown-item:contains("Print")');
        let $action_button = $('.actions-btn-group .dropdown-menu .dropdown-item:contains("Download PDF")');

        if ($action_button.length && $print_button.length) {
            $print_button.after($action_button.parent().detach());
        }
    }
};

function download_pdf(listview) {
    const docs = listview.get_checked_items();
    const doctype = listview.doctype;
    const print_settings = frappe.model.get_doc(":Print Settings", "Print Settings");
    const allow_print_for_draft = cint(print_settings.allow_print_for_draft);
    const is_submittable = frappe.model.is_submittable(doctype);
    const allow_print_for_cancelled = cint(print_settings.allow_print_for_cancelled);

    const valid_docs = docs
        .filter(doc => {
            return (
                !is_submittable ||
                doc.docstatus === 1 ||
                (allow_print_for_cancelled && doc.docstatus == 2) ||
                (allow_print_for_draft && doc.docstatus == 0) ||
                frappe.user.has_role("Administrator")
            );
        })
        .map(doc => doc.name);

    const invalid_docs = docs.filter(doc => !valid_docs.includes(doc.name));

    if (invalid_docs.length > 0) {
        frappe.msgprint(__("You selected Draft or Cancelled documents"));
        return;
    }

    if (valid_docs.length === 0) {
        frappe.msgprint(__("Select at least 1 record for printing"));
        return;
    }

    const json_string = JSON.stringify(valid_docs);
    const default_print_format = frappe.get_meta(doctype).default_print_format;
    const with_letterhead = 1;
    const print_format = default_print_format;
    const pdf_options = JSON.stringify({ "page-size": "A4" });

    frappe
        .call("frappe.utils.print_format.download_multi_pdf_async", {
            doctype: doctype,
            name: json_string,
            format: print_format,
            no_letterhead: with_letterhead ? "0" : "1",
            letterhead: null,
            options: pdf_options,
        })
        .then(response => {
            let task_id = response.message.task_id;
            frappe.realtime.task_subscribe(task_id);
            frappe.realtime.on(`task_complete:${task_id}`, data => {
                window.open(data.file_url);
                frappe.realtime.task_unsubscribe(task_id);
                frappe.realtime.off(`task_complete:${task_id}`);
            });
        });
}

Output:

2 Likes

Thanks, will try it :handshake:

@NCP Thanks.

After applying client script, print format is not showing:

Pls guide what should I do next?

Please check the video, and follow the steps. Also, I tested in v14. so worked properly.

Your code is OK. I think there is issue of wkhtmltopdf. Can you guide how can I install this with V15?

Check the thread: How to install wkhtmltopdf with patched qt in bench

lots of thread in the forum so search it.