Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I can't tell you exactly what causes your problem - it could be caused by buffering problems in StringIO. </p> <p>However, you are wrong if you assume that this code would actually stream the generated PDF data: StringIO.getvalue() returns the content of the string buffer at the time this method is called, not an output stream (see <a href="http://docs.python.org/library/stringio.html#StringIO.StringIO.getvalue" rel="nofollow noreferrer">http://docs.python.org/library/stringio.html#StringIO.StringIO.getvalue</a>).</p> <p>If you want to stream the output, you can treat the HttpResponse instance as a file-like object (see <a href="http://docs.djangoproject.com/en/1.2/ref/request-response/#usage" rel="nofollow noreferrer">http://docs.djangoproject.com/en/1.2/ref/request-response/#usage</a>).</p> <p>Secondly, I don't see any reason to make use of StringIO here. According to the documentation of Pisa I found (which calls this function CreatePDF, by the way) the source can be a string or a unicode object.</p> <p>Personally, I would try the following:</p> <ol> <li>Create the HTML as unicode string</li> <li>Create and configure the HttpResponse object</li> <li>Call the PDF generator with the string as input and the response as output</li> </ol> <p>In outline, this could look like this:</p> <pre><code>html = template.render(context) response = HttpResponse() response['Content-Type'] ='application/pdf' response['Content-Disposition']='attachment; filename=%s.pdf'%(filename) pisa.CreatePDF( src=html, dest=response, show_error_as_pdf=True) #response.flush() return response </code></pre> <p>However, I did <strong>not</strong> try if this actually works. (I did this sort of PDF streaming only in Java, so far.)</p> <p><strong>Update:</strong> I just looked at the implementation of HttpResponse. It implements the file interface by collecting the chunks of strings written to it in a list. Calling response.flush() is pointless, because it does nothing. Also, you can set response parameters like Content-Type even after the response has been accessed as file-object.</p> <p>Your original problem may also be related to the fact you never closed the StringIO objects. The underlying buffer of a StringIO object is not released before close() is called.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload