当用户提交表单时,我使用 reportlab 生成 pdf。现在,当我将生成 pdf 的代码放在 get 中时进行测试时 - 我得到了预期的正确 pdf。但是当我将生成 PDF 的代码放在 post 函数中时,问题就出现了,没有生成 pdf。我已经尝试过调试,并且遍历了 pdf 生成代码的所有部分。问题是什么?
任何帮助表示赞赏。
这是代码的一部分:
处理表单数据的 PDF 处理函数:
class PDFHandler(BlogHandler):
def get(self):
self.render("create_offer_letter.html")
def post(self):
logging.info('Inside Post')
candidate_name = self.request.get('candidate_name')
candidate_email = self.request.get('candidate_email')
candidate_address_street = self.request.get('candidate_address_street')
candidate_address_city = self.request.get('candidate_address_city')
candidate_address_state = self.request.get('candidate_address_state')
candidate_address_country = self.request.get('candidate_address_country')
candidate_address_pin = self.request.get('candidate_address_pin')
candidate_phone = self.request.get('candidate_phone')
course = self.request.get('course')
institute = self.request.get('institute')
admissionfee = int(self.request.get('admissionfee'))
coursefee = int(self.request.get('coursefee'))
feeterm = self.request.get('feeterm')
confirmation_date = self.request.get('field4')
candidate_address_part2 = candidate_address_city + candidate_address_state +candidate_address_pin
candidate_address = candidate_address_street + candidate_address_part2 + candidate_address_country
address_part= [candidate_address_street, candidate_address_part2, candidate_address_country]
generatePdf(self,candidate_name,candidate_email,candidate_phone,address_part,confirmation_date,coursefee,admissionfee,institute,course)
logging.info('End of Post')
这是实际 pdf 创建发生的 pdf 生成函数:
def generatePdf(self, candidate_name,candidate_email,candidate_phone,address_part,confirmation_date,coursefee,admissionfee,institute,course):
logging.info('Inside PDF Creation')
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'attachment; filename=my.pdf'
c = canvas.Canvas(self.response.out, pagesize=A4)
buff2 = StringIO.StringIO()
# image_data is a raw string containing a JPEG
# Draw it in the bottom left, 2 inches high and 2 inches wide
doc = BaseDocTemplate(buff2,pagesize=A4,
rightMargin=12,leftMargin=12,
topMargin=12,bottomMargin=12)
Story=[]
magName = "Pythonista"
issueNum = 12
subPrice = "99.00"
limitedDate = "03/05/2010"
freeGift = "tin foil hat"
#time.timezone('IST')
formatted_time = time.ctime()
full_name = "Mike Driscoll"
candidate_name="Siddhant"
#im = Image(buff, 8*inch, 11*inch)
address_part =['hello', 'bye']
#Story.append(im)
styles=getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
ptext = '<font size=12>%s</font>' % formatted_time
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
# Create return address
#ptext = '<font size=12>%s</font>' % candidate_name
#Story.append(Paragraph(ptext, styles["Normal"]))
#for part in address_part:
# ptext = '<font size=12>%s</font>' % part.strip()
# Story.append(Paragraph(ptext, styles["Normal"]))
#Story.append(Spacer(1, 12))
#ptext = '<font size=12>Dear %s:</font>' % candidate_name.split()[0].strip()
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
ptext = '<font size=12>We would like to welcome you to our subscriber base for %s Magazine! \
You will receive %s issues at the excellent introductory price of $%s. Please respond by\
%s to start receiving your subscription and get the following free gift: %s.</font>' % (magName,
issueNum,
subPrice,
limitedDate,
freeGift)
Story.append(Paragraph(ptext, styles["Justify"]))
Story.append(Spacer(1, 12))
logging.info('Generating PDF')
ptext = '<font size=12>Thank you very much and we look forward to serving you.</font>'
Story.append(Paragraph(ptext, styles["Justify"]))
Story.append(Spacer(1, 12))
ptext = '<font size=12>Sincerely,</font>'
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 48))
ptext = '<font size=12>Ima Sucker</font>'
Story.append(Paragraph(ptext, styles["Normal"]))
Story.append(Spacer(1, 12))
frameT = Frame(2*inch, 0*inch, 6*inch, 10*inch, id='normal',showBoundary=0)
doc.addPageTemplates([PageTemplate(id='first',frames=frameT,onPage=myFirstPage)])
doc.build(Story)
#c.drawString(100, 100, "Hello world")
#text = c.beginText()
#text.setTextOrigin(100, 500)
#text.setFont("Times Roman", 14)
#text.textLine("Hello world!")
#text.textLine("Look ma, multiple lines!")
#c.drawText(text)
#response = HttpResponse(mimetype='application/pdf')
#response['Content-Disposition'] = 'attachment; filename=output.pdf'
#f = Frame(inch, inch, 6*inch, 9*inch, showBoundary=1)
#f.addFromList(Story,c)
#c.showPage()
#c.save()
#self.response.clear()
self.response.write(buff2.getvalue())
buff2.close()
logging.info('Exiting PDF Generation')