Question

I use wkhtmltopdf with django-wkhtmltopdf and I think I have incorrectly serving static files. If I run wkhtmltopdf from the console are properly collected static files and generates a good pdf file:

wkhtmltopdf http://127.0.0.1:8000/dash/test/ test.pdf

"GET /static/base/js/jquery.js HTTP/1.1" 200 93106

"GET /static/base/css/bootstrap.css HTTP/1.1" 200 119892

"GET /static/base/js/bootstrap.min.js HTTP/1.1" 200 27726

"GET /static/dash/css/flot.css HTTP/1.1" 200 1810

"GET /static/dash/js/jquery.flot.categories.js HTTP/1.1" 200 6033

"GET /static/dash/js/jquery.flot.js HTTP/1.1" 200 119052

However, if the generated PDF file from your application using django-wkhtmltopdf I have only:

"GET /dash/test/ HTTP/1.1" 200 13246004

And the generated PDF file looks like this:

enter image description here

Static files in settings.py I have set up as follows:

MEDIA_ROOT = ''

MEDIA_URL = ''

STATIC_ROOT = '/Users/malt/Django/env/app/static/'

STATIC_URL = '/static/'

STATICFILES_DIRS = ( )

STATICFILES_FINDERS = ( )

What else should I check?

Was it helpful?

Solution

For static files, and for all settings I use something like this (in settings.py):

# settings.py
import os

BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))


def ABS_DIR(rel):
    return os.path.join(BASE_DIR, rel.replace('/', os.path.sep))

MEDIA_ROOT = ABS_DIR('project_name/site_media/')
MEDIA_URL = '/site_media/'
STATIC_ROOT = ABS_DIR('project_name/static/')
STATIC_URL = '/static/'

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
STATICFILES_DIRS = (
    ABS_DIR('project_static'),
)

and in urls.py (a part of it):

# urls.py
from django.conf import settings
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.conf.urls.static import static

# for dev static files serving
if settings.DEBUG:
    urlpatterns += staticfiles_urlpatterns()
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

It's tested and working. Maybe you have a problem with paths in PDF class?

OTHER TIPS

I had a similar issue, and the fix for me was using the full url as my STATIC_URL.

So, instead of:

STATIC_URL = '/static/'

I had to use:

STATIC_URL = 'http://localhost:8000/static/'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top