You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.3 KiB
Python

3 years ago
#!/usr/bin/env/ python
3 years ago
from flask import Flask, render_template, request, Response
# from weasyprint import HTML
3 years ago
from pagedjs import make_pdf
try:
from paseo import crear_camino
except ModuleNotFoundError:
pass
import os.path
basepath = os.path.dirname(__file__)
3 years ago
BASEURL = ''
app = Flask(__name__)
@app.route('{}/'.format(BASEURL))
def index():
3 years ago
return render_template('index.html')
@app.route('{}/book'.format(BASEURL), methods=['POST'])
def book():
fragment = max(0, min(1, int(request.form['fragment'])))
first_word = 'un'
if fragment == 0:
novel = os.path.join(basepath, '../data/emilia_prueba.txt')
author = 'Emilia Pardo Bazán'
title = 'La Madre Naturaleza'
else:
novel = os.path.join(basepath, '../data/prueba.txt')
author = 'Benito Pérez Gáldos'
title = 'Miau'
path = crear_camino(novel, first_word)
context = { 'title': title, 'author': author, 'path': path }
html = render_template('book.html', **context)
# pdf = HTML(string=html).write_pdf()
# Use pagedjs as weasyprint does not seem to support our layout.
pdf = make_pdf(html)
r = Response(pdf, mimetype='application/pdf')
r.headers.extend({
'Content-Disposition': 'attachment; filename="Paseo por arboles de madrid.pdf"'
})
return r
# return html