11

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?

hanleyhansen
  • 6,304
  • 8
  • 37
  • 73
  • are you may be using an older version within a virtualenv ? – karthikr Feb 27 '14 at 17:16
  • Nope. I installed it within my virtualenv. Also the console output above is from my virtualenv. I only have one virtualenv for my Django projects with the latest version of `reportlab` installed. – hanleyhansen Feb 27 '14 at 17:17

1 Answers1

18

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")

EDIT

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)
hanleyhansen
  • 6,304
  • 8
  • 37
  • 73
  • 3
    The file should be found in location similar to this `/home//.virtualenvs//lib/python2.7/site-packages/sx/pisa3/pisa_util.py` when using virtualenv on Linux. – andilabs Oct 29 '14 at 16:00
  • @hanleyhansen What about when coming to deploy in Heroku or in a production environment? Should we do the same in the server? Haven't they made a fix or a newer version with this fixed? – madtyn Mar 03 '15 at 22:58
  • @madtyn The fix was merged to master over a year ago. Should be working now. – hanleyhansen Mar 03 '15 at 23:03
  • @hanleyhansen Which dependencies and versions should I install from pip for ? Maybe you know some link where I can look for this all together? – madtyn Mar 03 '15 at 23:06
  • @madtyn what version do you have installed? – hanleyhansen Mar 03 '15 at 23:08
  • @hanleyhansen xhtml2pdf==0.0.6 – madtyn Mar 03 '15 at 23:26
  • 1
    @madtyn you can either wait until a new version comes out or use the latest version on github – hanleyhansen Mar 04 '15 at 01:24
  • not really a solution but does as a workaround. why is pisa repositoy not fixing this after more than a year? – mschmoock Sep 09 '15 at 13:57
  • These path will be much helpful `$VIRTUAL_ENV/lib/python2.7/site-packages/sx/pisa3/pisa_util.py`, `$VIRTUAL_ENV/lib/python3.5/site-packages/sx/pisa3/pisa_util.py`. In case, your python version is not same just change it. – Ajeeb.K.P May 20 '16 at 04:46