Epub2PDF Converter is a Python microservice that enables the conversion of .epub
files to .pdf
through a REST API. The service uses Flask for API creation and libraries such as EbookLib
and WeasyPrint
for file manipulation and conversion.
- Convert
.epub
files to.pdf
- File upload support via HTTP POST request
- Direct PDF file return in the response
- Preservation of original EPUB images in final PDF
- Character encoding handling
- Python 3.10
- Flask
- EbookLib
- WeasyPrint
- Docker and Docker Compose installed
git clone https://github.com/jugleni/Epub2PDF.git
cd Epub2PDF
Build and start the container:
docker-compose up --build
The service will be available at http://localhost:3453.
You can use Postman or any HTTP client tool to test the API.
- Endpoint:
/convert
- Method: POST
- Content-Type: multipart/form-data
- Parameter: file (EPUB file)
- Response: PDF file
- Service URL: http://localhost:3453
Basic Setup:
- Create new POST request to
http://localhost:3453/convert
- Go to "Body" tab
- Select "form-data"
- Add key
file
(Type: File) - Upload your EPUB file
Detailed Testing Steps:
-
Request Configuration:
- Method: POST
- URL: http://localhost:3453/convert
- Headers: None required (auto-set by Postman)
-
File Upload:
- In Body tab → form-data
- Key:
file
- Click dropdown next to key → Select 'File'
- Value: Click 'Select Files' → Choose EPUB file
-
Sending and Receiving:
- Click 'Send'
- Expected Response Code: 200 OK
- Response Type: application/pdf
-
Saving the PDF:
- Click 'Save Response' button (disk icon)
- Select 'Save as File'
- Choose location and name (use .pdf extension)
curl -X POST \
-F "file=@/path/to/your/book.epub" \
http://localhost:3453/convert \
--output converted.pdf
Expected Output:
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 15.2M 100 14.2M 100 1021k 4.2M 307k 0:00:03 0:00:03 --:--:-- 4.5M
import requests
url = 'http://localhost:3453/convert'
files = {'file': open('book.epub', 'rb')}
response = requests.post(url, files=files)
with open('output.pdf', 'wb') as f:
f.write(response.content)
-
Successful Conversion:
- Status: 200 OK
- Content-Type: application/pdf
- Body: Binary PDF data
-
Error Responses:
// 400 Bad Request {"error": "No file provided"} // 415 Unsupported Media Type {"error": "Invalid file format. Please upload an EPUB file"} // 500 Internal Server Error {"error": "Conversion failed"}
- File Upload: EPUB received via HTTP POST
- Temporary Storage: File saved temporarily
- Content Extraction:
- Text content extracted
- Images processed
- Character encoding fixed
- Conversion Steps:
- Images converted to base64
- HTML template generated
- WeasyPrint processes HTML to PDF
- Response: PDF returned to client
- Cleanup: Temporary files removed
# Start service
docker-compose up --build
# Stop service
docker-compose stop
# Remove container
docker-compose down
# View logs
docker-compose logs -f
-
Monitoring:
- View logs:
docker-compose logs -f
- Check container status:
docker ps
- View logs:
-
Resource Management:
- Default port: 3453
- Memory usage varies with EPUB size
- CPU usage spikes during conversion
-
Troubleshooting:
- Check container status
- Verify port availability
- Monitor resource usage
- Review conversion logs
-
File Size:
- Maximum recommended: 50MB
- Larger files may cause memory issues
-
Content:
- Complex layouts may not preserve perfectly
- Image quality matches source
- Some CSS properties might not convert
-
Performance:
- Processing time varies with file size
- Memory usage scales with content complexity
- Multiple simultaneous conversions may impact performance
/epub2pdf-converter
│ ├── app.py # Main microservice code
│ ├── Dockerfile # Docker container configuration
│ ├── requirements.txt # Python dependencies
│ ├── docker-compose.yaml # Docker Compose configuration
│ └── README.md # This file
If you encounter issues with the conversion:
- Ensure the EPUB file is valid and can be opened in other readers.
- Make sure the file is not corrupted.
- For specific encoding issues, check the container logs for more details.
Contributions are welcome! Feel free to open issues and pull requests to improve the project.
This project is licensed under the MIT License.
Created by Jugleni Krinski