2016-05-27 69 views
0

我試圖使用WeasyPrint從我的Flask應用程序下載頁面,但是當下載PDF時,我將登錄頁面作爲PDF而不是預期頁面。如何在密碼保護頁面上使用WeasyPrint PDF Builder?

我用下面的代碼:

@app.route('/report.pdf') 
def hello_pdf(): 
    # Make a PDF from another view 
    return render_pdf(url_for('myprojects')) 

登錄裝飾是:

def login_required(f): 
    '''login required decorator to protect routes 
    ''' 
    @wraps(f) 
    def wrap(*args, **kwargs): 
     if 'logged_in' in session: 
      return f(*args, **kwargs) 
     else: 
      flash('You need to login first.') 
      return redirect(url_for('login')) 
    return wrap 

出於某種原因,該功能似乎無法下載受保護的視圖,儘管我是登錄。我怎樣才能讓它正確下載?

回答

0

原來render_template只返回一個HTML字符串,這樣我就可以在路線的末端執行以下操作:

html = render_template('myprojects.html', projects=projects) 
return render_pdf(HTML(string=html))