Optimize PDF conversion in Django / Python -
I have a webpad that exports reports to PDF Everything is fine when the query returns less than 100 values when the records increase compared to 100, then the server increases the proxy error of 502. The report outputs exactly in HTML, the process of hanging the server is a conversion from HTML to PDF. I am using to generate pdf. Algorithm is something like this:
def view1 (request, ** someargs): queryset = someModel.objects.get (someargs) if request.GET ['PDF']: return pdfWrapper ('Template .html ', queries,' filenames') Other: return render_to_response ('template.html', queryset) def pdfWrapper (template_src, context_dict, filename): ############### ################################## There is an old version of the code given below ## recived comment # function now Also works for small HTML documents # 502 for large forese ################################# ####### Production #################### To Import StringI Import through import. Import from dijongo.template.loader as Pisa, import_template import from django.template import import from the demo. Http import HTTPPRPS ## escape from CGI import template = get_template (template_src) reference = context (reference_details) html = template.rendeR (reference) feedback = htpps () feedback ['content type'] = 'application / pdf response [ 'Content-dispute'] = 'attachment; File name =% s.pdf '% (filename) pisa.CreatePDF (src = html, dest = response, show_error_as_pdf = True) Return response ## Result = StringIO.StringIO () ## pdf = pisa.pisaDocument (## StringIO StringIO (html.encode ("ISO-8859-1"), ## results) ## if not pdf.err: ## response = HttpResponse (## result .getvalue (), ## mimetype = 'application / pdf ') ## Reply [' content-dispute '] =' affirmation; File name =% s.pdf '% (filename) ## Return response ## Return HTTPPREPSpacks (' hooo an error & lt; pre & gt;% s & lt; / pre & gt; '% escape (html)) < / Code>
I have thought about making a buffer so that the server can free up some memory, but I have not got anything yet. Can anyone help? Please?
I can not tell you that the cause of your problem - this is to buffer problems in stringio may be due to .
However, if you believe that this code will actually stream the generated PDF data then you are wrong: StringIO.getvalue () The content of the string buffer is called this method, not the output stream (See).
If you want to stream the output, you can see the HttpResponse instance as an object like the file (see).
Secondly, no one is visible to me. The reason for using stringio is here as per Pisa's documentation that I found (the way this function is called CreatePDF) can be source string or unicode object.
Individually, I will try the following:
- Create HTML as Unicode string
- Prepare and configure HTTPSpad objects
- Call the PDF generator with the string as input and the feedback as output
In the outline, it can look like this:
html = template.render (reference) response = HttpResponse () response ['content-type'] = 'application / PDF' Ticket ['content-dispute'] = 'attachment; File name =% s.pdf '% (filename) pisa.CreatePDF (src = html, dest = response, show_error_as_pdf = true) # response.flush () return response
However, Do not not try that it really works. (I have done so in this kind of PDF Streaming Java.)
Update: I looked into the implementation of HTPRPS. It implements the file interface by collecting pieces of stars written in a list. Calling answer.flas () is useless, because it does nothing. In addition, you can set content type such as content type, such as the response to the file object.
Your original problem may also be related to the fact that you have never closed stringio objects. The built-in buffer of string object object is not released before being called ().
Comments
Post a Comment