✊🏿 Black Lives Matter. Please consider donating to Black Girls Code today.
🧬 Learn how to build RNA-Seq data apps with Python & Dash. Register for the May 20 Webinar!

Downloading a PDF via static link


I am looking to have a download file link. Is there a specific component for this? How have others accomplished this? I’ve done it before with and EXCEL file but I’m having a hard time with PDF format.



Maybe you could use my Download component? I haven’t tried with PDF, but I guess it should work.

EDIT: If you only need static files, you could also just serve them directly via the flask server.

Thanks! I’ll have a look

It doesn’t appear that pandas has a pdf writer.

Ah, you don’t need that. The pandas step was only necessary in the other example to transform the python data structure into the desired output format. You should be able to write the bytes directly. Here is an example,

import os
import dash
import dash_html_components as html

from dash.dependencies import Output, Input
from dash_extensions import Download

# Create app.
app = dash.Dash(prevent_initial_callbacks=True)
app.layout = html.Div([html.Button("Download pdf", id="btn"), Download(id="download")])

@app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
def generate_csv(n_nlicks):
    # Path to a pdf file on the server.
    pdf_dir = "C:/Users/emilh/Dropbox"
    pdf_file = "Get Started with Dropbox.pdf"
    # Convert data to a string.
    with open(os.path.join(pdf_dir, pdf_file), 'rb') as f:
        content = list(f.read())
    # The output must follow this form for the download to work.
    return dict(filename=pdf_file, content=content, type="text/pdf")

if __name__ == '__main__':
1 Like

Ahh I was over complicating it. Thanks for the explanation!