Related to: django - pisa: adding images to a PDF file
I have a website that uses the Google chart API to display a bunch of reports for the user, and I'm trying to implement a PDF version. I use the link_callbackin option pisa.pisaDocument, which is great for local media (css / images), but I wonder if it will work with remote images (using Google Maps URLs).
From the documentation on the pisa website, they imply that this is possible, but they do not show how:
Normal pisa expects these files to be found on the local disk. They can also reference the source document. But the programmer may want to download various sources, such as the Internet through HTTP requests or from a database or everything else.
This is in a Django project, but it is rather inappropriate. Here is what I use for rendering:
html = render_to_string('reporting/pdf.html', keys,
context_instance=RequestContext(request))
result = StringIO.StringIO()
pdf = pisa.pisaDocument(
StringIO.StringIO(html.encode('ascii', 'xmlcharrefreplace')),
result, link_callback=link_callback)
return HttpResponse(result.getvalue(), mimetype='application/pdf')
I tried link_callback to return a urllib request object, but it does not work:
def link_callback(uri, rel):
if uri.find('chxt') != -1:
url = "%s?%s" % (settings.GOOGLE_CHART_URL, uri)
return urllib2.urlopen(url)
return os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
The created PDF file is excellent, except that the images in the Google graphics do not exist.