<filename>apps/utils/reports/init_config_xhtml2pdf.py
"""Init config xhtml2pdf.

   To allow URL references to be resolved using Django’s STATIC_URL and MEDIA_URL settings, 
   xhtml2pdf allows users to specify a link_callback paramter to point to a function that 
   converts relative URLs to absolute system paths.
"""

# Python
import os

# Django
from django.conf import settings


def link_callback(uri, rel):
    """
    Convert HTML URIs to absolute system paths so xhtml2pdf can access those
    resources
    """
    # use short variable names
    sUrl = settings.STATIC_URL      # Typically /static/
    sRoot = settings.STATIC_ROOT    # Typically /home/userX/project_static/
    mUrl = settings.MEDIA_URL       # Typically /static/media/
    mRoot = settings.MEDIA_ROOT     # Typically /home/userX/project_static/media/

    # convert URIs to absolute system paths
    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)

    # make sure that file exists
    if not os.path.isfile(path):
            raise Exception(
                'media URI must start with %s or %s' % (sUrl, mUrl)
            )
    return path

<filename>apps/utils/reports/init_config_xhtml2pdf.py
"""Init config xhtml2pdf.

   To allow URL references to be resolved using Django’s STATIC_URL and MEDIA_URL settings, 
   xhtml2pdf allows users to specify a link_callback paramter to point to a function that 
   converts relative URLs to absolute system paths.
"""

# Python
import os

# Django
from django.conf import settings


def link_callback(uri, rel):
    """
    Convert HTML URIs to absolute system paths so xhtml2pdf can access those
    resources
    """
    # use short variable names
    sUrl = settings.STATIC_URL      # Typically /static/
    sRoot = settings.STATIC_ROOT    # Typically /home/userX/project_static/
    mUrl = settings.MEDIA_URL       # Typically /static/media/
    mRoot = settings.MEDIA_ROOT     # Typically /home/userX/project_static/media/

    # convert URIs to absolute system paths
    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)

    # make sure that file exists
    if not os.path.isfile(path):
            raise Exception(
                'media URI must start with %s or %s' % (sUrl, mUrl)
            )
    return path

Init config xhtml2pdf.     To allow URL references to be resolved using Django’s STATIC_URL and MEDIA_URL settings,     xhtml2pdf allows users to specify a link_callback paramter to point to a function that     converts relative URLs to absolute system paths. # Python # Django Convert HTML URIs to absolute system paths so xhtml2pdf can access those     resources # use short variable names # Typically /static/ # Typically /home/userX/project_static/ # Typically /static/media/ # Typically /home/userX/project_static/media/ # convert URIs to absolute system paths # handle absolute uri (ie: http://some.tld/foo.png) # make sure that file exists