|
|
|
#!/usr/bin/env/ python
|
|
|
|
|
|
|
|
from flask import Flask, render_template, request, Response
|
|
|
|
# from weasyprint import HTML
|
|
|
|
from pagedjs import make_pdf
|
|
|
|
|
|
|
|
try:
|
|
|
|
from paseo import crear_camino
|
|
|
|
except ModuleNotFoundError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
basepath = os.path.dirname(__file__)
|
|
|
|
|
|
|
|
BASEURL = ''
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
|
|
@app.route('{}/'.format(BASEURL))
|
|
|
|
def index():
|
|
|
|
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
|