5

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?

JEEND0
  • 482
  • 4
  • 15
  • 3
    Don't use Pisa, use xhtml2pdf which is the continuation of the project. – Chris Morgan Sep 03 '11 at 05:56
  • 2
    I also faced the same problem.Mkstemp was of no help either. Pisa doesnt render images when we have set the images dimensions in html.So try generating pdf from html with only image.Then go on adding other html contents. I used HTMLTODOC for generating pdf in the end ->http://www.htmldoc.org/ – Abhaya Dec 09 '11 at 03:59

2 Answers2

3

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)
Cole Maclean
  • 5,627
  • 25
  • 37
0

I think this might help. Windows use of slashes was the culprit in my case. https://stackoverflow.com/a/12644633/1236599

Community
  • 1
  • 1
Krazzzeee
  • 76
  • 4