Forum Bugs

django pdf from response

serrrgggeee
Hello I tried to generate pdf from my response like here
def index(request):
p = Popen(["prince","","out.pdf"], stdin=PIPE, stdout=PIPE)
template = loader.get_template('pdf/pdf.html')
response = HttpResponse(template.render())
p.stdin.write(response)
p.stdin.close()
pdf = p.stdout.read()
print pdf
print("PDF is "+str(len(pdf))+" bytes in size")
print response
return HttpResponse(template.render())
but I get this message "TypeError: must be convertible to a buffer, not HttpResponse
" what's it meaning? and how it solwed?
mikeday
I assume the error is coming from p.stdin.write(response), what does template.render() return? Why are you wrapping it in HttpResponse?
mikeday
Also are you missing a command-line argument to Prince: ["prince","","out.pdf"]

Presumably the empty argument should be "-" to make Prince read from standard input. Also you may want to pass --structured-log=quiet to stop Prince writing error and warning messages to stderr and potentially blocking.
serrrgggeee
ok I did make, what you say
def index(request):
p = Popen(["prince","-","out.pdf"], stdin=PIPE, stdout=PIPE)
template = loader.get_template('pdf/pdf.html')
response = HttpResponse(template.render())
p.stdin.write(response)
p.stdin.close()
pdf = p.stdout.read()
print pdf
print("PDF is "+str(len(pdf))+" bytes in size")
print response
return HttpResponse(template.render())
but I get "must be convertible to a buffer, not HttpResponse"
and I use response because I have a dynamic page, and I'm sory for my english but I'm russia)))
mikeday
What if instead of this:
response = HttpResponse(template.render())
p.stdin.write(response)

You do this:
p.stdin.write(template.render())
serrrgggeee
ok thanks it works but I anyway have a error
'ascii' codec can't encode characters in position 131-135: ordinal not in range(128)
it's because russian symbol, I add in header skript
#-*- coding: utf-8 -*-
from __future__ import unicode_literals
but it does't halp me.
mikeday
okay sorry you will need to ask this question on the Django forum, I don't know how to fix this. :(
serrrgggeee
Ok anyway thank you))
dauwhe
Not sure if this will help, but here's a snippet I've used:

def prince(template_src, context_dict):
    template = get_template(template_src)
    context = Context(context_dict)
    html  = template.render(context)
    server_ip = 'http://' + str(context_dict['request'])
    log_folder = settings.LOG_PATH + '/log_file.log'
    p = Popen(["prince","--baseurl",server_ip,"--verbose","--log",log_folder,"-"], stdin=PIPE, stdout=PIPE)
    p.stdin.write(html.encode('UTF-8')) 
    p.stdin.close()
    pdf = p.stdout.read()     
    response = HttpResponse(pdf, content_type='application/pdf')
    # try to get book title in file name
    response['Content-Disposition'] = 'filename=' + str(context_dict['book']) + '.pdf'
    return response