I am having issues converting epub to pdf
Hi there, I am not sure if this is something you help with but I am using this tool in python to convert epub to pdf files. Basically a .epub file is uploaded and goes into the uploads folder then it runs the ebook-convert command (is this right?) then it is suppose to convert but it isn't working and failing the conversion on my linux vps hosting.
Calibre is installed:
ebook-convert --version
ebook-convert (calibre 6.11.0)
Created by: Kovid Goyal <kovid@kovidgoyal.net>
/usr/bin/ebook-convert
My code is here (epubtopdf.py):
import os
from flask import Flask, request, send_file, render_template
from jinja2 import FileSystemLoader, ChoiceLoader
app = Flask(__name__, static_folder='../static', static_url_path='/static')
current_directory = os.path.abspath(os.path.dirname(__file__))
# Configure Jinja2 to allow for multiple template folders
template_folders = [
os.path.join(current_directory, 'templates'),
os.path.join(current_directory, '..','shared-templates'),
]
# Create a ChoiceLoader with the default loader and an additional loader
template_loader = ChoiceLoader([
FileSystemLoader(template_folder) for template_folder in template_folders
])
# Set the loader for the Flask app
app.jinja_loader = template_loader
UPLOAD_FOLDER = os.path.join(current_directory, 'uploads')
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Path to Calibre's ebook-convert command
EBOOK_CONVERT_CMD = '/usr/bin/ebook-convert'
@app.route('/epub-to-pdf')
def index():
return render_template('epub-to-pdf.html')
@app.route('/convert-epub-to-pdf', methods=['POST'])
def convert_epub_to_pdf():
# Check if a file was uploaded
if 'epubFile' not in request.files:
return render_template('epub-to-pdf.html', error='No file uploaded.')
epub_file = request.files['epubFile']
# Check if the file has a valid name and extension
if epub_file.filename == '':
return render_template('epub-to-pdf.html', error='No selected file.')
if not epub_file.filename.lower().endswith('.epub'):
return render_template('epub-to-pdf.html', error='Invalid file format. Please upload an EPUB file.')
# Create the uploads folder if it doesn't exist
if not os.path.exists(UPLOAD_FOLDER):
os.makedirs(UPLOAD_FOLDER)
# Save the uploaded EPUB file
epub_file.save(os.path.join(UPLOAD_FOLDER, 'input.epub'))
# Convert EPUB to PDF using Calibre's ebook-convert command
output_pdf_path = os.path.join(UPLOAD_FOLDER, 'output.pdf')
command = f'{EBOOK_CONVERT_CMD} "{os.path.join(UPLOAD_FOLDER, "input.epub")}" "{output_pdf_path}"'
print("Executing command:", command)
result = os.system(command)
print("Conversion result:", result)
# Check if PDF conversion was successful
if not os.path.exists(output_pdf_path):
return render_template('epub-to-pdf.html', error='Failed to convert EPUB to PDF.')
# Send the converted PDF file for download
return send_file(output_pdf_path, as_attachment=True)
if __name__ == '__main__':
app.run(debug=True, port=8080)
|