Error generating PDF: cannot load library 'gobject-2.0-0': gobject-2.0-0: cannot open shared object file: No such file or directory. Additionally, ctypes.util.find_library() did not manage to locate a library called 'gobject-2.0-0'
WeasyPrint requires several system libraries for rendering HTML/CSS to PDF. The gobject-2.0-0 library is part of the GLib/GObject system that WeasyPrint uses for rendering.
Update your Dockerfile to include the required system libraries:
# Install system dependencies
RUN apt-get update && apt-get install -y \
curl \
tzdata \
# WeasyPrint dependencies
libgdk-pixbuf2.0-0 \
libpango-1.0-0 \
libcairo2 \
libpangocairo-1.0-0 \
libffi-dev \
shared-mime-info \
# Additional fonts and rendering support
fonts-liberation \
fonts-dejavu-core \
&& rm -rf /var/lib/apt/lists/*Then rebuild your Docker container:
docker-compose down
docker-compose build --no-cache
docker-compose upThe system automatically falls back to ReportLab when WeasyPrint fails. ReportLab generates basic but functional PDFs without external dependencies.
Pros:
- No system dependencies required
- Always works
- Faster generation
Cons:
- Less styling control
- No CSS support
- Basic layout only
Consider these alternatives if both WeasyPrint and ReportLab fail:
# Install alternative PDF libraries
pip install pdfkit # Requires wkhtmltopdf
pip install xhtml2pdf # Pure Python, limited featuresRun the test script to diagnose issues:
# Inside the container
python /app/docker/test-pdf-generation.py
# Or from host
docker exec -it your-container-name python /app/docker/test-pdf-generation.py=== PDF Generation Test ===
Checking system libraries:
✓ gobject-2.0-0: /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0
✓ pango-1.0-0: /usr/lib/x86_64-linux-gnu/libpango-1.0.so.0
✓ cairo: /usr/lib/x86_64-linux-gnu/libcairo.so.2
✓ gdk_pixbuf-2.0: /usr/lib/x86_64-linux-gnu/libgdk_pixbuf-2.0.so.0
=== Python Libraries ===
✓ WeasyPrint is available
=== Summary ===
✓ WeasyPrint is working - High-quality PDFs available
=== Summary ===
⚠ WeasyPrint failed but ReportLab is available - Basic PDFs available
=== Recommendations ===
1. Install system dependencies: libgdk-pixbuf2.0-0, libpango-1.0-0, libcairo2
2. Rebuild Docker container with updated Dockerfile
3. Or use ReportLab fallback (already configured)
- libgdk-pixbuf2.0-0: Image loading and manipulation
- libpango-1.0-0: Text layout and rendering
- libcairo2: 2D graphics rendering
- libpangocairo-1.0-0: Pango-Cairo integration
- libffi-dev: Foreign function interface
- shared-mime-info: MIME type detection
- fonts-liberation: Open-source font alternatives
- fonts-dejavu-core: Additional font support
- Base Image:
python:3.11-slimis minimal and may lack libraries - Architecture: ARM vs x86_64 can have different library names
- Package Names: Package names may vary between distributions
- Use Debian-based image:
python:3.11-slimis Debian-based - Install build tools: Add
build-essentialif compiling from source - Check package names: Verify package names with
apt-cache search
- Memory: Higher memory usage during generation
- Speed: Slower for complex layouts
- Quality: Professional-grade output
- Memory: Lower memory usage
- Speed: Faster generation
- Quality: Basic but functional
-
Check Current Status:
docker exec -it your-container-name python /app/docker/test-pdf-generation.py -
Verify System Libraries:
docker exec -it your-container-name ldconfig -p | grep -E "(gobject|pango|cairo)"
-
Check Package Installation:
docker exec -it your-container-name dpkg -l | grep -E "(libgdk|libpango|libcairo)"
-
Rebuild Container:
docker-compose down docker-compose build --no-cache docker-compose up
-
Test PDF Generation:
- Try generating a PDF from an invoice
- Check for fallback to ReportLab
- Verify PDF output quality
The system automatically detects WeasyPrint failures and switches to ReportLab:
try:
from app.utils.pdf_generator import InvoicePDFGenerator
# Use WeasyPrint
except Exception as e:
try:
from app.utils.pdf_generator_fallback import InvoicePDFGeneratorFallback
# Use ReportLab fallback
except Exception as fallback_error:
# Both failed- Monitor PDF generation logs
- Test both generators periodically
- Update system dependencies as needed
- After system updates
- When adding new PDF features
- If performance degrades
The PDF generation system is designed with fallbacks to ensure reliability:
- Primary: WeasyPrint for high-quality PDFs
- Fallback: ReportLab for basic PDFs
- Automatic: Seamless switching between generators
Most users will get working PDFs immediately, with the option to upgrade to high-quality output by fixing system dependencies.