Using frappe.utils.print_format.download_pdf without authentication

Hello World,

I have a question; In a custom python script, I’m trying to use frappe.utils.print_format.download_pdf by:

from frappe.utils.print_format import download_pdf
download_pdf(doctype, name)

But when I do I get this error:

Uncaught TypeError: Cannot read property 'session_expired' of null
    at Object.frappe.request.cleanup (desk.min.js:1482)
    at Object.<anonymous> (desk.min.js:1424)
    at i (jquery.min.js:2)
    at Object.fireWith [as rejectWith] (jquery.min.js:2)
    at z (jquery.min.js:4)
    at XMLHttpRequest.<anonymous> (jquery.min.js:4)

From the error it looks like I can’t access this function if I’m not logged in, so I then did:

import requests
s = requests.Session()
s.post("http://127.0.0.1:8000/api/method/login", data={"usr":user, "pwd":password})
r = s.get("http://127.0.0.1:8000/api/method/frappe.utils.print_format.download_pdf?doctype={}&name={}".format(doctype, name), stream=True)

And of cause it works. So I could use frappe-client, but I’d rather not provide the login details.
Would it be possible to @frappe.whitelist(allow_guest=True) or do something else to use this function?

You can create another whitelisted method which in turn will call the print_format method with appropriate arguments.

@netchampfaris thanks for the help, but that hasn’t solved it for me.

My code:

@frappe.whitelist(allow_guest=True)
def call_download_pdf(doctype, name):
	from frappe.utils.print_format import download_pdf
	download_pdf(doctype, name)

call_download_pdf(doctype, name)

This still produces these connected errors.
Which looks similar to this other issue posted on discuss.

Following his advice I did this. Which turned up no errors, but my previous code still failed. I’m unsure how to resolve this and would appreciate more help.
Thank you

P.S.
I forgot to mention before, but I already tried something similar to this; I edited frappe/utils/print_format.download_pdf, line 44, to be

@frappe.whitelist(allow_guest=True)
def download_pdf(doctype, name, format=None, doc=None):

instead of:

@frappe.whitelist()
def download_pdf(doctype, name, format=None, doc=None):

On Gitter @max_morais_dmm said:

allow guest doesn’t ensure that a guest will have permission to access the data;
it only allows a guest to make a call to a function.

download_pdf needs http://werkzeug.pocoo.org/docs/0.11/local/, I think it needs session details in request. Check frappe/__init__.py

If you don’t wish to store credentials use OAuth2. Web server and Mobile App flows are available.

2 Likes