Search code examples
pythonplotly-dashfpdf

Dash extensions problem downloading PDF file generated as a byte stream


I have a Dash application where the user interacts with the app and a PDF will be generated using FPDF. I am trying to use the Dash-extensions package and the download option to forward the PDF to be downloaded but am getting an error.

I have this function to generate the PDF:

def makeReport(info):
    return create_pdf(info)
def create_pdf(info):
    pdf = CustomPDF(format='letter')
    for i, beam in enumerate(info):
        pdf.addPage(info[i],i)

    data = pdf.output(dest='S').encode('latin-1')
    
    return data

Then this is the download button related code:

@app.callback(Output("download", "data"), [Input("download_btn", "n_clicks")], prevent_initial_call=True)
def sendPDF(n_clicks):
    firstName, lastName, mrn = nameParser(patient)
    fileName = lastName + ', ' + firstName + ' Cutout Factor Calc.pdf'
    return send_bytes(makeReport(info), fileName)

def downloadButton():
    return html.Div([html.Hr(),html.Button("Download Report", id="download_btn"), Download(id="download")])

The error I get when I click the button is:

enter image description here


Solution

  • The send_bytes utility function expects as first argument a function that writes bytes to BytesIO object, but you are passing it a byte string, so you need to write a small wrapper function. Here is a small example demonstrating how it can be done,

    import dash
    import dash_html_components as html
    from dash.dependencies import Output, Input
    from dash_extensions import Download
    from dash_extensions.snippets import send_bytes
    from fpdf import FPDF
    
    # Create example app.
    app = dash.Dash(prevent_initial_callbacks=True)
    app.layout = html.Div([html.Button("Download PDF", id="btn"), Download(id="download")])
    
    
    def create_pdf(n_nlicks):
        pdf = FPDF()
        pdf.add_page()
        pdf.set_font('Arial', 'B', 16)
        pdf.cell(40, 10, f'Hello World! (n_clicks is {n_nlicks})')
        return pdf
    
    
    @app.callback(Output("download", "data"), [Input("btn", "n_clicks")])
    def generate_xlsx(n_nlicks):
        def write_pdf(bytes_io):
            pdf = create_pdf(n_nlicks)  # pass argument to PDF creation here
            bytes_io.write(pdf.output(dest='S').encode('latin-1'))
    
        return send_bytes(write_pdf, "some_name.pdf")
    
    
    if __name__ == '__main__':
        app.run_server()