Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 04-20-2024, 04:20 AM   #1
matthewrussel
Junior Member
matthewrussel began at the beginning.
 
Posts: 1
Karma: 10
Join Date: Apr 2024
Device: none
Smile 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)
matthewrussel is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Having issues converting docx to epub suenhoho Conversion 6 03-20-2019 04:16 PM
Converting Epub to PDF Issues necron_mason Conversion 14 07-05-2017 02:31 AM
Issues with converting TXT to EPUB glynbarlow Conversion 7 08-14-2013 11:55 AM
bookmark issues converting HTML to EPUB isabellkirsten Calibre 0 04-09-2010 11:47 PM
Formatting issues when converting PDF to EPUB raptir Calibre 2 10-21-2009 10:32 PM


All times are GMT -4. The time now is 07:25 PM.


MobileRead.com is a privately owned, operated and funded community.