Include django finder for static files
@@ -53,33 +53,42 @@ that converts relative URLs to absolute system paths.
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
+ from django.contrib.staticfiles import finders
+
def link_callback(uri, rel):
"""
Convert HTML URIs to absolute system paths so xhtml2pdf can access those
resources
"""
- # use short variable names
- s_url = settings.STATIC_URL # Typically /static/
- s_root = settings.STATIC_ROOT # Typically /home/userX/project_static/
- m_url = settings.MEDIA_URL # Typically /static/media/
- m_root = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
-
- # convert URIs to absolute system paths
- if uri.startswith(m_url):
- path = os.path.join(m_root, uri.replace(m_url, ""))
- elif uri.startswith(s_url):
- path = os.path.join(s_root, uri.replace(s_url, ""))
+ result = finders.find(uri)
+ if result:
+ if not isinstance(result, (list, tuple)):
+ result = [result]
+ result = list(os.path.realpath(path) for path in result)
+ path=result[0]
+ else:
+ sUrl = settings.STATIC_URL # Typically /static/
+ sRoot = settings.STATIC_ROOT # Typically /home/userX/project_static/
+ mUrl = settings.MEDIA_URL # Typically /media/
+ mRoot = settings.MEDIA_ROOT # Typically /home/userX/project_static/media/
+
+ if uri.startswith(mUrl):
+ path = os.path.join(mRoot, uri.replace(mUrl, ""))
+ elif uri.startswith(sUrl):
+ path = os.path.join(sRoot, uri.replace(sUrl, ""))
else:
- return uri # handle absolute uri (ie: http://some.tld/foo.png)
+ return uri
# make sure that file exists
if not os.path.isfile(path):
raise Exception(
- 'media URI must start with %s or %s' % (s_url, m_url)
+ 'media URI must start with %s or %s' % (sUrl, mUrl)
)
return path
+
+
.. code:: python
def render_pdf_view(request):
