4
votes

I'm trying to create a pdf with this library implementing the most simple example of de docs

using the url and render directly from the template like this

url(r'^pdf/$', PDFTemplateView.as_view(template_name='my_template.html',
                                       filename='my_pdf.pdf'), name='pdf'),

this is the html of the template file

<!DOCTYPE html>
<html>
 <head>
    <title>Hello Wold</title>
 </head>
    <body>
       <p>My First Pdf</p>
     </body>
</html>

but when i try the url the error is this

AttributeError at /pdf/ 'NoneType' object has no attribute 'endswith'

how can i fix it

this is the Traceback

Traceback: File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py" in get_response 139. response = response.render() File "/usr/local/lib/python2.7/dist-packages/django/template/response.py" in render 105. self.content = self.rendered_content File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/views.py" in rendered_content 123. delete=(not debug) File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/views.py" in render_to_temporary_file 78. content = make_absolute_paths(content) File "/usr/local/lib/python2.7/dist-packages/wkhtmltopdf/utils.py" in make_absolute_paths 151. if not x['root'].endswith('/'):

Exception Type: AttributeError at /pdf/ Exception Value: 'NoneType' object has no attribute 'endswith'

Environment:

Request Method: GET

Django Version: 1.6.2
Python Version: 2.7.3
Installed Applications:
    ('django.contrib.admin',
     'django.contrib.auth',
     'django.contrib.contenttypes',
     'django.contrib.sessions',
     'django.contrib.messages',
     'django.contrib.staticfiles',
     'south',
     'rest_framework',
     'cian',
     'django_extensions',
     'wkhtmltopdf')
Installed Middleware:
    ('django.contrib.sessions.middleware.SessionMiddleware',
     'django.middleware.common.CommonMiddleware',
     'django.middleware.csrf.CsrfViewMiddleware',
     'django.contrib.auth.middleware.AuthenticationMiddleware',
     'django.contrib.messages.middleware.MessageMiddleware',
     'django.middleware.clickjacking.XFrameOptionsMiddleware')

this is the screenshot with the django-wkhtmltopd error

enter image description here

3

3 Answers

1
votes

Looks like you have a problem with your root path. Try settings STATIC_URL in Django setting file (settings.py)

5
votes

This error occurs because wkhtmltopdf uses the path that is in STATIC_ROOT of settings.py to generate the pdf file. Try assigning a value to STATIC_ROOT and test.

STATIC_ROOT = 'path.to.your.static.directory'
0
votes

The error was that i haven't define my media_root and media_url in settings.py the wrapper expects that you have static_root, media_root, static_url and media_url to work