PDF로 보고서를 내보내는 webapp가 있습니다. 쿼리가 100 개 미만의 값을 반환하면 모든 것이 잘됩니다. 레코드 수가 100을 초과하면 서버는 502 프록시 오류를 발생시킵니다. 이 보고서는 HTML 형식으로 출력됩니다. 서버를 끊는 프로세스는 html에서 PDF 로의 변환입니다. xhtml2pdf (AKA pisa 3.0)을 사용하여 PDF를 생성하고 있습니다. 나는 서버가 일부 메모리를 확보 할 수 있습니다하지만 난 아직 아무것도 찾지 못했습니다 그래서 버퍼를 만드는 방법에 대한 몇 가지 생각을 넣었습니다Django/Python에서 PDF 변환 최적화
def view1(request, **someargs):
queryset = someModel.objects.get(someargs)
if request.GET['pdf']:
return pdfWrapper('template.html',queryset,'filename')
else:
return render_to_response('template.html',queryset)
def pdfWrapper(template_src, context_dict, filename):
################################################
#
# The code comented below is an older version
# I updated the code according the comment recived
# The function still works for short HTML documents
# and produce the 502 for larger onese
#
################################################
##import cStringIO as StringIO
import ho.pisa as pisa
from django.template.loader import get_template
from django.template import Context
from django.http import HttpResponse
##from cgi import escape
template = get_template(template_src)
context = Context(context_dict)
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)
return response
##result = StringIO.StringIO()
##pdf = pisa.pisaDocument(
## StringIO.StringIO(html.encode("ISO-8859-1")),
## result)
##if not pdf.err:
## response = HttpResponse(
## result.getvalue(),
## mimetype='application/pdf')
## response['Content-Disposition']='attachement; filename=%s.pdf'%(filename)
## return response
##return HttpResponse('Hubo un error<pre>%s</pre>' % escape(html))
다음 algorythm이 같은 것입니다. 누구든지 도움을 줄 수 있습니까? 부디?
@ bernd-petersohn은 아래에 지적했듯이 cStringIO와 StringIO는 실제로 인터넷을 통해 콘텐츠를 스트리밍하지 않고 메모리에 상주하는 "파일과 유사한 객체"를 만들어 디스크에 꼭 필요하지는 않습니다. – marcoslhc