I installed xhtml2pdf
using pip
for use with Django. I am getting the following ImportError:
Reportlab Toolkit Version 2.2 or higher needed
But I have reportlab 3.0
>>> import reportlab
>>> print reportlab.Version
3.0
I found this try catch block in the __init__.py
of xhtml2pdf
:
REQUIRED_INFO = """
****************************************************
IMPORT ERROR!
%s
****************************************************
The following Python packages are required for PISA:
- Reportlab Toolkit >= 2.2 <http://www.reportlab.org/>
- HTML5lib >= 0.11.1 <http://code.google.com/p/html5lib/>
Optional packages:
- pyPDF <http://pybrary.net/pyPdf/>
- PIL <http://www.pythonware.com/products/pil/>
""".lstrip()
log = logging.getLogger(__name__)
try:
from xhtml2pdf.util import REPORTLAB22
if not REPORTLAB22:
raise ImportError, "Reportlab Toolkit Version 2.2 or higher needed"
except ImportError, e:
import sys
sys.stderr.write(REQUIRED_INFO % e)
log.error(REQUIRED_INFO % e)
raise
There's also another error in the util.py
:
if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
Shouldn't that read something like:
if not (reportlab.Version[:3] >="2.1"):
What gives?
In util.py
edit the following lines:
if not (reportlab.Version[0] == "2" and reportlab.Version[2] >= "1"):
raise ImportError("Reportlab Version 2.1+ is needed!")
REPORTLAB22 = (reportlab.Version[0] == "2" and reportlab.Version[2] >= "2")
And set to:
if not (reportlab.Version[:3] >="2.1"):
raise ImportError("Reportlab Version 2.1+ is needed!")
REPORTLAB22 = (reportlab.Version[:3] >="2.1")
While the above works it still uses string literals for version checking. There's a pull request in the xhtml2pdf
project with a more elegant solution that compares versions using tuples of integers. This is the proposed solution:
_reportlab_version = tuple(map(int, reportlab.Version.split('.')))
if _reportlab_version < (2,1):
raise ImportError("Reportlab Version 2.1+ is needed!")
REPORTLAB22 = _reportlab_version >= (2, 2)