diff options
author | mlen108 <matt.lenc@gmail.com> | 2012-11-14 16:32:18 +0000 |
---|---|---|
committer | mlen108 <matt.lenc@gmail.com> | 2012-11-14 16:32:18 +0000 |
commit | 2cc80138a7167c5a6adbb75d1b58fd56872077fd (patch) | |
tree | 64cf0557ae4e1c462eb0bc5176270ac85d46d185 /wkhtmltopdf | |
parent | 7399977bbc2d72a8b3a3aed2c2fedb7ab8451af3 (diff) | |
download | django-wkhtmltopdf-2cc80138a7167c5a6adbb75d1b58fd56872077fd.tar.gz django-wkhtmltopdf-2cc80138a7167c5a6adbb75d1b58fd56872077fd.tar.bz2 django-wkhtmltopdf-2cc80138a7167c5a6adbb75d1b58fd56872077fd.zip |
Fix paths for STATIC media files
Diffstat (limited to 'wkhtmltopdf')
-rw-r--r-- | wkhtmltopdf/views.py | 37 |
1 files changed, 27 insertions, 10 deletions
diff --git a/wkhtmltopdf/views.py b/wkhtmltopdf/views.py index ac406a3..9ff5395 100644 --- a/wkhtmltopdf/views.py +++ b/wkhtmltopdf/views.py @@ -71,16 +71,7 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): context = self.resolve_context(self.context_data) content = smart_str(template.render(context)) - # mattl: - # convert all MEDIA_ROOT files into a file://URL paths - # in order to correctly get it displayed in PDFs - # disclaimer: I know it sucks, but I haz no time for better solution now - media_root = settings.MEDIA_ROOT - if not media_root.endswith('/'): - media_root += '/' - relative_path = settings.MEDIA_URL - for x in re.findall('''["|']({0}.*?)["|']'''.format(relative_path), content): - content = content.replace(x, pathname2fileurl(media_root) + x[len(relative_path):]) + content = self.make_absolute_paths(content) tempfile = NamedTemporaryFile(mode=mode, bufsize=bufsize, suffix=suffix, prefix=prefix, @@ -152,6 +143,32 @@ class PDFTemplateResponse(TemplateResponse, PDFResponse): for f in filter(None, (input_file, header_file, footer_file)): f.close() + def make_absolute_paths(self, content): + """Convert all MEDIA files into a file://URL paths in order to correctly get it displayed in PDFs + + mattl's disclaimer: I know it sucks, but it works and I haz no time for better solution now + """ + + overrides = [ + { + 'root': settings.MEDIA_ROOT, + 'url': settings.MEDIA_URL, + }, + { + 'root': settings.STATIC_ROOT, + 'url': settings.STATIC_URL, + } + ] + has_scheme = re.compile(r'^[^:/]+://') + + for x in overrides: + if not has_scheme.match(x['url']): + if not x['root'].endswith('/'): + x['root'] += '/' + for occur in re.findall('''["|']({0}.*?)["|']'''.format(x['url']), content): + content = content.replace(occur, pathname2fileurl(x['root']) + occur[len(x['url']):]) + return content + class PDFTemplateView(TemplateView): """Class-based view for HTML templates rendered to PDF.""" |