我尝试为数据存储区中存在的所有报告生成完整的pdf。所以我创建了一个看起来像
的处理程序class PDFPage(webapp2.RequestHandler):
def get(self, vehicle_no):
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'attachement; filename={}.pdf'.format(vehicle_no)
report = Report.get_report_by_vehicle_no(vehicle_no)
if not report:
self.response.write('Vehicle Number does not exists')
PDFHelper.generate_pdf(self.response, report)
def generate_whole_report(self, reports):
PDFHelper.generate_whole_pdf(self.response, reports)
def post(self, *args):
self.response.headers['Content-Type'] = 'application/pdf'
self.response.headers['Content-Disposition'] = 'attachement; filename=whole_report.pdf'
vehicle_nos = eval(self.request.body).get('vehicle_nos', None)
reports = []
for vehicle_no in vehicle_nos:
reports.append(Report.get_report_by_vehicle_no(vehicle_no))
self.generate_whole_report(reports)
您可以看到,通过get
方法生成的单个报告的pdf生成有效但无法通过post
方法生成所有报告的整个pdf ..我尝试从{PDFHelper.generate_pdf
调用post
{1}}方法本身,但它也失败了。所以问题主要在于请求方法没有代码..
我不知道如何在self.reponse
方法内的post
对象中编写pdf文件。
我的PDFHelper
课程看起来像,
class PDFHelper:
def __init__(self):
pass
@classmethod
def generate_pdf(cls, filename, report):
doc = SimpleDocTemplate(filename, pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30,
bottomMargin=18)
doc.title = 'Self Regulation Report'
doc.pagesize = landscape(A4)
elements = []
data = [
["Name", report.name],
["Vehicle Number", report.vehicle_no],
["Measured Temperature", str(report.measured_temperature)],
["Cleaning", convert_boolean_to_text(report.cleaning)],
["Hygienic Workflow", convert_boolean_to_text(report.hygienic_workflow)],
["Cabin Cleaning", convert_boolean_to_text(report.cabin_cleaning)],
["Problem", report.problem or ''],
["Solution", report.solution or ''],
["Created Date", str(report.date_created).split('.')[0]]
]
# TODO: Get this line right instead of just copying it from the docs
style = TableStyle([('ALIGN', (1, 1), (-2, -2), 'RIGHT'),
('TEXTCOLOR', (1, 1), (-2, -2), colors.red),
('VALIGN', (0, 0), (0, -1), 'TOP'),
('TEXTCOLOR', (0, 0), (0, -1), colors.blue),
('ALIGN', (0, -1), (-1, -1), 'CENTER'),
('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
('TEXTCOLOR', (0, -1), (-1, -1), colors.green),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
])
# Configure style and word wrap
heading_style = getSampleStyleSheet()
style_h = heading_style['Title']
p = Paragraph("Report for Vehicle Number " + report.vehicle_no, style_h)
elements.append(p)
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t = Table(data2)
t.setStyle(style)
# Send the data and build the file
elements.append(t)
doc.build(elements)
@classmethod
def generate_sample_pdf(cls, filename, reports):
doc = SimpleDocTemplate(filename, pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30,
bottomMargin=18)
doc.title = 'Self Regulation Sample Report'
doc.pagesize = landscape(A4)
doc.build([])
@classmethod
def generate_whole_pdf(cls, filename, reports):
doc = SimpleDocTemplate(filename, pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30,
bottomMargin=18)
doc.title = 'Self Regulation Whole Report'
doc.pagesize = landscape(A4)
elements = []
for report in reports:
data = [
["Name", report.name],
["Vehicle Number", report.vehicle_no],
["Measured Temperature", str(report.measured_temperature)],
["Cleaning", convert_boolean_to_text(report.cleaning)],
["Hygienic Workflow", convert_boolean_to_text(report.hygienic_workflow)],
["Cabin Cleaning", convert_boolean_to_text(report.cabin_cleaning)],
["Problem", report.problem or ''],
["Solution", report.solution or ''],
["Created Date", str(report.date_created).split('.')[0]]
]
# TODO: Get this line right instead of just copying it from the docs
style = TableStyle([('ALIGN', (1, 1), (-2, -2), 'RIGHT'),
('TEXTCOLOR', (1, 1), (-2, -2), colors.red),
('VALIGN', (0, 0), (0, -1), 'TOP'),
('TEXTCOLOR', (0, 0), (0, -1), colors.blue),
('ALIGN', (0, -1), (-1, -1), 'CENTER'),
('VALIGN', (0, -1), (-1, -1), 'MIDDLE'),
('TEXTCOLOR', (0, -1), (-1, -1), colors.green),
('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black),
('BOX', (0, 0), (-1, -1), 0.25, colors.black),
])
# Configure style and word wrap
heading_style = getSampleStyleSheet()
style_h = heading_style['Title']
p = Paragraph("Report for Vehicle Number " + report.vehicle_no, style_h)
elements.append(p)
s = getSampleStyleSheet()
s = s["BodyText"]
s.wordWrap = 'CJK'
data2 = [[Paragraph(cell, s) for cell in row] for row in data]
t = Table(data2)
t.setStyle(style)
# Send the data and build the file
elements.append(t)
doc.build(elements)
在打印创建的pdf元素时,显示
[Paragraph(
'encoding': 'utf8'
'text': u'Report for Vehicle Number dxc'
'frags': [ParaFrag(__tag__='para', bold=1, fontName='Helvetica-Bold', fontSize=18, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Report for Vehicle Number dxc', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'Title'>
'caseSensitive': 1
) #Paragraph, Table(
rowHeights=[None, None, None, None, None, None, None, None, None],
colWidths=[None, None],
[[Paragraph(
'encoding': 'utf8'
'text': u'Name'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Name', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Avinash'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Avinash', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Vehicle Number'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Vehicle Number', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'dxc'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'dxc', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Measured Temperature'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Measured Temperature', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'-25'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'-25', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Cleaning'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Cleaning', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Hygienic Workflow'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Hygienic Workflow', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Not Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Not Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Cabin Cleaning'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Cabin Cleaning', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Not Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Not Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Problem'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Problem', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': ''
'frags': []
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Solution'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Solution', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': ''
'frags': []
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Created Date'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Created Date', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'2016-08-22 05:03:35'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'2016-08-22 05:03:35', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph]]
) # end table, Paragraph(
'encoding': 'utf8'
'text': u'Report for Vehicle Number dxc'
'frags': [ParaFrag(__tag__='para', bold=1, fontName='Helvetica-Bold', fontSize=18, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Report for Vehicle Number dxc', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'Title'>
'caseSensitive': 1
) #Paragraph, Table(
rowHeights=[None, None, None, None, None, None, None, None, None],
colWidths=[None, None],
[[Paragraph(
'encoding': 'utf8'
'text': u'Name'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Name', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Avinash'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Avinash', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Vehicle Number'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Vehicle Number', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'dxc'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'dxc', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Measured Temperature'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Measured Temperature', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'-25'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'-25', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Cleaning'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Cleaning', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Hygienic Workflow'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Hygienic Workflow', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Not Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Not Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Cabin Cleaning'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Cabin Cleaning', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'Not Ok'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Not Ok', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Problem'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Problem', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': ''
'frags': []
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Solution'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Solution', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': ''
'frags': []
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph],
[Paragraph(
'encoding': 'utf8'
'text': u'Created Date'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'Created Date', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph,
Paragraph(
'encoding': 'utf8'
'text': u'2016-08-22 05:03:35'
'frags': [ParaFrag(__tag__='para', bold=0, fontName='Helvetica', fontSize=10, greek=0, italic=0, link=None, rise=0, strike=0, sub=0, sup=0, text=u'2016-08-22 05:03:35', textColor=Color(0,0,0,1), underline=0)]
'bulletText': None
'debug': 0
'style': <ParagraphStyle 'BodyText'>
'caseSensitive': 1
) #Paragraph]]
) # end table]