Forums

Not found error when passing a pdf filename to download

Hi, in my webapp, I generate a pdf file with dynamic name by a function. Then I want the app to download it on the client. I tried passing the name to ajax->success, then create a link attribute. It generates "Not found" error.

def pdf_file_generator():
    # ... file building instructions
    pdf.output(f"/home/gionata15/templates/docs/{filename}")
    return filename

JS approach

def download():
    filename = pdf_file_generator()
    return jsonify(f"/home/gionata15/templates/docs/{filename}")

attempt to download from app.py

def download():
    filename = pdf_file_generator()
    return send_file(f"/home/gionata15/templates/docs/{filename}", as_attachment=True)

I tried with return send_file and return send_from_directory but nothing happens.

Any solution?

Thanks

GM

If you're getting a 404 error when accessing the endpoint that serves your file, make sure that you're generating that URL correctly and that it actually points at the view that you have defined.

In the end I solved with

return jsonify(render_template("dwlink.html", pdf_file=filename))

In dwlink.html

<a href='/docs/{{pdf_file}}'>Download PDF</a>

Hope this could be useful, thanks for support GM

Thanks for sharing that!