xhtml2pdf ImportError - Django
Asked Answered
A

1

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?

Ablepsia answered 27/2, 2014 at 17:11 Comment(2)
are you may be using an older version within a virtualenv ?Aeroembolism
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.Ablepsia
A
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)
Ablepsia answered 27/2, 2014 at 18:23 Comment(9)
The file should be found in location similar to this /home/<YOUR_USER_NAME>/.virtualenvs/<YOUR_VIRTUAL_ENV_NAME>/lib/python2.7/site-packages/sx/pisa3/pisa_util.py when using virtualenv on Linux.Reviviscence
@Ablepsia 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?Hillery
@Hillery The fix was merged to master over a year ago. Should be working now.Ablepsia
@Ablepsia Which dependencies and versions should I install from pip for ? Maybe you know some link where I can look for this all together?Hillery
@Hillery what version do you have installed?Ablepsia
@Ablepsia xhtml2pdf==0.0.6Hillery
@Hillery you can either wait until a new version comes out or use the latest version on githubAblepsia
not really a solution but does as a workaround. why is pisa repositoy not fixing this after more than a year?Rightism
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.Wollastonite

© 2022 - 2024 — McMap. All rights reserved.