How to send a pdf file for ajax response using flask?

Solution
from flask import (
    Flask,
    render_template,
    send_file
)
from fpdf import FPDF
import io

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/pdf/create')
def create_pdf():
    pdf = FPDF()
    pdf.set_title('My Example PDF')
    pdf.set_author('Artist Unknown')
    pdf.set_subject('Creation of a PDF')
    pdf.add_page()
    pdf.set_font('Arial', 'B', 16)
    pdf.cell(40, 10, 'Hello World')

    stream = io.BytesIO(pdf.output(dest='S').encode('latin-1'))
    return send_file(
        stream,
        mimetype='application/pdf',
        attachment_filename='example.pdf',
        as_attachment=False
    )


  
    
    
  
  
    Download