Answer the question
In order to leave comments, you need to log in
How to simplify code when generating PDF?
Good time of the day!
view.py:
......
/*Генерация pdf и сохранения его на сервере*/
result_pdf = open("%s%s.pdf" % (settings.MEDIA_ROOT, doc.title), 'wb')
pdf = pisa.CreatePDF(StringIO.StringIO(html.encode("UTF-8")), result_pdf, encoding='UTF-8')
result_pdf.close()
/*Генерация pdf для выдачи в response*/
result_response = StringIO.StringIO()
pdf_for_response = pisa.CreatePDF(StringIO.StringIO(html.encode("UTF-8")), result_response, encoding='UTF-8')
response['Content-Disposition'] = 'filename=test.pdf'
return response
....
Answer the question
In order to leave comments, you need to log in
learn python!!
generalize. define a function:
def create_pdf(target):
return pisa.CreatePDF(StringIO.StringIO(html.encode("UTF-8")), target, encoding='UTF-8')
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question