04-20-2024, 05:20 AM | #1 |
Junior Member
Posts: 1
Karma: 10
Join Date: Apr 2024
Device: none
|
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) |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Having issues converting docx to epub | suenhoho | Conversion | 6 | 03-20-2019 05:16 PM |
Converting Epub to PDF Issues | necron_mason | Conversion | 14 | 07-05-2017 03:31 AM |
Issues with converting TXT to EPUB | glynbarlow | Conversion | 7 | 08-14-2013 12:55 PM |
bookmark issues converting HTML to EPUB | isabellkirsten | Calibre | 0 | 04-10-2010 12:47 AM |
Formatting issues when converting PDF to EPUB | raptir | Calibre | 2 | 10-21-2009 11:32 PM |