Issue with rendering epub file using epub.js, Flask, and sqlalchemy

25 views Asked by At

Okay so I don't understand what's wrong here, I am a beginner in web development. Currently building an epub reader web app. I am trying to send the epub file to the client-side, and then render it using epub.js. Sorry if the code is messy. Any help will be appreciated!

Here is my Flask:

@web_app.route('/login/<username>/<title>')
def epub(username, title):
    with engine.connect() as conn:
        query = select(book_entry.c.ebook).where(
            (book_entry.c.title == title)
        )
        result = conn.execute(query)
        row = result.fetchone()
        epub_path = row[0]
        epub_file = url_for('get_epub', username=username, title=title, filepath=epub_path)
    return render_template('epub.html', username=username, epub=epub_file)

@web_app.route('/login/<username>/<title>')
def get_epub(username, title, filepath):
    directory = fr'D:\\Web App Exam\server\{username}'
    filename = os.path.basename(filepath)
    return send_from_directory(directory, filename)

and here is how I handle it in epub.js:

let epub_file = "{{ epub }}"
let book = ePub(epub_file, { openAs: "epub" });
let rendition = book.renderTo("area-epub-read", {
    width: window.innerWidth, height: window.innerHeight
});
let displayed = rendition.display();

and here is the html file:

<!DOCTYPE html>
<html lang="en">
<head>
<link href= "/static/style.css" rel="stylesheet" />
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/epubjs/dist/epub.min.js"></script>


</head>
<body class="epub-body">

<a id="prev" href="#" class="navlink"><</a>
    <div class="epub-container">
        <div id="area-epub-read"></div>
    </div>
<a id="next" href="#" class="navlink">></a>

<script src="/static/scripts.js"></script>
</body>


</html>

I seem to have trouble with sending the epub file to the client side. I tried using read() but when I printed it, it printed a ridiculously high amount of unreadable string. When I run the exact code I showed here, it just returns the status code 500 on the console.

0

There are 0 answers