Download PDF API

Hi there,
I have a fully working logic to custom downloading (pdf) through an API, but I cannot manage to pass the

settings={"compact_item_print": 1}

to frappe.get_print()

image

here is my fully working API function

from frappe.utils.pdf import get_pdf
import frappe

@frappe.whitelist(allow_guest=True)
def download_pdf(**kwargs):
    frappe.set_user("Administrator")
    args = frappe._dict(kwargs)

    doc =frappe.get_doc("Sales Invoice", args.name)
    html = frappe.get_print("Sales Invoice", args.name, "My Print Format", doc=doc, no_letterhead="no_letterhead")

    args = frappe._dict(kwargs)
    frappe.local.response.filename = f"{args.name}.pdf"
    frappe.local.response.filecontent = get_pdf(html)
    frappe.local.response.type = "download"    

Any help is really appreciated
Dave,

1 Like

Just pass the settings as an additional parameter in your request. get_print will use it automatically.

Thank you @snv ! I am afraid this will not work. While at it, if I pass it through http://server:port/api/method/frappe.utils.print_format.download_pdf? (frappe’s module) it works just fine, but if pass it through my custom app’s API (http://server:port/api/method/myapp.myapp.myapi.download_pdf?) it has no use. I have not delved into it but it seems as if the “flags” property of the returned object of frappe.get_doc() needs to be populated…

I read the code and the logic is get_print => printview.

In printview, we’re checking for frappe.form_dict.settings. So as long as this variable is set, it should override your default print settings.

@snv the variable settings in …/apps/frappe/frappe/www/printview.py line 34 has a value of “None” during this call.


Just to let you know my own function “dowload_pdf()” is part of my public API where the HTTP POST initiator is not being authenticated. I am simply impersonating the “Administrator” user by calling:

 frappe.set_user("Administrator")

Like I said earlier if the request comes through an authenticated user all works fine. My problem is that the caller of this API cannot populate the the HTTP headers to send me:

Authorization: token <api_key>:<api_secret>

ERPNext: v13.23.3 (version-13)
Frappe Framework: v13.23.0 (version-13)

Any help is really appreciated!
Dave,

Any core developers pitching in on this post?
Many thanks!
Dave