Production-ready PDF generation server powered by WeasyPrint on Scalingo. Convert HTML/CSS to high-quality PDF documents through a simple REST API.
-
Clone this repository:
git clone https://github.com/yourusername/weasyprint-server cd weasyprint-server -
Create a Scalingo app:
scalingo create my-pdf-server
-
Deploy:
git push scalingo main
Your PDF server will be available at https://my-pdf-server.osc-fr1.scalingo.io
Set these variables in your Scalingo dashboard or via CLI:
# CORS configuration (comma-separated origins)
scalingo -a my-pdf-server env-set ALLOWED_ORIGINS="https://myapp.com,https://app.myapp.com"
# Or allow all origins (not recommended for production)
scalingo -a my-pdf-server env-set ALLOWED_ORIGINS="*"This project uses the WeasyPrint buildpack. The .buildpacks file is already configured:
https://github.com/thibpoullain/weasyprint_buildpack
https://github.com/Scalingo/python-buildpack
- REST API for PDF generation
- Base64 encoding support
- Custom page formats and orientations
- CSS3 support including flexbox and grid
- Async processing with timeouts
- Health monitoring endpoint
- CORS support for web applications
- Automatic font handling
POST /generate
Generate a PDF from HTML content.
curl -X POST https://your-app.scalingo.io/generate \
-H "Content-Type: application/json" \
-d '{
"html": "<html><body><h1>Hello World</h1></body></html>",
"options": {
"format": "A4",
"landscape": false,
"base_url": "https://example.com"
}
}' \
--output document.pdfRequest Body:
{
"html": "<html>...</html>",
"options": {
"format": "A4",
"landscape": false,
"base_url": "https://example.com"
}
}Response: Binary PDF file
POST /generate-base64
Generate a PDF and return it as a base64-encoded string.
// JavaScript example
const response = await fetch('https://your-app.scalingo.io/generate-base64', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
html: '<html><body><h1>Hello World</h1></body></html>'
})
});
const data = await response.json();
// data.pdf contains the base64-encoded PDFResponse:
{
"pdf": "JVBERi0xLjQKJcfs...",
"size": 12345,
"hash": "d41d8cd98f00b204e9800998ecf8427e"
}GET /health
Check server status and WeasyPrint availability.
curl https://your-app.scalingo.io/healthResponse:
{
"status": "ok",
"weasyprint_version": "65.1",
"timestamp": "2025-01-19T12:00:00.000Z"
}GET /example
Generate a sample PDF to test the installation.
curl https://your-app.scalingo.io/example --output example.pdfAdd custom fonts to your PDFs:
<style>
@font-face {
font-family: 'CustomFont';
src: url('https://example.com/fonts/custom.ttf');
}
body {
font-family: 'CustomFont', Arial, sans-serif;
}
</style>Configure page size, margins, and headers/footers:
<style>
@page {
size: A4 landscape;
margin: 2cm;
@top-center {
content: "Company Report";
}
@bottom-right {
content: "Page " counter(page) " of " counter(pages);
}
}
</style>For best results with images:
<img src="https://example.com/image.png"
style="width: 300px; height: auto; image-rendering: -webkit-optimize-contrast;"># Build the image
docker build -t weasyprint-server .
# Run **locally**
docker run -p 5000:5000 -e ALLOWED_ORIGINS="*" weasyprint-server
# Test
curl http://localhost:5000/health# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Run the server
python app.py
# Server will be available at http://localhost:5000Increase the timeout in Procfile:
web: gunicorn app:app --timeout 300 --workers 2
Scale your dynos:
scalingo -a my-pdf-server scale web:MEnsure fonts are accessible via HTTPS and CORS is properly configured on the font server.
- Use absolute URLs for images
- Ensure the image server allows hotlinking
- Set the
base_urloption when generating PDFs
- Cache static assets: Use a CDN for images and fonts
- Optimize HTML: Minimize the HTML before sending
- Use efficient CSS: Avoid complex selectors and deep nesting
- Batch processing: For multiple PDFs, consider implementing a queue system
Contributions are welcome! To contribute:
- Fork the project
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Initial release
- REST API for PDF generation
- Base64 encoding support
- Health monitoring
- Example PDF generation
- CORS support
This project is licensed under the MIT License. See the LICENSE file for details.
- Issues: GitHub Issues
- WeasyPrint Documentation: weasyprint.org
- Scalingo Documentation: doc.scalingo.com
Made with β€οΈ