To start, I've seen the other threads on this, and I have tried nearly everything to try to fix this but...
When using Pisa to render an HTML page to PDF, the images in said HTML go conspicuously missing. That is to say, when the page is rendered to HTML, everything is just dandy, but when I switch the output to PDF using Pisa, the images disappear.
The most common thing I've found to do is to create a link callback function thus:
def fetch_resources(uri, rel):
path = os.path.join(settings.MEDIA_ROOT, uri.replace(settings.MEDIA_URL, ""))
return path
def my_view(request, variable1):
html = render_to_string('template_goes_here.html', dict, context_instance=RequestContext(request))
result = StringIO()
pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")), dest=result, link_callback=fetch_resources)
if not pdf.err:
return HttpResponse(result.getvalue(), mimetype='application/pdf')
return HttpResponse('Pisa hates you! %s' % cgi.escape(html))
However, this does not work. The PDF comes up, great, but the images are absent.
I also read a suggestion on another thread about writing the HTML to mkstemp, converting it to a PDF via command line, and then outputing the HTML, no success there either.
I even tried installing PIL 1.1.16 instead of 1.1.17 because of someone with a similar issue--no dice.
Does anyone have an idea on where I'm going wrong here?
It's been a while since I looked at this, but I think you have to use lambda or functools.
e.g.
links = lambda uri, rel: os.path.join(settings.MEDIA_ROOT,
uri.replace(settings.MEDIA_URL, ''))
pdf = pisa.pisaDocument(StringIO(html.encode("UTF-8")),
dest=result, link_callback=links)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With