169 lines
6.1 KiB
Python
169 lines
6.1 KiB
Python
from hashlib import sha1
|
||
|
||
from reportlab.lib.colors import HexColor
|
||
from reportlab.lib.units import cm, inch
|
||
from reportlab.platypus import Paragraph, Image, PageBreak, Frame, Table
|
||
from reportlab.platypus.doctemplate import PageTemplate, BaseDocTemplate
|
||
from reportlab.platypus.tableofcontents import TableOfContents
|
||
from reportlab.pdfgen import canvas
|
||
|
||
from Report.PdfStyle import cover_space, cover_company_style, cover_report_style, cover_time_style, chapter_style, \
|
||
section_style, adjust_line_width, para_style_single, para_style_bold, table_name, table_unit, table_mark, \
|
||
adjust_table_widths, adjust_table_style, Seashell4, toc_style_1, table_content_style, toc_style_2, darkGolden
|
||
from Report.path_tool import gen_pdf_path, get_pic_path
|
||
|
||
# 页码起始设置
|
||
offset = 3
|
||
|
||
|
||
class HeaderAndFooterCanvas(canvas.Canvas):
|
||
def __init__(self, *args, **kwargs):
|
||
canvas.Canvas.__init__(self, *args, **kwargs)
|
||
self._saved_page_states = []
|
||
|
||
def showPage(self):
|
||
self._saved_page_states.append(dict(self.__dict__))
|
||
self._startPage()
|
||
|
||
def draw_header_and_footer(self, page_count):
|
||
|
||
# 把封面加上
|
||
# if self._pageNumber == 1:
|
||
# self.drawImage(get_pic_path(picName='cover.png'), 0, 0, 596, 842)
|
||
|
||
self.setFont("Helvetica", 7)
|
||
if self._pageNumber > offset:
|
||
self.setFont('pingbold', 8)
|
||
self.setFillColor(HexColor(Seashell4))
|
||
self.drawString(74, 805, "远东资信评估有限公司")
|
||
self.drawString(74, 40, "%d / %d" % (self._pageNumber - offset, page_count - offset))
|
||
|
||
self.setStrokeColor(darkGolden)
|
||
self.setLineWidth(2.5)
|
||
self.line(74, 820, 6.8 * inch, 820)
|
||
self.setLineWidth(1)
|
||
self.line(74, 50, 6.8 * inch, 50)
|
||
|
||
def save(self):
|
||
num_pages = len(self._saved_page_states)
|
||
for state in self._saved_page_states:
|
||
self.__dict__.update(state)
|
||
self.draw_header_and_footer(num_pages)
|
||
canvas.Canvas.showPage(self)
|
||
canvas.Canvas.save(self)
|
||
|
||
|
||
class MyDocTemplate(BaseDocTemplate):
|
||
def __init__(self, filename, **kw):
|
||
self.allowSplitting = 0
|
||
BaseDocTemplate.__init__(self, filename, **kw)
|
||
template = PageTemplate('normal', [Frame(2.5 * cm, 2.5 * cm, 15 * cm, 25 * cm, id='F1')])
|
||
self.addPageTemplates(template)
|
||
|
||
def afterFlowable(self, flowable):
|
||
if flowable.__class__.__name__ == 'Paragraph':
|
||
text = flowable.getPlainText()
|
||
style = flowable.style.name
|
||
|
||
if style == 'chapter_style':
|
||
level = 1
|
||
elif style == 'section_style':
|
||
level = 2
|
||
else:
|
||
return
|
||
|
||
e = (level, text, self.page - offset)
|
||
self.notify('TOCEntry', e)
|
||
|
||
|
||
class ReportGenerator:
|
||
|
||
def __init__(self, **kwargs):
|
||
# 文本数据
|
||
self.text_model = kwargs['text_model']
|
||
# from static.test.test_data import demo
|
||
# self.model = demo
|
||
# 调用模板,在临时报告文件夹中创建指定名称的PDF文档
|
||
self.doc = MyDocTemplate(gen_pdf_path(name=kwargs['name']))
|
||
# 内容框
|
||
self.story = list()
|
||
|
||
# 生成报告
|
||
def gen_report(self):
|
||
self.gen_cover()
|
||
self.gen_menu()
|
||
self.gen_main_part()
|
||
|
||
# 将内容输出到PDF中
|
||
self.doc.multiBuild(self.story, canvasmaker=HeaderAndFooterCanvas)
|
||
|
||
# 设置章节、小节标题
|
||
def set_head(self, text, sty):
|
||
bn = sha1("Vintage".encode('utf-8')).hexdigest()
|
||
h = Paragraph(text + '<a name="%s"/>' % bn, sty)
|
||
h._bookmarkName = bn
|
||
self.story.append(h)
|
||
|
||
# 目录
|
||
def gen_menu(self):
|
||
toc = TableOfContents()
|
||
toc.levelStyles = [toc_style_1, toc_style_2]
|
||
self.story.append(Paragraph('目录', table_content_style))
|
||
self.story.append(toc)
|
||
self.story.append(PageBreak())
|
||
|
||
# 封面
|
||
def gen_cover(self):
|
||
data = self.text_model
|
||
self.story.append(PageBreak())
|
||
self.story.append(Paragraph('.', cover_space))
|
||
self.story.append(Paragraph(data['企业名称'], cover_company_style))
|
||
self.story.append(Paragraph('综合信用等级评价报告', cover_report_style))
|
||
self.story.append(Image(get_pic_path(pic_name='ydzx.png'), width=202, height=37))
|
||
self.story.append(Paragraph(data['生成日期'], cover_time_style))
|
||
self.story.append(PageBreak())
|
||
|
||
# 正文
|
||
def gen_main_part(self):
|
||
data = self.text_model['报告内容']
|
||
|
||
for chapter in data:
|
||
self.set_head(chapter['章节'], chapter_style)
|
||
|
||
for section in chapter['章节内容']:
|
||
self.set_head(section['小节'], section_style)
|
||
|
||
for part in section['小节内容']:
|
||
|
||
# 段落
|
||
if list(part.keys())[0] == '段落':
|
||
self.story.append(Paragraph(adjust_line_width(part['段落'], 8), para_style_single))
|
||
# self.story.append(Paragraph(part['段落'], para_style_single))
|
||
|
||
# 节点
|
||
elif list(part.keys())[0] == '节点':
|
||
self.story.append(Paragraph(part['节点'], para_style_bold))
|
||
|
||
# 表名
|
||
elif list(part.keys())[0] == '表名':
|
||
self.story.append(Paragraph(part['表名'], table_name))
|
||
|
||
# 单位
|
||
elif list(part.keys())[0] == '单位':
|
||
self.story.append(Paragraph(part['单位'], table_unit))
|
||
|
||
# 注释
|
||
elif list(part.keys())[0] == '注释':
|
||
self.story.append(Paragraph(part['注释'], table_mark))
|
||
|
||
# 表格
|
||
elif list(part.keys())[0] == '表格':
|
||
width = adjust_table_widths(part['表格'])
|
||
style = adjust_table_style(part['表格'])
|
||
self.story.append(Table(part['表格'], colWidths=width, style=style))
|
||
|
||
|
||
if __name__ == '__main__':
|
||
model = ReportGenerator(name='远东资信评估有限公司')
|
||
model.gen_report()
|